]> git.madduck.net Git - etc/vim.git/blob - src/black/report.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Solved Problem with Non-ASCII .gitignore Files (#2229)
[etc/vim.git] / src / black / report.py
1 """
2 Summarize Black runs to users.
3 """
4 from dataclasses import dataclass
5 from enum import Enum
6 from pathlib import Path
7
8 from click import style
9
10 from black.output import out, err
11
12
13 class Changed(Enum):
14     NO = 0
15     CACHED = 1
16     YES = 2
17
18
19 @dataclass
20 class Report:
21     """Provides a reformatting counter. Can be rendered with `str(report)`."""
22
23     check: bool = False
24     diff: bool = False
25     quiet: bool = False
26     verbose: bool = False
27     change_count: int = 0
28     same_count: int = 0
29     failure_count: int = 0
30
31     def done(self, src: Path, changed: Changed) -> None:
32         """Increment the counter for successful reformatting. Write out a message."""
33         if changed is Changed.YES:
34             reformatted = "would reformat" if self.check or self.diff else "reformatted"
35             if self.verbose or not self.quiet:
36                 out(f"{reformatted} {src}")
37             self.change_count += 1
38         else:
39             if self.verbose:
40                 if changed is Changed.NO:
41                     msg = f"{src} already well formatted, good job."
42                 else:
43                     msg = f"{src} wasn't modified on disk since last run."
44                 out(msg, bold=False)
45             self.same_count += 1
46
47     def failed(self, src: Path, message: str) -> None:
48         """Increment the counter for failed reformatting. Write out a message."""
49         err(f"error: cannot format {src}: {message}")
50         self.failure_count += 1
51
52     def path_ignored(self, path: Path, message: str) -> None:
53         if self.verbose:
54             out(f"{path} ignored: {message}", bold=False)
55
56     @property
57     def return_code(self) -> int:
58         """Return the exit code that the app should use.
59
60         This considers the current state of changed files and failures:
61         - if there were any failures, return 123;
62         - if any files were changed and --check is being used, return 1;
63         - otherwise return 0.
64         """
65         # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
66         # 126 we have special return codes reserved by the shell.
67         if self.failure_count:
68             return 123
69
70         elif self.change_count and self.check:
71             return 1
72
73         return 0
74
75     def __str__(self) -> str:
76         """Render a color report of the current state.
77
78         Use `click.unstyle` to remove colors.
79         """
80         if self.check or self.diff:
81             reformatted = "would be reformatted"
82             unchanged = "would be left unchanged"
83             failed = "would fail to reformat"
84         else:
85             reformatted = "reformatted"
86             unchanged = "left unchanged"
87             failed = "failed to reformat"
88         report = []
89         if self.change_count:
90             s = "s" if self.change_count > 1 else ""
91             report.append(
92                 style(f"{self.change_count} file{s} {reformatted}", bold=True)
93             )
94         if self.same_count:
95             s = "s" if self.same_count > 1 else ""
96             report.append(f"{self.same_count} file{s} {unchanged}")
97         if self.failure_count:
98             s = "s" if self.failure_count > 1 else ""
99             report.append(style(f"{self.failure_count} file{s} {failed}", fg="red"))
100         return ", ".join(report) + "."