summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/rbar_util/main.go
diff options
context:
space:
mode:
Diffstat (limited to '.config/wmii-hg/rbar_util/main.go')
-rw-r--r--.config/wmii-hg/rbar_util/main.go76
1 files changed, 76 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)
+}