]> git.madduck.net Git - etc/vim.git/blob - src/black/output.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:

Don't include profiling/ to cut down sdist by ~2x (#2362)
[etc/vim.git] / src / black / output.py
1 """Nice output for Black.
2
3 The double calls are for patching purposes in tests.
4 """
5
6 from typing import Any, Optional
7 from mypy_extensions import mypyc_attr
8 import tempfile
9
10 from click import echo, style
11
12
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:
16             styles["bold"] = True
17         message = style(message, **styles)
18     echo(message, nl=nl, err=True)
19
20
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:
24             styles["fg"] = "red"
25         message = style(message, **styles)
26     echo(message, nl=nl, err=True)
27
28
29 def out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
30     _out(message, nl=nl, **styles)
31
32
33 def err(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None:
34     _err(message, nl=nl, **styles)
35
36
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`."""
39     import difflib
40
41     a_lines = [line for line in a.splitlines(keepends=True)]
42     b_lines = [line for line in b.splitlines(keepends=True)]
43     diff_lines = []
44     for line in difflib.unified_diff(
45         a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5
46     ):
47         # Work around https://bugs.python.org/issue2142
48         # See:
49         # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
50         if line[-1] == "\n":
51             diff_lines.append(line)
52         else:
53             diff_lines.append(line + "\n")
54             diff_lines.append("\\ No newline at end of file\n")
55     return "".join(diff_lines)
56
57
58 def color_diff(contents: str) -> str:
59     """Inject the ANSI color codes to the diff."""
60     lines = contents.split("\n")
61     for i, line in enumerate(lines):
62         if line.startswith("+++") or line.startswith("---"):
63             line = "\033[1;37m" + line + "\033[0m"  # bold white, reset
64         elif line.startswith("@@"):
65             line = "\033[36m" + line + "\033[0m"  # cyan, reset
66         elif line.startswith("+"):
67             line = "\033[32m" + line + "\033[0m"  # green, reset
68         elif line.startswith("-"):
69             line = "\033[31m" + line + "\033[0m"  # red, reset
70         lines[i] = line
71     return "\n".join(lines)
72
73
74 @mypyc_attr(patchable=True)
75 def dump_to_file(*output: str, ensure_final_newline: bool = True) -> str:
76     """Dump `output` to a temporary file. Return path to the file."""
77     with tempfile.NamedTemporaryFile(
78         mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
79     ) as f:
80         for lines in output:
81             f.write(lines)
82             if ensure_final_newline and lines and lines[-1] != "\n":
83                 f.write("\n")
84     return f.name