def track_history(interval: int = 5, *, stop_after=None, notify=True):
"""Tracks clipboard history by polling at regular intervals.
Args:
interval (int): Seconds to wait between checks. Default is 5 seconds.
stop_after (int | None): Optional maximum duration (in seconds) before stopping.
notify (bool): Whether to show GUI notifications for new entries.
Writes:
Appends new clipboard entries to 'work/clip/history.txt', separated by '\n...\n'.
"""
import pyperclip
import os
history_path = gw.resource('work', 'clip', 'history.txt')
os.makedirs(os.path.dirname(history_path), exist_ok=True)
last_value = None
start_time = time.time()
gw.info(f"Started clipboard tracking every {interval}s.")
try:
while True:
current = pyperclip.paste()
clean_current = current.strip()
if not clean_current or clean_current == last_value:
time.sleep(interval)
continue
normalized = current.replace('\r\n', '\n').replace('\r', '\n').rstrip() + '\n'
with open(history_path, 'a', encoding='utf-8') as f:
if last_value is not None:
f.write("\n...\n")
f.write(normalized)
last_value = clean_current
if notify:
summary = clean_current if len(clean_current) <= 60 else clean_current[:57] + "..."
gw.screen.notify("Clipboard captured: " + summary)
gw.debug("New clipboard entry recorded.")
if stop_after and (time.time() - start_time) > stop_after:
gw.info("Clipboard tracking stopped after timeout.")
break
time.sleep(interval)
except KeyboardInterrupt:
gw.warning("Clipboard tracking manually interrupted.")