File: click.py

package info (click to toggle)
python-moreorless 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: python: 403; makefile: 40; sh: 11
file content (39 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
from pathlib import Path

import click

from . import unified_diff


def echo_color_unified_diff(astr: str, bstr: str, filename: str, n: int = 3) -> None:
    """
    Just like `moreorless.unified_diff` except using `click.secho`.
    """
    echo_color_precomputed_diff(unified_diff(astr, bstr, filename, n))


def echo_color_precomputed_diff(diff: str) -> None:
    """
    Like `echo_color_unified_diff`, but for precomputed diff results.
    """
    for line in diff.splitlines(True):
        # TODO benchmark and see if constructing the string up front is faster
        if line.startswith("---") or line.startswith("+++"):
            click.secho(line, bold=True, nl=False)
        elif line.startswith("@@"):
            click.secho(line, fg="cyan", nl=False)
        elif line.startswith("-"):
            click.secho(line, fg="red", nl=False)
        elif line.startswith("+"):
            click.secho(line, fg="green", nl=False)
        else:
            click.secho(line, nl=False)


def main(afile: str, bfile: str) -> None:  # pragma: no cover
    echo_color_unified_diff(Path(afile).read_text(), Path(bfile).read_text(), afile)


if __name__ == "__main__":  # pragma: no cover
    main(*sys.argv[1:])