"""Persistent list of recently opened SQL files (~/.dbclient/recent_files.json).""" import json from pathlib import Path _RECENT_FILE = Path.home() / ".dbclient" / "recent_files.json" _MAX = 10 def load_recent() -> list[str]: """Return up to _MAX recent file paths that still exist on disk.""" if not _RECENT_FILE.exists(): return [] try: with open(_RECENT_FILE, "r", encoding="utf-8") as f: data = json.load(f) return [p for p in data if isinstance(p, str) and Path(p).exists()][:_MAX] except Exception: return [] def add_recent(path: str) -> None: """Prepend path to the recent list, deduplicating and capping at _MAX.""" recent = load_recent() recent = [p for p in recent if p != path] recent.insert(0, path) _save(recent[:_MAX]) def clear_recent() -> None: _save([]) def _save(data: list) -> None: _RECENT_FILE.parent.mkdir(parents=True, exist_ok=True) try: with open(_RECENT_FILE, "w", encoding="utf-8") as f: json.dump(data, f, indent=2) except Exception: pass