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.
1 """Nice output for Black.
3 The double calls are for patching purposes in tests.
6 from typing import Any, Optional
7 from mypy_extensions import mypyc_attr
10 from click import echo, style
13 def _out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
14 if message is not None:
15 if "bold" not in styles:
17 message = style(message, **styles)
18 echo(message, nl=nl, err=True)
21 def _err(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
22 if message is not None:
23 if "fg" not in styles:
25 message = style(message, **styles)
26 echo(message, nl=nl, err=True)
29 def out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
30 _out(message, nl=nl, **styles)
33 def err(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
34 _err(message, nl=nl, **styles)
37 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
38 """Return a unified diff string between strings `a` and `b`."""
41 a_lines = [line for line in a.splitlines(keepends=True)]
42 b_lines = [line for line in b.splitlines(keepends=True)]
44 for line in difflib.unified_diff(
45 a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5
47 # Work around https://bugs.python.org/issue2142
48 # See https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
50 diff_lines.append(line)
52 diff_lines.append(line + "\n")
53 diff_lines.append("\\ No newline at end of file\n")
54 return "".join(diff_lines)
57 def color_diff(contents: str) -> str:
58 """Inject the ANSI color codes to the diff."""
59 lines = contents.split("\n")
60 for i, line in enumerate(lines):
61 if line.startswith("+++") or line.startswith("---"):
62 line = "\033[1;37m" + line + "\033[0m" # bold white, reset
63 elif line.startswith("@@"):
64 line = "\033[36m" + line + "\033[0m" # cyan, reset
65 elif line.startswith("+"):
66 line = "\033[32m" + line + "\033[0m" # green, reset
67 elif line.startswith("-"):
68 line = "\033[31m" + line + "\033[0m" # red, reset
70 return "\n".join(lines)
73 @mypyc_attr(patchable=True)
74 def dump_to_file(*output: str, ensure_final_newline: bool = True) -> str:
75 """Dump `output` to a temporary file. Return path to the file."""
76 with tempfile.NamedTemporaryFile(
77 mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
81 if ensure_final_newline and lines and lines[-1] != "\n":