Skip to content

Commit 71e9391

Browse files
committed
change the file change detection
1 parent 32e82ab commit 71e9391

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

json-schemas/schema2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"$id": "https://example.com/employee.schema.json",
4-
"title": "small json",
5-
"description": "This document records the details of an employee",
4+
"title": "Artikel",
5+
"description": "Example of a json schema for an article",
66
"type": "object",
77
"additionalProperties": false,
88
"properties": {

simple-git-editor.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import socket
1212
import json
1313
from jsonschema import validate, ValidationError
14+
from pathlib import Path
1415

1516
load_dotenv()
1617

@@ -33,8 +34,6 @@ def __init__(self, root):
3334
# Frames
3435
left_frame = ctk.CTkFrame(root)
3536
left_frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
36-
right_frame = ctk.CTkFrame(root)
37-
right_frame.pack(side=tk.TOP, padx=10, pady=10, fill=tk.X)
3837

3938
#buttons
4039
self.get_files_button = ctk.CTkButton(left_frame, text="📂 Get Files", command=self.get_files)
@@ -43,9 +42,9 @@ def __init__(self, root):
4342
self.save_files_button.pack(side=tk.LEFT)
4443

4544

46-
self.add_file_button = ctk.CTkButton(right_frame, text="➕ Add File", command=self.add_file)
45+
self.add_file_button = ctk.CTkButton(left_frame, text="➕ Add File", command=self.add_file)
4746
self.add_file_button.pack(side=tk.RIGHT)
48-
self.delete_file_button = ctk.CTkButton(right_frame, text="❌ Delete File", command=self.delete_file)
47+
self.delete_file_button = ctk.CTkButton(left_frame, text="❌ Delete File", command=self.delete_file)
4948
self.delete_file_button.pack(side=tk.RIGHT)
5049

5150

@@ -62,10 +61,9 @@ def __init__(self, root):
6261
self.file_list.heading("Filename", text="Filename", anchor=tk.W)
6362
self.file_list.bind("<Double-1>", self.on_file_click)
6463
self.file_list.pack(fill=tk.BOTH, expand=True, side=tk.BOTTOM)
65-
66-
self.file_states = {}
67-
self.check_for_changes()
64+
6865
self.populate_list(repo_path)
66+
self.check_for_changes()
6967

7068
def get_files(self):
7169
if not repo_url or not git_access_token:
@@ -116,7 +114,6 @@ def populate_list(self, repo_path):
116114
folders = ['']
117115

118116
self.file_list.delete(*self.file_list.get_children())
119-
self.file_states = {}
120117

121118
for folder in folders:
122119
folder_path = os.path.join(repo_path, folder)
@@ -125,26 +122,24 @@ def populate_list(self, repo_path):
125122
if any(filename.endswith(ext) for ext in valid_extensions):
126123
file_rel_path = os.path.join(folder, filename)
127124
file_full_path = os.path.join(folder_path, filename)
128-
self.file_states[file_rel_path] = os.path.getmtime(file_full_path)
129125
self.file_list.insert("", tk.END, values=("", file_rel_path), tags=('unchanged',))
130126
# tag for modified files
131-
self.file_list.tag_configure('modified', background='yellow')
127+
# self.file_list.tag_configure('modified', background='light_yellow')
132128

133129
def check_for_changes(self):
134-
for filename in self.file_states:
135-
file_path = os.path.join(repo_path, filename)
136-
if os.path.exists(file_path) and os.path.getmtime(file_path) != self.file_states[filename]:
137-
# File has been modified
138-
self.file_states[filename] = os.path.getmtime(file_path)
139-
self.highlight_modified(filename)
140-
130+
repo = Repo(repo_path)
131+
changed_files = [item.a_path for item in repo.index.diff(None)] + repo.untracked_files
132+
for filename in changed_files:
133+
self.highlight_modified(filename)
141134
# Schedule next check
142135
self.root.after(5000, self.check_for_changes)
143136

144137
def highlight_modified(self, filename):
145138
for item in self.file_list.get_children():
146-
if self.file_list.item(item, 'values')[1] == filename:
147-
self.file_list.item(item, values=("⭐", filename), tags=('modified',))
139+
treeview_filelist_item = os.path.normpath(os.path.normpath(self.file_list.item(item, 'values')[1]))
140+
filename_norm = os.path.normpath(filename)
141+
if treeview_filelist_item == filename_norm:
142+
self.file_list.item(item, values=("✏️", filename), tags=('modified',))
148143
break
149144

150145
def on_file_click(self, event):
@@ -174,6 +169,9 @@ def on_file_click(self, event):
174169
def show_json_dialog(self, json_data, schema, file_path):
175170
dialog = ctk.CTkToplevel(self.root)
176171
dialog.title(schema.get('title', 'JSON Data'))
172+
173+
dialog.transient(self.root)
174+
dialog.focus_set()
177175

178176
self.widget_references = {}
179177

@@ -286,7 +284,6 @@ def delete_file(self):
286284
if os.path.exists(file_path):
287285
os.remove(file_path)
288286
self.file_list.delete(selected_item)
289-
del self.file_states[filename]
290287

291288
def add_file(self):
292289
extensions = os.environ.get('file_extensions', '')
@@ -312,17 +309,17 @@ def add_file(self):
312309

313310
# Add to Treeview
314311
self.file_list.insert("", tk.END, values=("➕", filename), tags=('new_file',))
315-
self.file_states[filename] = os.path.getmtime(new_file_path)
316312

317313
# Configure tag for new files
318-
self.file_list.tag_configure('new_file', background='lime green')
314+
# self.file_list.tag_configure('new_file', background='lime green')
319315

320316
root = ctk.CTk()
321317
root.geometry("1024x768")
322318
###Treeview Customisation (theme colors are selected)
323319
bg_color = root._apply_appearance_mode(ctk.ThemeManager.theme["CTkFrame"]["fg_color"])
324320
text_color = root._apply_appearance_mode(ctk.ThemeManager.theme["CTkLabel"]["text_color"])
325321
selected_color = root._apply_appearance_mode(ctk.ThemeManager.theme["CTkButton"]["fg_color"])
322+
326323
style = ttk.Style()
327324
style.theme_use("default")
328325

0 commit comments

Comments
 (0)