Skip to content

Commit

Permalink
Merge pull request #46 from Carreau/color-diff
Browse files Browse the repository at this point in the history
Optionally print the diff in color.
  • Loading branch information
akaihola authored Aug 10, 2020
2 parents 5df4aa6 + a75237e commit 56575e4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Added
Fixed
-----
- Paths from ``--diff`` are now relative to current working directory, similar to output
from ``black --diff``
from ``black --diff``, and blank lines after the lines markers (``@@ ... @@``) have been removed.
- The ``--diff`` option will highlight syntax on screen if the ``pygments`` package is available.


1.0.0_ - 2020-07-15
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ The following `command line arguments`_ can also be used to modify the defaults:
.. code-block:: shell
--diff Don't write the files back, just output a diff for
each file on stdout
each file on stdout. Highlight syntax on screen if
the `pygments` package is available.
--check Don't write the files back, just return the status.
Return code 0 means nothing would change. Return code
Expand Down
9 changes: 9 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ ignore_missing_imports = True

[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-pygments.*]
ignore_missing_imports = True

[mypy-pygments.formatters.*]
ignore_missing_imports = True

[mypy-pygments.lexers.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ addopts =
--isort
--mypy
--doctest-modules
--durations 10
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ test =
pytest-black
pytest-isort>=1.1.0
pytest-mypy
pygments
23 changes: 17 additions & 6 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,24 @@ def modify_file(path: Path, new_content: str) -> None:
def print_diff(path: Path, old_content: str, new_lines: List[str]) -> None:
"""Print ``black --diff`` style output for the changes"""
relative_path = path.resolve().relative_to(Path.cwd()).as_posix()
difflines = list(
unified_diff(old_content.splitlines(), new_lines, relative_path, relative_path)
diff = "\n".join(
line.rstrip("\n")
for line in unified_diff(
old_content.splitlines(), new_lines, relative_path, relative_path,
)
)
header1, header2, *rest = difflines
print(header1, end="")
print(header2, end="")
print("\n".join(rest))

if sys.stdout.isatty():
try:
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import DiffLexer
except ImportError:
print(diff)
else:
print(highlight(diff, DiffLexer(), TerminalFormatter()))
else:
print(diff)


def main(argv: List[str] = None) -> int:
Expand Down
5 changes: 4 additions & 1 deletion src/darker/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def parse_command_line(argv: List[str]) -> Namespace:
parser.add_argument(
"--diff",
action="store_true",
help="Don't write the files back, just output a diff for each file on stdout",
help=(
"Don't write the files back, just output a diff for each file on stdout."
" Highlight syntax on screen if the `pygments` package is available."
),
)
parser.add_argument(
"--check",
Expand Down
5 changes: 0 additions & 5 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def test_format_edited_parts_empty():
'--- a.py',
'+++ a.py',
'@@ -1,3 +1,4 @@',
'',
' import sys',
' import os',
"-print( '42')",
Expand All @@ -82,7 +81,6 @@ def test_format_edited_parts_empty():
'--- a.py',
'+++ a.py',
'@@ -1,3 +1,4 @@',
'',
' import sys',
' import os',
"-print( '42')",
Expand All @@ -95,7 +93,6 @@ def test_format_edited_parts_empty():
'--- a.py',
'+++ a.py',
'@@ -1,3 +1,4 @@',
'',
'+import os',
' import sys',
'-import os',
Expand Down Expand Up @@ -189,15 +186,13 @@ def test_output_diff(capsys):
'--- a.py',
'+++ a.py',
'@@ -1,5 +1,5 @@',
'',
'+inserted',
' unchanged',
'-removed',
' kept 1',
' 2',
' 3',
'@@ -7,4 +7,4 @@',
'',
' 5',
' 6',
' 7',
Expand Down

0 comments on commit 56575e4

Please sign in to comment.