Skip to content

Commit 629e927

Browse files
added comments to the Diff_Util project
1 parent 53b7a7a commit 629e927

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

projects/Diff_Util/diff.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env python3
22

3+
# sys: for reading command-line arguments.
4+
# rich: for coloring the text.
35
import sys
46
from rich import print
57

8+
# Print Usage message if enough arguments are not passed.
69
if len(sys.argv) < 3:
710
print("Usage:")
811
print("\tMust provide two file names as command-line arguments.")
@@ -12,26 +15,34 @@
1215
orignal = sys.argv[1]
1316
changed = sys.argv[2]
1417

18+
# Read the contents of the files in lists.
1519
orignal_contents = open(orignal, "r").readlines()
1620
changed_contents = open(changed, "r").readlines()
1721

1822
color = "green"
1923
symbol = f"[bold {color}][+]"
2024

2125
print()
26+
27+
# Determine which file has changed much.
2228
if len(changed_contents) <= len(orignal_contents):
2329
color = "red"
2430
symbol = f"[bold {color}][-]"
2531
smallest_sloc, largest_sloc = changed_contents, orignal_contents
2632
else:
2733
smallest_sloc, largest_sloc = orignal_contents, changed_contents
2834

35+
# Go over all the lines to check the changes.
2936
for line in range(0, len(smallest_sloc)):
3037
if orignal_contents[line] == changed_contents[line]:
38+
# Ignore if the lines are same.
3139
continue
3240
else:
41+
# Display the changes on the respective lines of the files.
3342
print(f"[bold red][-] Line {line + 1}:[/bold red] {orignal_contents[line]}", end = "")
3443
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.
3546
if line == len(smallest_sloc) - 1:
3647
for new_line in range(line + 1, len(largest_sloc)):
3748
print(f"{symbol} Line {new_line + 1}:[/bold {color}] {largest_sloc[new_line]}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rich==10.11.0

0 commit comments

Comments
 (0)