]> 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:

Reorganize docs v2 (GH-2174)
[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 https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
49         if line[-1] == "\n":
50             diff_lines.append(line)
51         else:
52             diff_lines.append(line + "\n")
53             diff_lines.append("\\ No newline at end of file\n")
54     return "".join(diff_lines)
55
56
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
69         lines[i] = line
70     return "\n".join(lines)
71
72
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"
78     ) as f:
79         for lines in output:
80             f.write(lines)
81             if ensure_final_newline and lines and lines[-1] != "\n":
82                 f.write("\n")
83     return f.name