beholder icon indicating copy to clipboard operation
beholder copied to clipboard

Handle file paths gracefully

Open jyn514 opened this issue 6 months ago • 0 comments

Currently, beholder can only handle directories, not files (i.e. it inherits https://github.com/gmethvin/directory-watcher/issues/95). It would be nice to work around this upstream. Here is some code I use in my own project—maybe this fits into the API?

(require  [babashka.fs :as fs])

(defn on-file-change
  [cb paths event]
    ; behold doesn't support file filters, only directory filters. implement them ourselves.
    (when (contains? paths (-> event :path))
         (cb event)))

(defn to-dir [path]
  (let [dir (if (fs/directory? path) path (fs/parent path))]
    (-> dir fs/real-path str)))

(defn watch-files
  [cb paths]
  (let [abs-paths (set (map fs/real-path paths))
        dirs (set (map to-dir abs-paths))]
    (apply behold/watch #(on-file-change cb abs-paths %) dirs)))

jyn514 avatar Aug 10 '25 03:08 jyn514