|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
| 3 | +# sys: for reading command-line arguments. |
| 4 | +# rich: for coloring the text. |
3 | 5 | import sys |
4 | 6 | from rich import print |
5 | 7 |
|
| 8 | +# Print Usage message if enough arguments are not passed. |
6 | 9 | if len(sys.argv) < 3: |
7 | 10 | print("Usage:") |
8 | 11 | print("\tMust provide two file names as command-line arguments.") |
|
12 | 15 | orignal = sys.argv[1] |
13 | 16 | changed = sys.argv[2] |
14 | 17 |
|
| 18 | +# Read the contents of the files in lists. |
15 | 19 | orignal_contents = open(orignal, "r").readlines() |
16 | 20 | changed_contents = open(changed, "r").readlines() |
17 | 21 |
|
18 | 22 | color = "green" |
19 | 23 | symbol = f"[bold {color}][+]" |
20 | 24 |
|
21 | 25 | print() |
| 26 | + |
| 27 | +# Determine which file has changed much. |
22 | 28 | if len(changed_contents) <= len(orignal_contents): |
23 | 29 | color = "red" |
24 | 30 | symbol = f"[bold {color}][-]" |
25 | 31 | smallest_sloc, largest_sloc = changed_contents, orignal_contents |
26 | 32 | else: |
27 | 33 | smallest_sloc, largest_sloc = orignal_contents, changed_contents |
28 | 34 |
|
| 35 | +# Go over all the lines to check the changes. |
29 | 36 | for line in range(0, len(smallest_sloc)): |
30 | 37 | if orignal_contents[line] == changed_contents[line]: |
| 38 | + # Ignore if the lines are same. |
31 | 39 | continue |
32 | 40 | else: |
| 41 | + # Display the changes on the respective lines of the files. |
33 | 42 | print(f"[bold red][-] Line {line + 1}:[/bold red] {orignal_contents[line]}", end = "") |
34 | 43 | print(f"[bold green][+] Line {line + 1}:[/bold green] {changed_contents[line]}") |
| 44 | + |
| 45 | + # Show the additions [+] or deletions [-] for the file that is the largest. |
35 | 46 | if line == len(smallest_sloc) - 1: |
36 | 47 | for new_line in range(line + 1, len(largest_sloc)): |
37 | 48 | print(f"{symbol} Line {new_line + 1}:[/bold {color}] {largest_sloc[new_line]}") |
0 commit comments