From daf3465cee2f57421fb9d4096f64e96dea277bae Mon Sep 17 00:00:00 2001 From: Rafal Jeczalik Date: Sat, 13 Sep 2014 17:03:17 +0200 Subject: [PATCH] Move utility funcs to util.go --- notify_fsnotify.go | 73 ---------------------- util.go | 81 +++++++++++++++++++++++++ notify_fsnotify_test.go => util_test.go | 0 3 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 util.go rename notify_fsnotify_test.go => util_test.go (100%) diff --git a/notify_fsnotify.go b/notify_fsnotify.go index 8ec9d31..963725b 100644 --- a/notify_fsnotify.go +++ b/notify_fsnotify.go @@ -2,25 +2,12 @@ package notify import ( "os" - "path/filepath" - "sort" - "strings" "sync" old "gopkg.in/fsnotify.v1" ) -var ( - wd string - sep = string(os.PathSeparator) -) - func init() { - dir, err := os.Getwd() - if err != nil { - panic(err) - } - wd = dir w, err := old.NewWatcher() if err != nil { panic(err) @@ -33,50 +20,6 @@ func init() { } } -func abs(path string) string { - if !filepath.IsAbs(path) { - path = filepath.Join(wd, path) - } - return filepath.Clean(path) -} - -// TODO(rjeczalik): Sort by directory depth? -func appendset(s []string, x string) []string { - n := len(s) - if n == 0 { - return []string{x} - } - i := sort.SearchStrings(s, x) - if i != n && s[i] == x { - return s - } - if i == n { - return append(s, x) - } - s = append(s, s[n-1]) - copy(s[i+1:], s[i:n]) - s[i] = x - return s -} - -func splitabs(p string) (s []string) { - if p == "" || p == "." || p == sep { - return - } - i := strings.Index(p, sep) + 1 - if i == 0 || i == len(p) { - return - } - for i < len(p) { - j := strings.Index(p[i:], sep) - if j == -1 { - j = len(p) - i - } - s, i = append(s, p[i:i+j]), i+j+1 - } - return -} - type watcher struct { ch []chan<- string n int @@ -90,22 +33,6 @@ type fsnotify struct { refn map[string]uint } -// TODO(rjeczalik): Remove isdir? Don't care if path is a file or a directory at -// the moment of creating a watcher? -func joinevents(events []Event, isdir bool) (e Event) { - if len(events) == 0 || (len(events) == 1 && events[0] == Recursive) { - e = All - } else { - for _, event := range events { - e |= event - } - } - if !isdir { - e &= ^Recursive - } - return -} - func (fs *fsnotify) Watch(name string, c chan<- EventInfo, events ...Event) { if c == nil { panic("notify: Watch using nil channel") diff --git a/util.go b/util.go new file mode 100644 index 0000000..7f62040 --- /dev/null +++ b/util.go @@ -0,0 +1,81 @@ +package notify + +import ( + "os" + "path/filepath" + "sort" + "strings" +) + +var ( + wd string + sep = string(os.PathSeparator) +) + +func init() { + dir, err := os.Getwd() + if err != nil { + panic(err) + } + wd = dir +} + +func abs(path string) string { + if !filepath.IsAbs(path) { + path = filepath.Join(wd, path) + } + return filepath.Clean(path) +} + +// TODO(rjeczalik): Sort by directory depth? +func appendset(s []string, x string) []string { + n := len(s) + if n == 0 { + return []string{x} + } + i := sort.SearchStrings(s, x) + if i != n && s[i] == x { + return s + } + if i == n { + return append(s, x) + } + s = append(s, s[n-1]) + copy(s[i+1:], s[i:n]) + s[i] = x + return s +} + +func splitabs(p string) (s []string) { + if p == "" || p == "." || p == sep { + return + } + i := strings.Index(p, sep) + 1 + if i == 0 || i == len(p) { + return + } + for i < len(p) { + j := strings.Index(p[i:], sep) + if j == -1 { + j = len(p) - i + } + s, i = append(s, p[i:i+j]), i+j+1 + } + return +} + +// TODO(rjeczalik): Remove isdir? Don't care if path is a file or a directory at +// the moment of creating a watcher? +func joinevents(events []Event, isdir bool) (e Event) { + if len(events) == 0 || (len(events) == 1 && events[0] == Recursive) { + e = All + } else { + for _, event := range events { + e |= event + } + } + if !isdir { + e &= ^Recursive + } + return +} diff --git a/notify_fsnotify_test.go b/util_test.go similarity index 100% rename from notify_fsnotify_test.go rename to util_test.go