Skip to content

Commit 65f8c74

Browse files
committed
[__main__] add support for unified diffs and unified color diffs
1 parent 58ad8da commit 65f8c74

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

lib/fdiff/__main__.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import os
45
import sys
56
import argparse
67

78
from fdiff import __version__
9+
from fdiff.color import color_unified_diff_line
10+
from fdiff.diff import u_diff
11+
from fdiff.utils import file_exists
812

913

1014
def main(): # pragma: no cover
@@ -15,19 +19,40 @@ def run(argv):
1519
# ===========================================================
1620
# argparse command line argument definitions
1721
# ===========================================================
18-
parser = argparse.ArgumentParser(
19-
description="A font OpenType table diff tool"
20-
)
22+
parser = argparse.ArgumentParser(description="A font OpenType table diff tool")
2123
parser.add_argument(
2224
"--version", action="version", version="fdiff v{}".format(__version__)
2325
)
24-
parser.add_argument('-c', action='store_true', default=False,
25-
help='ANSI escape code colored diff')
26-
parser.add_argument('-l', '--lines', type=int, default=3,
27-
help='Number of context lines (default 3)')
28-
parser.add_argument('PREFILE',
29-
help='Font file path 1')
30-
parser.add_argument('POSTFILE',
31-
help='Font file path 2')
26+
parser.add_argument(
27+
"-c",
28+
"--color",
29+
action="store_true",
30+
default=False,
31+
help="ANSI escape code colored diff",
32+
)
33+
parser.add_argument(
34+
"-l", "--lines", type=int, default=3, help="Number of context lines (default 3)"
35+
)
36+
parser.add_argument("PREFILE", help="Font file path 1")
37+
parser.add_argument("POSTFILE", help="Font file path 2")
3238

3339
args = parser.parse_args(argv)
40+
41+
if not file_exists(args.PREFILE):
42+
sys.stderr.write(
43+
f"[*] ERROR: The file path '{args.PREFILE}' can not be found.{os.linesep}"
44+
)
45+
sys.exit(1)
46+
if not file_exists(args.POSTFILE):
47+
sys.stderr.write(
48+
f"[*] ERROR: The file path '{args.POSTFILE}' can not be found.{os.linesep}"
49+
)
50+
sys.exit(1)
51+
52+
diff = u_diff(args.PREFILE, args.POSTFILE, context_lines=args.lines)
53+
54+
if args.color:
55+
for line in diff:
56+
sys.stdout.write(color_unified_diff_line(line))
57+
else:
58+
sys.stdout.writelines(diff)

0 commit comments

Comments
 (0)