From fae50137a9cb2545823b989c12eb675ca6b3fed0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 23 Nov 2025 12:15:29 +0530 Subject: [PATCH] Add a clear cache action --- kittens/choose_files/main.go | 9 +++++++++ kittens/choose_files/main.py | 5 +++++ tools/disk_cache/api.go | 6 ++++++ tools/disk_cache/implementation.go | 31 ++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index 19017940c..99c9dc4ba 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -748,6 +748,15 @@ var default_cwd string var use_light_colors bool func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) { + if opts.ClearCache { + c, err := preview_cache() + if err != nil { + return 1, err + } + if err = c.Clear(); err != nil { + return 1, err + } + } write_output := func(selections []string, interrupted bool, current_filter string) { payload := make(map[string]any) if err != nil { diff --git a/kittens/choose_files/main.py b/kittens/choose_files/main.py index ff8270c78..1868c6d59 100644 --- a/kittens/choose_files/main.py +++ b/kittens/choose_files/main.py @@ -217,6 +217,11 @@ def handle_result(args: list[str], data: dict[str, Any], target_window_id: int, --write-pid-to Path to a file to which to write the process ID (PID) of this process to. + + +--clear-cache +type=bool-set +Clear the caches used by this kitten. '''.format(config_help=CONFIG_HELP.format(conf_name='choose-files', appname=appname)).format diff --git a/tools/disk_cache/api.go b/tools/disk_cache/api.go index d7d8cecb5..9b9ac8cf1 100644 --- a/tools/disk_cache/api.go +++ b/tools/disk_cache/api.go @@ -66,6 +66,12 @@ func (dc *DiskCache) Remove(key string) (err error) { return dc.remove(key) } +func (dc *DiskCache) Clear() (err error) { + dc.lock() + defer dc.unlock() + return dc.clear() +} + func (dc *DiskCache) AddPath(path, key string, items map[string][]byte) (ans map[string]string, err error) { dc.lock() defer dc.unlock() diff --git a/tools/disk_cache/implementation.go b/tools/disk_cache/implementation.go index e9ae1d053..a420e7bcc 100644 --- a/tools/disk_cache/implementation.go +++ b/tools/disk_cache/implementation.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "slices" + "strings" "syscall" "time" @@ -60,6 +61,8 @@ func get_file_state_from_path(path string) (*file_state, error) { } } +const GET_DIR_PREFIX = "getdir-" + func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) { if path, err = filepath.Abs(path); err != nil { return @@ -81,7 +84,7 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) { if _, err := ans.prune(); err != nil { return nil, err } - if ans.get_dir, err = os.MkdirTemp(ans.Path, "getdir-*"); err != nil { + if ans.get_dir, err = os.MkdirTemp(ans.Path, GET_DIR_PREFIX+"*"); err != nil { return } if err = utils.AtExitRmtree(ans.get_dir); err != nil { @@ -129,7 +132,9 @@ func (dc *DiskCache) unlock() { } } -func (dc *DiskCache) entries_path() string { return filepath.Join(dc.Path, "entries.json") } +const ENTRIES_NAME = "entries.json" + +func (dc *DiskCache) entries_path() string { return filepath.Join(dc.Path, ENTRIES_NAME) } func (dc *DiskCache) write_entries_if_dirty() (err error) { if !dc.entries_dirty { @@ -321,6 +326,28 @@ func (dc *DiskCache) get(key string, items []string) (map[string]string, error) return ans, dc.write_entries_if_dirty() } +func (dc *DiskCache) clear() (err error) { + if entries, err := os.ReadDir(dc.Path); err != nil { + return err + } else { + defer func() { + if we := dc.write_entries_if_dirty(); we != nil && err == nil { + err = we + } + }() + for _, x := range entries { + if x.IsDir() && !strings.HasPrefix(x.Name(), GET_DIR_PREFIX) { + _ = os.RemoveAll(filepath.Join(dc.Path, x.Name())) + } + } + dc.entries_dirty = true + dc.entry_map = make(map[string]*Entry) + dc.entries.SortedEntries = make([]*Entry, 0) + dc.entries.TotalSize = 0 + } + return +} + func (dc *DiskCache) remove(key string) (err error) { if err = dc.ensure_entries(); err != nil { return