This repository was archived by the owner on May 25, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathdiff.py
executable file
·48 lines (39 loc) · 1.62 KB
/
diff.py
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
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
# sys: for reading command-line arguments.
# rich: for coloring the text.
importsys
fromrichimportprint
# Print Usage message if enough arguments are not passed.
iflen(sys.argv) <3:
print("Usage:")
print("\tMust provide two file names as command-line arguments.")
print("\tdiff.py <orignal_file> <changed_file>")
exit(1)
orignal=sys.argv[1]
changed=sys.argv[2]
# Read the contents of the files in lists.
orignal_contents=open(orignal, "r").readlines()
changed_contents=open(changed, "r").readlines()
color="green"
symbol=f"[bold {color}][+]"
print()
# Determine which file has changed much.
iflen(changed_contents) <=len(orignal_contents):
color="red"
symbol=f"[bold {color}][-]"
smallest_sloc, largest_sloc=changed_contents, orignal_contents
else:
smallest_sloc, largest_sloc=orignal_contents, changed_contents
# Go over all the lines to check the changes.
forlineinrange(0, len(smallest_sloc)):
iforignal_contents[line] ==changed_contents[line]:
# Ignore if the lines are same.
continue
else:
# Display the changes on the respective lines of the files.
print(f"[bold red][-] Line {line+1}:[/bold red] {orignal_contents[line]}", end="")
print(f"[bold green][+] Line {line+1}:[/bold green] {changed_contents[line]}")
# Show the additions [+] or deletions [-] for the file that is the largest.
ifline==len(smallest_sloc) -1:
fornew_lineinrange(line+1, len(largest_sloc)):
print(f"{symbol} Line {new_line+1}:[/bold {color}] {largest_sloc[new_line]}")