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.
2 Summarize Black runs to users.
4 from dataclasses import dataclass
6 from pathlib import Path
8 from click import style
10 from black.output import out, err
21 """Provides a reformatting counter. Can be rendered with `str(report)`."""
29 failure_count: int = 0
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
40 if changed is Changed.NO:
41 msg = f"{src} already well formatted, good job."
43 msg = f"{src} wasn't modified on disk since last run."
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
52 def path_ignored(self, path: Path, message: str) -> None:
54 out(f"{path} ignored: {message}", bold=False)
57 def return_code(self) -> int:
58 """Return the exit code that the app should use.
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;
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:
70 elif self.change_count and self.check:
75 def __str__(self) -> str:
76 """Render a color report of the current state.
78 Use `click.unstyle` to remove colors.
80 if self.check or self.diff:
81 reformatted = "would be reformatted"
82 unchanged = "would be left unchanged"
83 failed = "would fail to reformat"
85 reformatted = "reformatted"
86 unchanged = "left unchanged"
87 failed = "failed to reformat"
90 s = "s" if self.change_count > 1 else ""
92 style(f"{self.change_count} file{s} {reformatted}", bold=True)
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) + "."