Skip to content

Instantly share code, notes, and snippets.

Last active May 12, 2022 21:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Reload browser on file-change (Ubuntu/xdotool/babashka)
#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod 'org.babashka/fswatcher "0.0.3")
(require '[pod.babashka.fswatcher :as fw])
(println "select window to reload")
;; This will block the script until the user has selected a window
(def browser-window (:out (shell/sh "xdotool" "selectwindow")))
(println "will reload" (:out (shell/sh "xdotool" "getwindowname" browser-window)))
(defn reload []
(println "reloading...")
(shell/sh "xdotool" "key" "--window" browser-window "F5"))
;; This implements a debounce mechanism where we want to
;; delay triggering the reload until we stop receiving events.
(def reload-op (atom nil))
(defn trigger-reload [_]
(let [[old _] (reset-vals! reload-op (future (Thread/sleep 20) (reload)))]
(when old (future-cancel old))))
(let [folder (first *command-line-args*)]
(fw/watch folder
{:delay-ms 0 :recursive true})
(println "watching" (str (fs/canonicalize folder))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment