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.
7 from typing import Any, Optional
8 from mypy_extensions import mypyc_attr
11 from click import echo, style
14 def _out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
15 if message is not None:
16 if "bold" not in styles:
18 message = style(message, **styles)
19 echo(message, nl=nl, err=True)
22 def _err(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
23 if message is not None:
24 if "fg" not in styles:
26 message = style(message, **styles)
27 echo(message, nl=nl, err=True)
30 def out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
31 _out(message, nl=nl, **styles)
34 def err(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
35 _err(message, nl=nl, **styles)
38 def ipynb_diff(a: str, b: str, a_name: str, b_name: str) -> str:
39 """Return a unified diff string between each cell in notebooks `a` and `b`."""
44 "".join(a_nb["cells"][cell_number]["source"]) + "\n",
45 "".join(b_nb["cells"][cell_number]["source"]) + "\n",
46 f"{a_name}:cell_{cell_number}",
47 f"{b_name}:cell_{cell_number}",
49 for cell_number, cell in enumerate(a_nb["cells"])
50 if cell["cell_type"] == "code"
52 return "".join(diff_lines)
55 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
56 """Return a unified diff string between strings `a` and `b`."""
59 a_lines = [line for line in a.splitlines(keepends=True)]
60 b_lines = [line for line in b.splitlines(keepends=True)]
62 for line in difflib.unified_diff(
63 a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5
65 # Work around https://bugs.python.org/issue2142
67 # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
69 diff_lines.append(line)
71 diff_lines.append(line + "\n")
72 diff_lines.append("\\ No newline at end of file\n")
73 return "".join(diff_lines)
76 def color_diff(contents: str) -> str:
77 """Inject the ANSI color codes to the diff."""
78 lines = contents.split("\n")
79 for i, line in enumerate(lines):
80 if line.startswith("+++") or line.startswith("---"):
81 line = "\033[1;37m" + line + "\033[0m" # bold white, reset
82 elif line.startswith("@@"):
83 line = "\033[36m" + line + "\033[0m" # cyan, reset
84 elif line.startswith("+"):
85 line = "\033[32m" + line + "\033[0m" # green, reset
86 elif line.startswith("-"):
87 line = "\033[31m" + line + "\033[0m" # red, reset
89 return "\n".join(lines)
92 @mypyc_attr(patchable=True)
93 def dump_to_file(*output: str, ensure_final_newline: bool = True) -> str:
94 """Dump `output` to a temporary file. Return path to the file."""
95 with tempfile.NamedTemporaryFile(
96 mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
100 if ensure_final_newline and lines and lines[-1] != "\n":