diff options
Diffstat (limited to '.config/wmii-hg/rbar_util')
-rw-r--r-- | .config/wmii-hg/rbar_util/main.go | 76 | ||||
-rw-r--r-- | .config/wmii-hg/rbar_util/util.go | 49 |
2 files changed, 125 insertions, 0 deletions
diff --git a/.config/wmii-hg/rbar_util/main.go b/.config/wmii-hg/rbar_util/main.go new file mode 100644 index 0000000..5fccd7e --- /dev/null +++ b/.config/wmii-hg/rbar_util/main.go @@ -0,0 +1,76 @@ +package rbar_util + +import ( + "os" + "io" + "fmt" +) + + +type Impl struct { + LeftClick func() error + MiddleClick func() error + RightClick func() error + ScrollUp func() error + ScrollDown func() error + Update func(tag string) error +} + +func gerror(status int, err error, format string, a ...interface{}) { + msg := fmt.Sprintf(format, a...) + if err == nil { + fmt.Fprintf(os.Stderr, "%s: %s", os.Args[0], msg) + } else { + fmt.Fprintf(os.Stderr, "%s: %s: %v", os.Args[0], msg, err) + } + if status != 0 { + os.Exit(status) + } +} + +func errusage(fmt string, a ...interface{}) { + gerror(0, nil, fmt, a...) + usage(os.Stderr) + os.Exit(2) +} + +func usage(w io.Writer) { + fmt.Fprintf(w, "Usage: %[1]v NN_LABEL\n or: %[1]v BTN_ID\n or: %[1]v -h\n", os.Args[0]) +} + +func Main(impl Impl) { + if len(os.Args) != 2 { + errusage("exactly 1 argument expected, got %d", len(os.Args)-1) + } + var fn func() error + switch os.Args[1] { + case "-h": + usage(os.Stdout) + os.Exit(0) + case "1": fn = impl.LeftClick; + case "2": fn = impl.MiddleClick; + case "3": fn = impl.RightClick; + case "4": fn = impl.ScrollUp; + case "5": fn = impl.ScrollDown; + default: + arg := os.Args[1] + if len(arg) > 3 && '0' <= arg[0] && arg[0] <= '9' && '0' <= arg[1] && arg[1] <= '9' && arg[2] == '_' { + if impl.Update != nil { + fn = func() error { return impl.Update(arg) } + } + } else { + errusage("invalid argument: %s\n", os.Args[1]) + } + } + if fn == nil { + fn = func() error { return nil } + } + if os.Getenv("XDG_RUNTIME_DIR") == "" { + gerror(6, nil, "XDG_RUNTIME_DIR isn't set") + } + err := fn() + if err != nil && err != NoRbar{ + gerror(1, err, "") + } + os.Exit(0) +} diff --git a/.config/wmii-hg/rbar_util/util.go b/.config/wmii-hg/rbar_util/util.go new file mode 100644 index 0000000..9e5d622 --- /dev/null +++ b/.config/wmii-hg/rbar_util/util.go @@ -0,0 +1,49 @@ +package rbar_util + +import ( + "os" + "io" + "path/filepath" + "strings" + "errors" + + "fmt" +) + +func GlobEscape(lit string) string { + glob := lit + glob = strings.Replace(glob, "\\", "\\\\", -1) + glob = strings.Replace(glob, "*", "\\*", -1) + glob = strings.Replace(glob, "?", "\\?", -1) + glob = strings.Replace(glob, "[", "\\[", -1) + return glob +} + +var NoRbar = errors.New("no WMII rbars found") + +func Write(filename string, msg string) error { + dirnames, _ := filepath.Glob(GlobEscape(os.Getenv("XDG_RUNTIME_DIR"))+"/wmii*/rbar") + if len(dirnames) == 0 { + return NoRbar + } + for _, dirname := range dirnames { + file, err := os.OpenFile(filepath.Join(dirname, filename), os.O_WRONLY| os.O_APPEND|os.O_CREATE, 0666) + if err != nil { + continue + } + io.WriteString(file, msg) + file.Close() + } + return nil +} + +func Remove(glob string) { + fmt.Println("remove", glob); + fullglob := GlobEscape(os.Getenv("XDG_RUNTIME_DIR"))+"/wmii*/rbar/"+glob + fmt.Println("glob", fullglob) + filenames, err := filepath.Glob(fullglob) + fmt.Println("globerr", err) + for _, filename := range filenames { + os.Remove(filename) + } +} |