Advertisement
here2share

# tk_QuickCompare.py inspired by github's version

Sep 5th, 2024 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. # tk_QuickCompare.py
  2. # inspired by github's version
  3.  
  4. import tkinter as tk
  5. from tkinter import messagebox
  6.  
  7. root = tk.Tk()
  8. root.geometry("640x640+0+0")
  9. root.title("QuickCompare")
  10.  
  11. text_area = tk.Text(root, wrap="none")
  12. text_area.pack(expand=True, fill="both")
  13.  
  14. def update_colors():
  15.     text_area.tag_configure("doc1_selected", background="white")
  16.     text_area.tag_configure("doc2_not_selected", background="light green")
  17.     text_area.tag_configure("doc1_removed", background="light pink")
  18.  
  19.     for i, tag in enumerate(selection_idx):
  20.         text_area.tag_add(tag, f"{i + 1}.0", f"{i + 1}.end + 1c")
  21.  
  22. def paste_doc2():
  23.     global selection_idx
  24.     try:
  25.         doc1_lines = text_area.get(1.0, tk.END).strip().split('\n')
  26.         doc2_lines = root.clipboard_get().strip().split('\n')
  27.  
  28.         merged_lines = []
  29.         selection_idx = []
  30.  
  31.         doc1_set = set(doc1_lines)
  32.         doc2_set = set(doc2_lines)
  33.  
  34.         for line in doc1_lines:
  35.             if line in doc2_set:
  36.                 merged_lines.append(line)
  37.                 selection_idx.append("doc1_selected")
  38.             else:
  39.                 merged_lines.append(line)
  40.                 selection_idx.append("doc1_removed")
  41.  
  42.         doc2_index = 0
  43.         for i, line in enumerate(merged_lines):
  44.             while doc2_index < len(doc2_lines) and doc2_lines[doc2_index] not in doc1_set:
  45.                 merged_lines.insert(i, doc2_lines[doc2_index])
  46.                 selection_idx.insert(i, "doc2_not_selected")
  47.                 doc2_index += 1
  48.                 i += 1
  49.             if doc2_index < len(doc2_lines) and doc2_lines[doc2_index] == line:
  50.                 doc2_index += 1
  51.  
  52.         while doc2_index < len(doc2_lines):
  53.             merged_lines.append(doc2_lines[doc2_index])
  54.             selection_idx.append("doc2_not_selected")
  55.             doc2_index += 1
  56.  
  57.         text_area.delete(1.0, tk.END)
  58.         text_area.insert(tk.END, '\n'.join(merged_lines))
  59.         update_colors()
  60.     except tk.TclError:
  61.         messagebox.showerror("Error", "Clipboard Is Empty Or Not Accessible.")
  62.  
  63. # Demo
  64. doc1 = """
  65. print("Hello, World!")
  66. x = 1
  67. y = 2
  68. z = x + y
  69. print(z)
  70. a = 3
  71. b = 4
  72. c = a + b
  73. print(c)
  74. # doc1
  75. d = 5
  76. e = 6
  77. f = d + e
  78. print(f)""".strip()
  79.  
  80. doc2 = """
  81. # demo
  82. print("Hello, World!")
  83. x = 1
  84. y = 2
  85. z = x + y
  86. print(z)
  87. def bar():
  88.    a = 3
  89.    b = 4
  90.    c = a + b
  91.    print(c)
  92. # doc1
  93. # doc2
  94. d = 5
  95. e = 6
  96. f = d + e
  97. print(f)
  98. bar()"""
  99.  
  100. text_area.delete(1.0, tk.END)
  101. text_area.insert(tk.END, doc1)
  102.  
  103. root.clipboard_clear()
  104. root.clipboard_append(doc2)
  105.  
  106. paste_button = tk.Button(root, text="Paste Doc2", command=paste_doc2)
  107. paste_button.pack()
  108.  
  109. root.mainloop()
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement