sigildocs

(sigil watch)

(sigil watch) - File Watching via Polling

Provides a portable, polling-based file watcher for detecting changes in source directories. Used by sigil build --watch and sigil test --watch.

(import (sigil watch))

;; Simple: watch directories, call back on changes
(watch-files '("src" "test")
  (lambda (changed-files) (println "Changed: ~a" changed-files)))

;; Low-level: manual poll loop
(let ((w (make-watcher '("src"))))
  (let loop ()
    (let ((changed (watcher-poll w)))
      (when (pair? changed)
        (println "Changed: ~a" changed)))
    (sleep 0.5)
    (loop)))

Exports

Collect (filepath . mtime) pairs for a list of files.

Returns an alist mapping each file path to its modification time. Files that don't exist get mtime 0.

(get-file-mtimes '("src/main.sgl" "src/lib.sgl"))
; => (("src/main.sgl" . 1702847123) ("src/lib.sgl" . 1702847100))

Check if any files have changed between two mtime snapshots.

Detects both content changes (mtime differs) and structural changes (files added or removed).

(files-changed? old-mtimes new-mtimes)  ; => #t or #f

Recursively find all files in the given directories.

Returns a flat list of file paths from all directories. Directories that don't exist are silently skipped.

(find-files-in-dirs '("src" "test"))
; => ("src/main.sgl" "src/lib.sgl" "test/test-main.sgl")
make-watcherprocedure

Create a watcher for the given directories.

Returns a mutable watcher object that tracks file modification times. Use watcher-poll to check for changes.

(define w (make-watcher '("src" "test")))
watcher-pollprocedure

Poll the watcher for changes.

Returns a list of changed file paths if any files have been modified, added, or removed since the last poll. Returns '() if nothing changed. Updates the watcher's internal state after each poll.

(let ((changed (watcher-poll w)))
  (when (pair? changed)
    (for-each (lambda (f) (println "Changed: ~a" f)) changed)))
watcher-stopprocedure

Stop the watcher.

After stopping, watcher-poll always returns '().

watch-filesprocedure

Watch directories and invoke a callback when files change.

Polls the given directories at interval: seconds (default 0.5) and calls on-change with the list of changed file paths. This function loops forever until interrupted.

(watch-files '("src" "test")
  (lambda (changed)
    (println "Files changed: ~a" changed))
  interval: 1.0)