// retoor <retoor@molodetz.nl>
import "fswatch" for FileWatcher, FsEvent
import "timer" for Timer
import "io" for File, FileFlags
System.print("=== FSWatch Module Demo ===\n")
var testFile = "/tmp/fswatch_demo.txt"
System.print("--- Setting up test file ---")
var f = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.create | FileFlags.truncate)
f.writeBytes("initial content", 0)
f.close()
System.print("Created test file: %(testFile)")
System.print("\n--- Starting file watcher ---")
var watcher = FileWatcher.new(testFile)
var eventCount = 0
watcher.start { |event|
eventCount = eventCount + 1
System.print("Event %(eventCount):")
System.print(" Filename: %(event.filename)")
System.print(" Is rename: %(event.isRename)")
System.print(" Is change: %(event.isChange)")
}
System.print("Watcher active: %(watcher.isActive)")
System.print("\n--- Modifying file ---")
Timer.immediate {
var f2 = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.truncate)
f2.writeBytes("first modification", 0)
f2.close()
System.print("Made first modification")
}
Timer.sleep(150)
Timer.immediate {
var f3 = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.truncate)
f3.writeBytes("second modification", 0)
f3.close()
System.print("Made second modification")
}
Timer.sleep(150)
System.print("\n--- Stopping watcher ---")
watcher.stop()
System.print("Watcher active: %(watcher.isActive)")
System.print("Total events received: %(eventCount)")