Skip to content

Commit

Permalink
fix: output encoded binary to avoid newline issues
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Oct 6, 2024
1 parent 370a937 commit d55f02f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from darker.help import LINTING_GUIDE, get_extra_instruction
from darker.import_sorting import apply_isort, isort
from darker.terminal import output
from darker.utils import debug_dump, glob_any
from darker.verification import ASTVerifier, BinarySearch, NotEquivalentError
from darkgraylib.command_line import (
Expand Down Expand Up @@ -415,7 +416,7 @@ def print_diff(
n=5, # Black shows 5 lines of context, do the same
)
)
print(colorize(diff, "diff", use_color))
output(colorize(diff, "diff", use_color), end="\n")


def print_source(new: TextDocument, use_color: bool) -> None:
Expand All @@ -428,11 +429,11 @@ def print_source(new: TextDocument, use_color: bool) -> None:
PythonLexer,
) = _import_pygments() # type: ignore
except ImportError:
print(new.string, end="")
output(new.string)
else:
print(highlight(new.string, PythonLexer(), TerminalFormatter()), end="")
output(highlight(new.string, PythonLexer(), TerminalFormatter()))
else:
print(new.string, end="")
output(new.string)


def _import_pygments(): # type: ignore
Expand Down
10 changes: 10 additions & 0 deletions src/darker/terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Terminal output helpers."""

import sys


def output(*args: str, end: str = "") -> None:
"""Print encoded binary output to terminal, with no newline by default."""
sys.stdout.buffer.write(*[arg.encode() for arg in args])
if end:
sys.stdout.buffer.write(end.encode())
9 changes: 5 additions & 4 deletions src/darker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Collection, List

from darker.terminal import output
from darkgraylib.utils import DiffChunk

logger = logging.getLogger(__name__)
Expand All @@ -14,14 +15,14 @@ def debug_dump(black_chunks: List[DiffChunk], edited_linenums: List[int]) -> Non
if logger.getEffectiveLevel() > logging.DEBUG:
return
for offset, old_lines, new_lines in black_chunks:
print(80 * "-")
output(80 * "-", end="\n")
for delta, old_line in enumerate(old_lines):
linenum = offset + delta
edited = "*" if linenum in edited_linenums else " "
print(f"{edited}-{linenum:4} {old_line}")
output(f"{edited}-{linenum:4} {old_line}", end="\n")
for _, new_line in enumerate(new_lines):
print(f" + {new_line}")
print(80 * "-")
output(f" + {new_line}", end="\n")
output(80 * "-", end="\n")


def glob_any(path: Path, patterns: Collection[str]) -> bool:
Expand Down

0 comments on commit d55f02f

Please sign in to comment.