#!/usr/bin/env python3 import os import tkinter as tk from tkinter import scrolledtext import shutil import ctypes import string import configparser # ============================================================ # CONFIG: config = configparser.ConfigParser() config.read(r"C:\ProgramData\Program-Search\config.ini") # PATHS SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"] TRASH_DIR = config["Paths"]["TRASH_DIR"] # FEATURES EDITOR_ENABLED = config.getboolean("Features", "EDITOR_ENABLED", fallback=True) AUTO_CLOSE_SECONDS = config.getint("Features", "AUTO_CLOSE_SECONDS", fallback=30) # UI TEXT_FONT = config.get("UI", "TEXT-font", fallback="Consolas") SEARCH_WIDTH = config.getint("UI", "SEARCH_WIDTH", fallback=400) SEARCH_HEIGHT = config.getint("UI", "SEARCH_HEIGHT", fallback=150) SEARCH_FONT_SIZE = config.getint("UI", "SEARCH_FONT_SIZE", fallback=12) RESULTS_WIDTH = config.getint("UI", "RESULTS_WIDTH", fallback=1000) RESULTS_HEIGHT = config.getint("UI", "RESULTS_HEIGHT", fallback=700) RESULTS_FONT_SIZE = config.getint("UI", "RESULTS_FONT_SIZE", fallback=11) PREVIEW_WIDTH = config.getint("UI", "PREVIEW_WIDTH", fallback=1200) PREVIEW_HEIGHT = config.getint("UI", "PREVIEW_HEIGHT", fallback=900) PREVIEW_FONT_SIZE = config.getint("UI", "PREVIEW_FONT_SIZE", fallback=24) ERROR_WIDTH = config.getint("UI", "ERROR_WIDTH", fallback=400) ERROR_HEIGHT = config.getint("UI", "ERROR_HEIGHT", fallback=100) ERROR_FONT_SIZE = config.getint("UI", "ERROR_FONT_SIZE", fallback=11) # BUTTONS BUTTON_SIZE_SEARCH = config.getint("Buttons", "BUTTON_SIZE_SEARCH", fallback=15) BUTTON_SIZE_OPEN = config.getint("Buttons", "BUTTON_SIZE_OPEN", fallback=15) BUTTON_SIZE_SELECT_ALL = config.getint("Buttons", "BUTTON_SIZE_SELECT_ALL", fallback=15) BUTTON_SIZE_SAVE = config.getint("Buttons", "BUTTON_SIZE_SAVE", fallback=15) BUTTON_SIZE_COPY = config.getint("Buttons", "BUTTON_SIZE_COPY", fallback=15) BUTTON_SIZE_TRASH = config.getint("Buttons", "BUTTON_SIZE_TRASH", fallback=15) BUTTON_SIZE_CLOSE = config.getint("Buttons", "BUTTON_SIZE_CLOSE", fallback=15) BUTTON_SIZE_MESSAGE = config.getint("Buttons", "BUTTON_SIZE_MESSAGE", fallback=15) BUTTON_SIZE_YES_NO = config.getint("Buttons", "BUTTON_SIZE_YES_NO", fallback=15) # ============================================================ os.makedirs(TRASH_DIR, exist_ok=True) def center_window(win, width, height): screen_width = win.winfo_screenwidth() screen_height = win.winfo_screenheight() x = int((screen_width / 2) - (width / 2)) y = int((screen_height / 2) - (height / 2)) win.geometry(f"{width}x{height}+{x}+{y}") def custom_message(title, message): win = tk.Toplevel() win.title(title) win.protocol("WM_DELETE_WINDOW", win.destroy) tk.Label( win, text=message, font=(TEXT_FONT, ERROR_FONT_SIZE), wraplength=ERROR_WIDTH - 30, justify="center" ).pack(padx=15, pady=15) tk.Button(win, text="OK", font=(TEXT_FONT, BUTTON_SIZE_MESSAGE), command=win.destroy).pack(pady=10) win.bind("", lambda e: win.destroy()) center_window(win, ERROR_WIDTH, ERROR_HEIGHT) win.lift() win.focus_force() win.transient() win.grab_set() win.update() win.wait_window() def custom_yesno(title, message): win = tk.Toplevel() win.title(title) result = {"value": False} tk.Label( win, text=message, font=(TEXT_FONT, ERROR_FONT_SIZE), wraplength=ERROR_WIDTH - 30, justify="center" ).pack(padx=15, pady=15) button_frame = tk.Frame(win) button_frame.pack(pady=10) def yes(): result["value"] = True win.destroy() def no(): result["value"] = False win.destroy() tk.Button(button_frame, text="Yes", width=BUTTON_SIZE_YES_NO, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=yes).pack(side="left", padx=10) tk.Button(button_frame, text="No", width=BUTTON_SIZE_YES_NO, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=no).pack(side="left", padx=10) win.protocol("WM_DELETE_WINDOW", no) center_window(win, ERROR_WIDTH, ERROR_HEIGHT) win.lift() win.focus_force() win.grab_set() win.wait_window() return result["value"] def popup_search_input(): win = tk.Toplevel() win.title("Search") win.geometry(f"{SEARCH_WIDTH}x{SEARCH_HEIGHT}") tk.Label(win, text="Enter part number or name:", font=(TEXT_FONT, SEARCH_FONT_SIZE)).pack(pady=10) entry = tk.Entry(win, font=(TEXT_FONT, SEARCH_FONT_SIZE)) entry.pack(pady=5) win.lift() win.attributes("-topmost", True) result = {"value": None, "force_close": False} def submit(): result["value"] = entry.get().strip().lower() win.destroy() def force_close(): result["force_close"] = True win.destroy() entry.bind("", lambda e: submit()) tk.Button(win, text="Search", font=(TEXT_FONT, BUTTON_SIZE_SEARCH), command=submit).pack(pady=10) center_window(win, SEARCH_WIDTH, SEARCH_HEIGHT) win.protocol("WM_DELETE_WINDOW", force_close) win.after(50, lambda: (entry.focus_set(), entry.icursor(tk.END))) win.after(100, lambda: win.attributes("-topmost", False)) win.wait_window() return result["value"] def search_files(term): matches = [] term_lower = term.lower() for root, dirs, files in os.walk(TRASH_DIR): for fname in files: path = os.path.join(root, fname) filename_match = term_lower in fname.lower() contents_match = False try: with open(path, "r", errors="ignore") as f: if term_lower in f.read().lower(): contents_match = True except Exception: pass if filename_match or contents_match: matches.append(path) return matches def popup_results_list(matches): win = tk.Toplevel() win.title("Matches Found") win.geometry(f"{RESULTS_WIDTH}x{RESULTS_HEIGHT}") tk.Label(win, text="Select files to preview:", font=(TEXT_FONT, RESULTS_FONT_SIZE)).pack(pady=10) lb = tk.Listbox(win, selectmode=tk.MULTIPLE, font=(TEXT_FONT, RESULTS_FONT_SIZE)) lb.pack(expand=True, fill="both") lb.bind("", lambda e: submit()) for m in matches: parent = os.path.basename(os.path.dirname(m)) filename = os.path.basename(m) display = f"{parent}\\{filename}" lb.insert(tk.END, display) result = {"selected": []} def select_all(): lb.select_set(0, tk.END) def submit(): sel = lb.curselection() if not sel: custom_message("No Selection", "Please select at least 1 file.") return result["selected"] = [matches[i] for i in sel] win.destroy() tk.Button(win, text="Open Selected", font=(TEXT_FONT, BUTTON_SIZE_OPEN), command=submit).pack(padx=10) tk.Button(win, text="Select All", font=(TEXT_FONT, BUTTON_SIZE_SELECT_ALL), command=select_all).pack(padx=10) center_window(win, RESULTS_WIDTH, RESULTS_HEIGHT) win.lift() win.focus_force() win.wait_window() return result["selected"] def popup_file_preview(path, on_close_callback): win = tk.Toplevel() height = min(PREVIEW_HEIGHT, win.winfo_screenheight() - 80) win.geometry(f"{PREVIEW_WIDTH}x{height}") frame = tk.Frame(win) frame.pack(side="bottom", fill="x") box = scrolledtext.ScrolledText(win, wrap=tk.WORD, font=(TEXT_FONT, PREVIEW_FONT_SIZE)) box.pack(expand=True, fill="both", padx=5, pady=10) try: with open(path, "r", errors="ignore") as f: original_text = f.read() box.insert(tk.END, original_text) box.edit_modified(False) except Exception as e: original_text = "" box.insert(tk.END, f"Error reading file:\n{e}") modified = {"changed": False} def mark_modified(event=None): if box.edit_modified(): modified["changed"] = True box.edit_modified(False) if EDITOR_ENABLED: box.bind("<>", mark_modified) else: box.configure(state="disabled") def save_file(): try: with open(path, "w", errors="ignore") as f: f.write(box.get("1.0", tk.END)) modified["changed"] = False custom_message("Saved", f"File saved:\n{path}") except Exception as e: custom_message("Save Error", str(e)) if EDITOR_ENABLED: tk.Button(frame, text="Save", fg="green", font=(TEXT_FONT, BUTTON_SIZE_SAVE), command=save_file).pack(side="left", padx=10) def restore_file(): if EDITOR_ENABLED and modified["changed"]: ask = custom_yesno("Unsaved Changes", "You have unsaved changes.\nSave before restoring?") if ask: save_file() try: rel_path = os.path.relpath(path, TRASH_DIR) dest_path = os.path.join(SEARCH_ROOT, rel_path) os.makedirs(os.path.dirname(dest_path), exist_ok=True) shutil.move(path, dest_path) custom_message("Restored", f"File restored to:\n{dest_path}") except Exception as e: custom_message("Restore Error", str(e)) def close(): if EDITOR_ENABLED and modified["changed"]: ask = custom_yesno("Unsaved Changes", "You have unsaved changes.\nSave before closing?") if ask: save_file() win.destroy() on_close_callback() def auto_close(): if win.winfo_exists(): win.destroy() on_close_callback() win.protocol("WM_DELETE_WINDOW", close) win.after(AUTO_CLOSE_SECONDS * 1000, auto_close) tk.Button(frame, text="Restore File", fg="blue", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=restore_file).pack(side="left", padx=10) tk.Button(frame, text="Close", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=close).pack(side="right", padx=10) def main(): root = tk.Tk() root.withdraw() while True: result = popup_search_input() search_term = result["value"] if result["force_close"]: root.destroy() return if not search_term: custom_message("Invalid Search", "Please enter a search term.") continue matches = search_files(search_term) if not matches: custom_message("No Results", "No matching files found.") continue break selected = popup_results_list(matches) if not selected: root.destroy() return open_windows = {"count": len(selected)} def window_closed(): open_windows["count"] -= 1 if open_windows["count"] == 0: root.destroy() for path in selected: popup_file_preview(path, window_closed) root.mainloop() if __name__ == "__main__": main()