diff --git a/README.md b/README.md index 388b388..1b73f1f 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@ python3 -m pip install . python3 scripts/demo.py ``` -Install from this checkout to use the reviewed source version. The installed tool has no third-party runtime dependencies; cloning and installation can require network access. - ## Features ### Choose your diff diff --git a/src/diffnote/cli.py b/src/diffnote/cli.py index 20961be..6d0fd7b 100644 --- a/src/diffnote/cli.py +++ b/src/diffnote/cli.py @@ -30,8 +30,12 @@ def build_parser(): help="Print a GitHub PR-body markdown.") fmt.add_argument("--json", action="store_true", help="Print machine-readable JSON.") - fmt.add_argument("--no-color", action="store_true", - help="Disable ANSI colors.") + color = fmt.add_mutually_exclusive_group() + color.add_argument("--color", action="store_true", dest="use_color", + help="Force ANSI colors, even when output is redirected.") + color.add_argument("--no-color", action="store_false", dest="use_color", + help="Disable ANSI colors.") + p.set_defaults(use_color=None) fmt.add_argument("--llm", action="store_true", help="Enhance with a local Ollama model (needs Ollama running).") return p @@ -87,7 +91,7 @@ def main(argv=None): elif args.markdown: print(render.render_markdown(summary, llm_prose)) else: - print(render.render_terminal(summary, use_color=not args.no_color, llm_prose=llm_prose)) + print(render.render_terminal(summary, use_color=args.use_color, llm_prose=llm_prose)) return 0 diff --git a/src/diffnote/render.py b/src/diffnote/render.py index 6785715..01bd1d5 100644 --- a/src/diffnote/render.py +++ b/src/diffnote/render.py @@ -1,6 +1,7 @@ """Output renderers: terminal (ANSI), markdown (PR body), json.""" import json as _json +import sys RESET = "\033[0m" BOLD = "\033[1m" @@ -16,7 +17,9 @@ def _c(text, code, use_color): return f"{code}{text}{RESET}" if use_color else text -def render_terminal(summary, use_color=True, llm_prose=None): +def render_terminal(summary, use_color=None, llm_prose=None): + if use_color is None: + use_color = sys.stdout.isatty() out = [] bar = "─" * 42 out.append(_c("diffnote", BOLD + CYAN, use_color) + diff --git a/tests/test_cli.py b/tests/test_cli.py index 1cc13d6..62201d7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,6 +3,8 @@ import subprocess import tempfile +import pytest + from diffnote import cli @@ -58,6 +60,25 @@ def test_json_output(capsys): assert "title" in data +@pytest.mark.parametrize("is_tty,args,has_color", [ + (True, [], True), + (False, [], False), + (True, ["--color"], True), + (False, ["--color"], True), + (True, ["--no-color"], False), + (False, ["--no-color"], False), +]) +def test_terminal_color_selection(capsys, monkeypatch, is_tty, args, has_color): + monkeypatch.chdir(_make_dirty_repo()) + monkeypatch.setattr(cli.sys.stdout, "isatty", lambda: is_tty) + + assert cli.main(args) == 0 + + out = capsys.readouterr().out + assert "files changed" in out + assert ("\033[" in out) is has_color + + def test_markdown_output(capsys): import os d = _make_dirty_repo()