Skip to content

Commit

Permalink
V0.12.0 Added basic file comparison capability
Browse files Browse the repository at this point in the history
  • Loading branch information
domhnallmorr committed Oct 17, 2021
1 parent 62bb390 commit 061f1b1
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
Binary file modified src/__pycache__/branch_tab.cpython-39.pyc
Binary file not shown.
Binary file added src/__pycache__/file_comparison.cpython-39.pyc
Binary file not shown.
Binary file modified src/__pycache__/sidebar_tree.cpython-39.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions src/branch_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import address_bar
import autoscrollbar
import explorer_backend
import file_comparison
import search_window
import treeview_functions

Expand Down Expand Up @@ -123,6 +124,9 @@ def OnRightClick(self, event):
file_name = self.treeview.item(iid, 'text')
popup_menu.add_command(label="Open in Text Editor", command=self.open_in_text_editor)
popup_menu.add_command(label="Rename", command=lambda mode='edit', initialvalue=file_name: self.new_file(mode, initialvalue))
popup_menu.add_command(label="Left Compare", command=self.left_compare)
if self.mainapp.file_compare_left:
popup_menu.add_command(label="Right Compare", command=self.right_compare)
popup_menu.add_separator()

new_menu = tk.Menu(popup_menu, tearoff = 0)
Expand Down Expand Up @@ -239,7 +243,17 @@ def paste_file(self):
def search(self):
self.w=search_window.SearchWindow(self.mainapp, self.master, self)
self.master.wait_window(self.w.top)

def left_compare(self):
current_selection = treeview_functions.get_current_selection(self.treeview)
self.mainapp.file_compare_left = os.path.join(self.explorer.current_directory, current_selection[1][0])

def right_compare(self):
current_selection = treeview_functions.get_current_selection(self.treeview)
self.mainapp.file_compare_right = os.path.join(self.explorer.current_directory, current_selection[1][0])

self.w=file_comparison.ComparisonWindow(self.mainapp, self.master, self)
self.master.wait_window(self.w.top)
class AddFoldersWindow(ttk.Frame):
def __init__(self, mainapp, master, branch_tab):
super(AddFoldersWindow, self).__init__()
Expand Down
75 changes: 75 additions & 0 deletions src/file_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import difflib
import os
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from tkinter import messagebox
from tkinter import simpledialog

import autoscrollbar

class ComparisonWindow(ttk.Frame):
def __init__(self, mainapp, master, branch_tab):
super(ComparisonWindow, self).__init__()
top=self.top=Toplevel(master)
top.grab_set()
self.mainapp = mainapp
self.branch_tab = branch_tab

# File name labels
ttk.Label(self.top, text=self.mainapp.file_compare_left).grid(row=0, column=0, columnspan=8, sticky='NSEW', padx=5, pady=5, ipadx=2, ipady=5)
ttk.Label(self.top, text=self.mainapp.file_compare_right).grid(row=0, column=8, columnspan=8, sticky='NSEW', padx=5, pady=5, ipadx=2, ipady=5)

# Text widgets
self.left_text = tk.Text(self.top, wrap="none", width=50, height=20)
self.left_text.grid(row=1, column=0, columnspan=8, sticky='NSEW', padx=5, pady=5, ipadx=2, ipady=5)

self.right_text = tk.Text(self.top, wrap="none", width=50, height=20)
self.right_text.grid(row=1, column=8, columnspan=8, sticky='NSEW', padx=5, pady=5, ipadx=2, ipady=5)

vsb = autoscrollbar.AutoScrollbar(self.top, orient="vertical", command=self.multiple_yview)
vsb.grid(row=1, column=16, sticky='NSEW')
self.left_text.configure(yscrollcommand=vsb.set)
self.right_text.configure(yscrollcommand=vsb.set)

# Tags for text widgets
for t in [self.left_text, self.right_text]:
t.tag_configure("SAME", foreground='white', background='dark green')
t.tag_configure("DIFF", foreground='white', background='red4')

self.top.grid_rowconfigure(1, weight=1)
self.top.grid_columnconfigure(0, weight=1)
self.top.grid_columnconfigure(8, weight=1)

self.compare_files()

def compare_files(self):
f1 = open(self.mainapp.file_compare_left, "r").readlines()
f2 = open(self.mainapp.file_compare_right, "r").readlines()

self.differ = difflib.Differ()
self.comparison = self.differ.compare(f1, f2)
self.update_text_widgets()

def update_text_widgets(self):
for line in self.comparison:
marker = line[0]
if marker == " ":
# line is same in both
self.left_text.insert("end", line[2:], 'SAME')
self.right_text.insert("end", line[2:], 'SAME')

elif marker == "-":
# line is only on the left
self.left_text.insert("end", line[2:], 'DIFF')
self.right_text.insert("end", "\n")

elif marker == "+":
# line is only on the right
self.left_text.insert("end", "\n")
self.right_text.insert("end", line[2:], 'DIFF')

def multiple_yview(self, *args):
self.left_text.yview(*args)
self.right_text.yview(*args)
4 changes: 3 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, parent, *args, **kwargs):
self.setup_tabs()

def setup_variables(self):
self.version = '0.11.0'
self.version = '0.12.0'
self.parent.title(f"Tk Path Finder V{self.version}")
self.config_data = config_file_manager.load_config_file(self)

Expand All @@ -59,6 +59,8 @@ def setup_variables(self):
}

self.file_to_copy = None
self.file_compare_left = None
self.file_compare_right = None

def setup_menu(self):
menu = tk.Menu(self.master)
Expand Down
2 changes: 1 addition & 1 deletion src/tk_path_finder_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"Some node": {
"Link1": "C:\\Users\\domhn\\Documents\\Python\\Tk Path Finder\\src",
"Link2": "C:\\Users\\domhn\\Documents\\Python\\Tk Path Finder\\src"
"Link2": "C:\\Users\\domhn\\Downloads\\Tk-Path-Finder-0.10.0\\Tk-Path-Finder-0.10.0\\src"
}
},
"node_iids": {
Expand Down

0 comments on commit 061f1b1

Please sign in to comment.