Skip to content

Commit

Permalink
examples: sort subcommand output in hyprctl
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Jul 24, 2024
1 parent 1ce94af commit 37e6540
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions examples/hyprctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"

"github.com/thiagokokada/hyprland-go"
)

var (
c *hyprland.RequestClient
c *hyprland.RequestClient
// default error/usage output
out io.Writer = flag.CommandLine.Output()
)

// Needed for an acumulator flag, i.e.: can be passed multiple times, get the
// results in a string array
// https://stackoverflow.com/a/28323276
type arrayFlags []string

Expand All @@ -41,10 +45,27 @@ func must(err error) {
}
}

// Unmarshal structs as JSON and indent output
func mustMarshalIndent(v any) []byte {
return must1(json.MarshalIndent(v, "", " "))
}

func usage(m map[string]func(args []string)) {
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(out, " %s [subcommand] <options>\n\n", os.Args[0])
fmt.Fprintf(out, "Available subcommands:\n")

// Sort keys before printing, since Go randomises order
subcommands := make([]string, 0, len(m))
for s := range m {
subcommands = append(subcommands, s)
}
sort.Strings(subcommands)
for _, s := range subcommands {
fmt.Fprintf(out, " - %s\n", s)
}
}

func main() {
batchFS := flag.NewFlagSet("batch", flag.ExitOnError)
var batch arrayFlags
Expand All @@ -60,17 +81,19 @@ func main() {
theme := setcursorFS.String("theme", "Adwaita", "Cursor theme")
size := setcursorFS.Int("size", 32, "Cursor size")

m := map[string]func(){
"activewindow": func() {
// Map pf subcommands to a function to handle the subcommand. Will
// receive the subcommand arguments as parameter
m := map[string]func(args []string){
"activewindow": func(_ []string) {
v := must1(c.ActiveWindow())
fmt.Printf("%s\n", mustMarshalIndent(v))
},
"activeworkspace": func() {
"activeworkspace": func(_ []string) {
v := must1(c.ActiveWorkspace())
fmt.Printf("%s\n", mustMarshalIndent(v))
},
"batch": func() {
batchFS.Parse(os.Args[2:])
"batch": func(args []string) {
batchFS.Parse(args)
if len(batch) == 0 {
fmt.Fprintf(out, "Error: at least one '-c' is required for batch.\n")
os.Exit(1)
Expand All @@ -84,8 +107,8 @@ func main() {
fmt.Printf("%s\n", v)
}
},
"dispatch": func() {
dispatchFS.Parse(os.Args[2:])
"dispatch": func(args []string) {
dispatchFS.Parse(args)
if len(dispatch) == 0 {
fmt.Fprintf(out, "Error: at least one '-c' is required for dispatch.\n")
os.Exit(1)
Expand All @@ -94,46 +117,39 @@ func main() {
fmt.Printf("%s\n", v)
}
},
"kill": func() {
"kill": func(_ []string) {
v := must1(c.Kill())
fmt.Printf("%s\n", v)
},
"reload": func() {
"reload": func(_ []string) {
v := must1(c.Reload())
fmt.Printf("%s\n", v)
},
"setcursor": func() {
"setcursor": func(_ []string) {
setcursorFS.Parse(os.Args[2:])
v := must1(c.SetCursor(*theme, *size))
fmt.Printf("%s\n", v)
},
"version": func() {
"version": func(_ []string) {
v := must1(c.Version())
fmt.Printf("%s\n", mustMarshalIndent(v))
},
}

flag.Usage = func() {
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(out, " %s [subcommand] <options>\n\n", os.Args[0])
fmt.Fprintf(out, "Available subcommands:\n")
for k := range m {
fmt.Fprintf(out, " - %s\n", k)
}
}
flag.Usage = func() { usage(m) }
flag.Parse()

if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}

subcmd := os.Args[1]
if run, ok := m[subcmd]; ok {
subcommand := os.Args[1]
if run, ok := m[subcommand]; ok {
c = hyprland.MustClient()
run()
run(os.Args[2:])
} else {
fmt.Fprintf(out, "Error: unknown subcommand: %s\n", subcmd)
fmt.Fprintf(out, "Error: unknown subcommand: %s\n", subcommand)
os.Exit(1)
}
}

0 comments on commit 37e6540

Please sign in to comment.