From 0670f01cc80d5717f47127f6f37e15bde788b095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bohusl=C3=A1vek?= Date: Thu, 17 Feb 2022 20:39:27 +0000 Subject: [PATCH] acme/Watch: Add -X flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is sometimes useful to watch 1. only a single file or 2. every file in Acme. The “-X regexp” option is analogous to Edit X/regexp/. The idioms for the use cases above are: 1. Watch -X $% cmd… 2. Watch -X . cmd… I've considered adding the complementary -Y flag, but it seemed less useful. --- acme/Watch/main.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/acme/Watch/main.go b/acme/Watch/main.go index c61c616..afb5aff 100644 --- a/acme/Watch/main.go +++ b/acme/Watch/main.go @@ -6,12 +6,16 @@ // // Usage: // -// Watch cmd [args...] +// Watch [-r | -X regexp] cmd [args...] // // Watch opens a new acme window named for the current directory // with a suffix of /+watch. The window shows the execution of the given -// command. Each time any file in that directory is Put from within acme, -// Watch reexecutes the command and updates the window. +// command. Each time any file in that directory (or any subdirectory +// when the -r flag is given) is Put from within acme, Watch reexecutes +// the command and updates the window. With the -r flag +// +// There's also the -X flag which makes Watch reexecute every time a +// file matching the pattern specified by -X is Put. // // The command and arguments are joined by spaces and passed to rc(1) // to be interpreted as a shell command line. @@ -51,9 +55,10 @@ var args []string var win *acme.Win var needrun = make(chan bool, 1) var recursive = flag.Bool("r", false, "watch all subdirectories recursively") +var filePattern = flag.String("X", "", "watch all acme files matching `regexp`") func usage() { - fmt.Fprintf(os.Stderr, "usage: Watch [-r] cmd args...\n") + fmt.Fprintf(os.Stderr, "usage: Watch [-r | -X regexp] cmd args...\n") os.Exit(2) } @@ -82,6 +87,20 @@ func main() { win.Fprintf("tag", "Get Kill Quit ") win.Fprintf("body", "%% %s\n", strings.Join(args, " ")) + matches := func(name string) bool { + return path.Dir(name) == pwd || *recursive && strings.HasPrefix(name, pwdSlash) + } + if *filePattern != "" { + if *recursive { + log.Fatal("cannot use both -r and -X flags") + } + re, err := regexp.Compile(*filePattern) + if err != nil { + log.Fatal(err) + } + matches = func(name string) bool { return re.MatchString(name) } + } + needrun <- true go events() go runner() @@ -95,7 +114,7 @@ func main() { if err != nil { log.Fatal(err) } - if ev.Op == "put" && (path.Dir(ev.Name) == pwd || *recursive && strings.HasPrefix(ev.Name, pwdSlash)) { + if ev.Op == "put" && matches(ev.Name) { select { case needrun <- true: default: