]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/black/output.py
f4c17f28ea418d363f9b388e3770c208ca26b1b1
1 """Nice output for Black.
3 The double calls are for patching purposes in tests.
8 from typing
import Any
, Optional
10 from click
import echo
, style
11 from mypy_extensions
import mypyc_attr
14 @mypyc_attr(patchable
=True)
15 def _out(message
: Optional
[str] = None, nl
: bool = True, **styles
: Any
) -> None:
16 if message
is not None:
17 if "bold" not in styles
:
19 message
= style(message
, **styles
)
20 echo(message
, nl
=nl
, err
=True)
23 @mypyc_attr(patchable
=True)
24 def _err(message
: Optional
[str] = None, nl
: bool = True, **styles
: Any
) -> None:
25 if message
is not None:
26 if "fg" not in styles
:
28 message
= style(message
, **styles
)
29 echo(message
, nl
=nl
, err
=True)
32 @mypyc_attr(patchable
=True)
33 def out(message
: Optional
[str] = None, nl
: bool = True, **styles
: Any
) -> None:
34 _out(message
, nl
=nl
, **styles
)
37 def err(message
: Optional
[str] = None, nl
: bool = True, **styles
: Any
) -> None:
38 _err(message
, nl
=nl
, **styles
)
41 def ipynb_diff(a
: str, b
: str, a_name
: str, b_name
: str) -> str:
42 """Return a unified diff string between each cell in notebooks `a` and `b`."""
47 "".join(a_nb
["cells"][cell_number
]["source"]) + "\n",
48 "".join(b_nb
["cells"][cell_number
]["source"]) + "\n",
49 f
"{a_name}:cell_{cell_number}",
50 f
"{b_name}:cell_{cell_number}",
52 for cell_number
, cell
in enumerate(a_nb
["cells"])
53 if cell
["cell_type"] == "code"
55 return "".join(diff_lines
)
58 def diff(a
: str, b
: str, a_name
: str, b_name
: str) -> str:
59 """Return a unified diff string between strings `a` and `b`."""
62 a_lines
= a
.splitlines(keepends
=True)
63 b_lines
= b
.splitlines(keepends
=True)
65 for line
in difflib
.unified_diff(
66 a_lines
, b_lines
, fromfile
=a_name
, tofile
=b_name
, n
=5
68 # Work around https://bugs.python.org/issue2142
70 # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
72 diff_lines
.append(line
)
74 diff_lines
.append(line
+ "\n")
75 diff_lines
.append("\\ No newline at end of file\n")
76 return "".join(diff_lines
)
79 def color_diff(contents
: str) -> str:
80 """Inject the ANSI color codes to the diff."""
81 lines
= contents
.split("\n")
82 for i
, line
in enumerate(lines
):
83 if line
.startswith("+++") or line
.startswith("---"):
84 line
= "\033[1m" + line
+ "\033[0m" # bold, reset
85 elif line
.startswith("@@"):
86 line
= "\033[36m" + line
+ "\033[0m" # cyan, reset
87 elif line
.startswith("+"):
88 line
= "\033[32m" + line
+ "\033[0m" # green, reset
89 elif line
.startswith("-"):
90 line
= "\033[31m" + line
+ "\033[0m" # red, reset
92 return "\n".join(lines
)
95 @mypyc_attr(patchable
=True)
96 def dump_to_file(*output
: str, ensure_final_newline
: bool = True) -> str:
97 """Dump `output` to a temporary file. Return path to the file."""
98 with tempfile
.NamedTemporaryFile(
99 mode
="w", prefix
="blk_", suffix
=".log", delete
=False, encoding
="utf8"
103 if ensure_final_newline
and lines
and lines
[-1] != "\n":