package rgb import ( "encoding/json" "fmt" "os" "github.com/spf13/cobra" "github.com/wobkey/rgb/internal/rgb" "github.com/wobkey/rgb/internal/via" ) func NewInfoCmd() *cobra.Command { return &cobra.Command{ Use: "info", Short: "Show current RGB state", Run: func(cmd *cobra.Command, args []string) { proto, _, err := OpenDevice() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } defer proto.Close() if err := proto.EnableHandshake(); err != nil { fmt.Fprintf(os.Stderr, "Handshake failed: %v\n", err) os.Exit(1) } var state rgb.State resp, err := proto.GetValue(via.RGBLight, uint8(rgb.Brightness)) if err == nil && len(resp) >= 5 { state.Brightness = uint8(resp[4]) } resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Speed)) if err == nil && len(resp) >= 5 { state.Speed = uint8(resp[4]) } resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Mode)) if err == nil && len(resp) >= 5 { state.Mode = rgb.Effect(resp[4]) } resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Enable)) if err == nil && len(resp) >= 5 { state.Enabled = resp[4] != 0 } out := struct { Enabled bool `json:"enabled"` Mode string `json:"mode"` Brightness uint8 `json:"brightness"` Speed uint8 `json:"speed"` }{ Enabled: state.Enabled, Mode: state.Mode.String(), Brightness: state.Brightness, Speed: state.Speed, } data, _ := json.MarshalIndent(out, "", " ") fmt.Println(string(data)) }, } }