diff --git a/docs/README.md b/docs/README.md index 5c9426ae4..27ac311f8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -105,3 +105,4 @@ SR No | Project | Author 98 | [PNG to ICO converter](https://github.com/chavarera/python-mini-projects/tree/master/projects/convert_png_images_to_ico_format)| [weicheansoo](https://github.com/weicheansoo) 99 | [Find IMDB Ratings](https://github.com/chavarera/python-mini-projects/tree/master/projects/Find_imdb_rating)| [Utkarsh Bajaj](https://github.com/utkarshbajaj) 100 | [Terminal Based Hangman Game](https://github.com/chavarera/python-mini-projects/tree/master/projects/Terminal_Based_Hangman_Game)| [neohboonyee99](https://github.com/neohboonyee99) +101 | [Diff Utility](https://github.com/Python-World/python-mini-projects/tree/master/projects/Diff_Util)| [KILLinefficiency](https://github.com/KILLinefficiency) diff --git a/projects/Diff_Util/README.md b/projects/Diff_Util/README.md new file mode 100644 index 000000000..028986d36 --- /dev/null +++ b/projects/Diff_Util/README.md @@ -0,0 +1,62 @@ +# Diff Utility + +This program is a minimal clone of the UNIX ``diff`` program. + +``diff.py`` takes two file names as command-line arguments and compares them for changes. + +## Prerequisites +* Rich: ``rich==10.11.0`` + +## How to run the script +**Running on Windows:** + +``` +python diff.py +``` + +**Running on Linux / macOS:** + +``` +./diff.py +``` + +## Usage Example + +Consider two files ``v1`` and ``v2``: + +**v1**: +``` +Bruce +Alfred +Jason +``` + +**v2**: +``` +Batman +Alfred +Red Hood +Joker +Ra's Al Ghul +``` + +On running ``./diff.py v1 v2``, you'll get the following output: +``` + +[-] Line 1: Bruce +[+] Line 1: Batman + +[-] Line 3: Jason +[+] Line 3: Red Hood + +[+] Line 4: Joker + +[+] Line 5: Ra's Al Ghul + +``` + +## Screenshot +![Python Diff Utility](diff_util.jpg) + +# KILLinefficiency +Github Link: [KILLinefficiency](https://www.github.com/KILLinefficiency) diff --git a/projects/Diff_Util/diff.py b/projects/Diff_Util/diff.py new file mode 100755 index 000000000..9706d433c --- /dev/null +++ b/projects/Diff_Util/diff.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +# sys: for reading command-line arguments. +# rich: for coloring the text. +import sys +from rich import print + +# Print Usage message if enough arguments are not passed. +if len(sys.argv) < 3: + print("Usage:") + print("\tMust provide two file names as command-line arguments.") + print("\tdiff.py ") + 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. +if len(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. +for line in range(0, len(smallest_sloc)): + if orignal_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. + if line == len(smallest_sloc) - 1: + for new_line in range(line + 1, len(largest_sloc)): + print(f"{symbol} Line {new_line + 1}:[/bold {color}] {largest_sloc[new_line]}") diff --git a/projects/Diff_Util/diff_util.jpg b/projects/Diff_Util/diff_util.jpg new file mode 100644 index 000000000..ea3772d44 Binary files /dev/null and b/projects/Diff_Util/diff_util.jpg differ diff --git a/projects/Diff_Util/requirements.txt b/projects/Diff_Util/requirements.txt new file mode 100644 index 000000000..c6402e593 --- /dev/null +++ b/projects/Diff_Util/requirements.txt @@ -0,0 +1 @@ +rich==10.11.0