svc.fnl (995B)
1 (local inotify (require :inotify)) 2 (local { : file-exists? } (require :anoia)) 3 4 (fn read-line [name] 5 (with-open [f (assert (io.open name :r) (.. "can't open file " name))] 6 (f:read "*l"))) 7 8 (fn watch-fsevents [directory-name] 9 (let [handle (inotify.init)] 10 (handle:addwatch directory-name 11 inotify.IN_CREATE 12 inotify.IN_MOVE 13 inotify.IN_DELETE 14 inotify.IN_DELETE_SELF 15 inotify.IN_MOVED_FROM 16 inotify.IN_MOVED_TO 17 inotify.IN_CLOSE_WRITE) 18 handle)) 19 20 (fn open [directory] 21 (let [watcher (watch-fsevents directory) 22 has-file? (fn [filename] (file-exists? (.. directory "/" filename)))] 23 { 24 :wait (fn [] (watcher:read)) 25 :ready? (fn [self] 26 (and (has-file? "state") (not (has-file? ".lock")))) 27 :output (fn [_ filename] (read-line (.. directory "/" filename))) 28 :close #(watcher:close) 29 })) 30 31 { : open }