File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Diff Utility
2+
3+ This program is a minimal clone of the UNIX `` diff `` program.
4+
5+ `` diff.py `` takes two file names as command-line arguments and compares them for changes.
6+
7+ ## Dependencies:
8+ * Rich: `` rich==10.11.0 ``
9+
10+ ## Running on Windows:
11+
12+ ```
13+ python diff.py <orignal_file> <changed_file>
14+ ```
15+
16+ ## Running on Linux / macOS:
17+
18+ ```
19+ ./diff.py <orignal_file> <changed_file>
20+ ```
21+
22+ ## Example:
23+
24+ Consider two files `` v1 `` and `` v2 `` :
25+
26+ ** v1** :
27+ ```
28+ Bruce
29+ Alfred
30+ Jason
31+ ```
32+
33+ ** v2** :
34+ ```
35+ Batman
36+ Alfred
37+ Red Hood
38+ Joker
39+ Ra's Al Ghul
40+ ```
41+
42+ On running `` ./diff.py v1 v2 `` , you'll get the following output:
43+ ```
44+
45+ [-] Line 1: Bruce
46+ [+] Line 1: Batman
47+
48+ [-] Line 3: Jason
49+ [+] Line 3: Red Hood
50+
51+ [+] Line 4: Joker
52+
53+ [+] Line 5: Ra's Al Ghul
54+
55+ ```
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ import sys
4+ from rich import print
5+
6+ if len (sys .argv ) < 3 :
7+ print ("Error" )
8+ exit (1 )
9+
10+ orignal = sys .argv [1 ]
11+ changed = sys .argv [2 ]
12+
13+ orignal_contents = open (orignal , "r" ).readlines ()
14+ changed_contents = open (changed , "r" ).readlines ()
15+
16+ color = "green"
17+ symbol = f"[bold { color } ][+]"
18+
19+ print ()
20+ if len (changed_contents ) <= len (orignal_contents ):
21+ color = "red"
22+ symbol = f"[bold { color } ][-]"
23+ smallest_sloc , largest_sloc = changed_contents , orignal_contents
24+ else :
25+ smallest_sloc , largest_sloc = orignal_contents , changed_contents
26+
27+ for line in range (0 , len (smallest_sloc )):
28+ if orignal_contents [line ] == changed_contents [line ]:
29+ continue
30+ else :
31+ print (f"[bold red][-] Line { line + 1 } :[/bold red] { orignal_contents [line ]} " , end = "" )
32+ print (f"[bold green][+] Line { line + 1 } :[/bold green] { changed_contents [line ]} " )
33+ if line == len (smallest_sloc ) - 1 :
34+ for new_line in range (line + 1 , len (largest_sloc )):
35+ print (f"{ symbol } Line { new_line + 1 } :[/bold { color } ] { largest_sloc [new_line ]} " )
You can’t perform that action at this time.
0 commit comments