def get_filtered_history(self): search_term = self.search_var.get().lower() filtered = [item for item in self.history if search_term in item["text"].lower()] # Sort: pinned first, then by timestamp desc filtered.sort(key=lambda x: (x["text"], x["timestamp"]) not in self.pinned) return filtered
self.history = [] self.pinned = set() self.load_history() self.last_text = pyperclip.paste() self.running = True # GUI self.create_widgets() self.update_history_display() # Start background monitor self.monitor_thread = threading.Thread(target=self.monitor_clipboard, daemon=True) self.monitor_thread.start() self.root.protocol("WM_DELETE_WINDOW", self.on_close) windows clipboard history
def clear_history(self): if messagebox.askyesno("Clear History", "Delete all clipboard history?"): self.history = [] self.pinned.clear() self.save_history() self.update_history_display() self.status_var.set("History cleared") def get_filtered_history(self): search_term = self
def on_close(self): self.running = False self.save_history() self.root.destroy() if == " main ": root = tk.Tk() # Add placeholder text workaround class EntryWithPlaceholder(tk.Entry): def init (self, master=None, placeholder="", **kwargs): super(). init (master, **kwargs) self.placeholder = placeholder self.bind("<FocusIn>", self.on_focus_in) self.bind("<FocusOut>", self.on_focus_out) self.on_focus_out() def on_focus_in(self, e): if self.get() == self.placeholder: self.delete(0, tk.END) self.config(fg="black") def on_focus_out(self, e): if not self.get(): self.insert(0, self.placeholder) self.config(fg="grey") tk.Entry = EntryWithPlaceholder **kwargs): super(). init (master
def update_history_display(self): self.listbox.delete(0, tk.END) filtered = self.get_filtered_history() for item in filtered: text = item["text"].replace("\n", " ").replace("\r", "") if len(text) > 80: text = text[:77] + "..." # Pin indicator prefix = "📌 " if (item["text"], item["timestamp"]) in self.pinned else " " display = f"prefixtext" self.listbox.insert(tk.END, display) self.status_var.set(f"History: len(filtered) items (max MAX_HISTORY)")
def paste_selected(self): selection = self.listbox.curselection() if selection: filtered = self.get_filtered_history() idx = selection[0] if idx < len(filtered): text = filtered[idx]["text"] pyperclip.copy(text) self.status_var.set(f"Copied to clipboard: text[:50]...") # Flash window (optional) self.root.attributes('-topmost', True) self.root.after(1000, lambda: self.root.attributes('-topmost', False))