From 57e6a492ad52980fc388256312c67e5b810a6e6f Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sat, 30 Jul 2022 17:28:03 +0200 Subject: [PATCH] Add {Event,Op}.Has() (#477) Using: if event.Op&fsnotify.Create == fsnotify.Create { } is overly verbose and awkward; a Has() method makes this much nicer: if event.Has(fsnotify.Create) { } PR #76 was previously rejected; I think that HasCreate() etc. is overkill, but a Has() method makes the library quite a bit more convenient IMO. I added it to both the Event and Op to avoid confusion; with this we can change the Op field to be private in a future v2. Fixes #107 --- README.md | 2 +- cmd/fsnotify/main.go | 2 +- fsnotify.go | 40 ++++++++++++++++++++++++---------------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 41871a6..96c5f46 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ func main() { return } log.Println("event:", event) - if event.Op&fsnotify.Write == fsnotify.Write { + if event.Has(fsnotify.Write) { log.Println("modified file:", event.Name) } case err, ok := <-watcher.Errors: diff --git a/cmd/fsnotify/main.go b/cmd/fsnotify/main.go index 1b0356a..b92419b 100644 --- a/cmd/fsnotify/main.go +++ b/cmd/fsnotify/main.go @@ -42,7 +42,7 @@ func main() { i++ m := "" - if e.Op&fsnotify.Write == fsnotify.Write { + if e.Has(fsnotify.Write) { m = "(modified)" } line("%3d %-10s %-10s %q", i, e.Op, m, e.Name) diff --git a/fsnotify.go b/fsnotify.go index 6a305b5..1610d66 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -5,7 +5,8 @@ //go:build !plan9 // +build !plan9 -// Package fsnotify provides a platform-independent interface for file system notifications. +// Package fsnotify provides a cross-platform interface for file system +// notifications. package fsnotify import ( @@ -24,6 +25,9 @@ type Event struct { Name string // File operation that triggered the event. + // + // This is a bitmask as some systems may send multiple operations at once. + // Use the Op.Has() or Event.Has() method instead of comparing with ==. Op Op } @@ -40,30 +44,34 @@ const ( ) func (op Op) String() string { - // Use a builder for efficient string concatenation - var builder strings.Builder - - if op&Create == Create { - builder.WriteString("|CREATE") + var b strings.Builder + if op.Has(Create) { + b.WriteString("|CREATE") } - if op&Remove == Remove { - builder.WriteString("|REMOVE") + if op.Has(Remove) { + b.WriteString("|REMOVE") } - if op&Write == Write { - builder.WriteString("|WRITE") + if op.Has(Write) { + b.WriteString("|WRITE") } - if op&Rename == Rename { - builder.WriteString("|RENAME") + if op.Has(Rename) { + b.WriteString("|RENAME") } - if op&Chmod == Chmod { - builder.WriteString("|CHMOD") + if op.Has(Chmod) { + b.WriteString("|CHMOD") } - if builder.Len() == 0 { + if b.Len() == 0 { return "" } - return builder.String()[1:] // Strip leading pipe + return b.String()[1:] } +// Has reports if this operation has the given operation. +func (o Op) Has(h Op) bool { return o&h == h } + +// Has reports if this event has the given operation. +func (e Event) Has(op Op) bool { return e.Op.Has(op) } + // String returns a string representation of the event in the form // "file: REMOVE|WRITE|..." func (e Event) String() string { -- 2.50.1