From 70651dc9e438c8da8d08e96b90c8341f4c2f4969 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 23 Jul 2024 23:13:15 +0100 Subject: [PATCH] examples: improve hyprctl example --- examples/hyprctl/main.go | 70 ++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/examples/hyprctl/main.go b/examples/hyprctl/main.go index 28fcf2b..243e830 100644 --- a/examples/hyprctl/main.go +++ b/examples/hyprctl/main.go @@ -51,36 +51,52 @@ func main() { flag.Parse() + m := map[string]func(){ + "activewindow": func() { + v := must1(c.ActiveWindow()) + fmt.Printf("%s\n", mustMarshalIndent(v)) + }, + "activeworkspace": func() { + v := must1(c.ActiveWorkspace()) + fmt.Printf("%s\n", mustMarshalIndent(v)) + }, + "dispatch": func() { + dispatchFS.Parse(os.Args[2:]) + v := must1(c.Dispatch(dispatch...)) + fmt.Printf("%s\n", v) + }, + "kill": func() { + v := must1(c.Kill()) + fmt.Printf("%s\n", v) + }, + "reload": func() { + v := must1(c.Reload()) + fmt.Printf("%s\n", v) + }, + "setcursor": func() { + setcursorFS.Parse(os.Args[2:]) + v := must1(c.SetCursor(*theme, *size)) + fmt.Printf("%s\n", v) + }, + "version": func() { + v := must1(c.Version()) + fmt.Printf("%s\n", mustMarshalIndent(v)) + }, + } + if len(os.Args) < 2 { - fmt.Println("Expected subcommand.") + fmt.Println("Expected one of the following subcommands:") + for k := range m { + fmt.Printf("- %s\n", k) + } os.Exit(1) } - switch os.Args[1] { - case "activewindow": - v := must1(c.ActiveWindow()) - fmt.Printf("%s\n", mustMarshalIndent(v)) - case "activeworkspace": - v := must1(c.ActiveWorkspace()) - fmt.Printf("%s\n", mustMarshalIndent(v)) - case "dispatch": - dispatchFS.Parse(os.Args[2:]) - v := must1(c.Dispatch(dispatch...)) - fmt.Printf("%s\n", v) - case "kill": - v := must1(c.Kill()) - fmt.Printf("%s\n", v) - case "reload": - v := must1(c.Reload()) - fmt.Printf("%s\n", v) - case "setcursor": - setcursorFS.Parse(os.Args[2:]) - v := must1(c.SetCursor(*theme, *size)) - fmt.Printf("%s\n", v) - case "version": - v := must1(c.Version()) - fmt.Printf("%s\n", mustMarshalIndent(v)) - default: - fmt.Printf("[ERROR] Unknown command: %s\n", os.Args[1]) + subcmd := os.Args[1] + if fn, ok := m[subcmd]; ok { + fn() + } else { + fmt.Printf("Unknown subcommand: %s\n", subcmd) + os.Exit(1) } }