Skip to content

Commit

Permalink
Add the -d / --stdout option. Fixes #164.
Browse files Browse the repository at this point in the history
This still needs refining. Currently it prints out every file which was
changed by reformatting modified lines. We probably want to only print
out a single file.
  • Loading branch information
akaihola committed Jul 17, 2021
1 parent 7e07535 commit 17efdca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ def print_diff(path: Path, old: TextDocument, new: TextDocument) -> None:
print(diff)


def print_source(new: TextDocument) -> None:
"""Print the reformatted Python source code"""
if sys.stdout.isatty():
try:
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers.python import PythonLexer
except ImportError:
print(new.string)
else:
print(highlight(new.string, PythonLexer(), TerminalFormatter()))
else:
print(new.string)


def main(argv: List[str] = None) -> int:
"""Parse the command line and reformat and optionally lint each source file
Expand Down Expand Up @@ -233,7 +248,7 @@ def main(argv: List[str] = None) -> int:
failures_on_modified_lines = False

revrange = RevisionRange.parse(args.revision)
write_modified_files = not args.check and not args.diff
write_modified_files = not args.check and not args.diff and not args.stdout
if revrange.rev2 != WORKTREE and write_modified_files:
raise ArgumentError(
Action(["-r", "--revision"], "revision"),
Expand All @@ -247,6 +262,8 @@ def main(argv: List[str] = None) -> int:
failures_on_modified_lines = True
if args.diff:
print_diff(path, old, new)
elif args.stdout:
print_source(new)
if write_modified_files:
modify_file(path, new)
if run_linters(args.lint, git_root, changed_files, revrange):
Expand Down
1 change: 1 addition & 0 deletions src/darker/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def add_arg(help_text: Optional[Text], *name_or_flags: Text, **kwargs: Any) -> N
add_arg(hlp.REVISION, "-r", "--revision", default="HEAD")
add_arg(hlp.DIFF, "--diff", action="store_true")
add_arg(hlp.CHECK, "--check", action="store_true")
add_arg(hlp.STDOUT, "-d", "--stdout", action="store_true")
add_arg(hlp.ISORT, "-i", "--isort", action="store_true")
add_arg(hlp.LINT, "-L", "--lint", action="append", metavar="CMD", default=[])
add_arg(hlp.CONFIG, "-c", "--config", metavar="PATH")
Expand Down
5 changes: 5 additions & 0 deletions src/darker/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
" reformatted."
)

STDOUT = (
"Force complete reformatted output to stdout, instead of in-place. Only valid if"
" there's just one file to reformat."
)

ISORT_PARTS = ["Also sort imports using the `isort` package"]
if not isort:
ISORT_PARTS.append(f". {ISORT_INSTRUCTION} to enable usage of this option.")
Expand Down

0 comments on commit 17efdca

Please sign in to comment.