Documentation
¶
Overview ¶
Package fswatch wraps macOS FSEvents without cgo, adding event coalescing, debouncing, typed event flags, and gitignore-style path filtering.
It is intended for workspace triggers such as live RAG re-indexing, coding-agent file watchers, and Metal shader hot-reload.
Watch starts an FSEvents stream on a dedicated Grand Central Dispatch serial queue and delivers coalesced batches of Event values on Watcher.Events. Call Watcher.Close or cancel the context passed to Watch to stop the stream and release underlying system resources.
Basic usage:
w, err := fswatch.Watch(ctx, []string{"."}, fswatch.Options{
Latency: 50 * time.Millisecond,
Debounce: 100 * time.Millisecond,
FileEvents: true,
IgnoreDotGit: true,
Ignore: []string{"build/", "*.tmp"},
})
if err != nil {
log.Fatal(err)
}
defer w.Close()
for batch := range w.Events() {
for _, ev := range batch {
fmt.Printf("%s %s\n", ev.Flags, ev.Path)
}
}
Example ¶
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/tmc/apple/x/fswatch"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
w, err := fswatch.Watch(ctx, []string{"."}, fswatch.Options{
Latency: 50 * time.Millisecond,
Debounce: 100 * time.Millisecond,
FileEvents: true,
IgnoreDotGit: true,
Ignore: []string{"build/", "*.tmp"},
})
if err != nil {
log.Fatal(err)
}
defer w.Close()
for batch := range w.Events() {
for _, ev := range batch {
fmt.Printf("%s %s\n", ev.Flags, ev.Path)
}
}
}
Output:
Index ¶
Examples ¶
Constants ¶
const DefaultLatency = 50 * time.Millisecond
DefaultLatency is the FSEvents kernel coalescing latency used when Options.Latency is zero.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Flags ¶
type Flags uint32
Flags is a bitmask of typed FSEvents item and stream flags.
func (Flags) String ¶
String returns a human-readable pipe-separated representation of f.
Example ¶
package main
import (
"fmt"
"github.com/tmc/apple/x/fswatch"
)
func main() {
flags := fswatch.Created | fswatch.Modified | fswatch.IsFile
fmt.Println(flags)
fmt.Println(flags.Has(fswatch.Modified))
}
Output: Created|Modified|IsFile true
type Options ¶
type Options struct {
// Latency is the FSEvents kernel coalescing window.
// Zero uses [DefaultLatency].
Latency time.Duration
// Debounce is an optional user-space coalescing window for rapid edits
// to the same path. Zero delivers each coalesced FSEvents batch
// immediately.
Debounce time.Duration
// FileEvents requests per-file events instead of directory-level events.
FileEvents bool
// Ignore is a list of gitignore-style patterns to exclude from delivered
// events (for example, "node_modules/", "build/", "*.tmp", "!keep.tmp").
Ignore []string
// IgnoreDotGit excludes .git directories and all files inside them.
IgnoreDotGit bool
}
Options configures a Watcher.
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher delivers coalesced filesystem events from a Darwin FSEventStream. A zero Watcher is not usable; create one with Watch.
func Watch ¶
Watch starts watching paths with macOS FSEvents and returns a Watcher. Canceling ctx stops the watcher and closes Watcher.Events.
func (*Watcher) Close ¶
Close stops the watcher, releases its FSEvents stream, and closes Watcher.Events. Close is safe to call multiple times and concurrently.