| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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()
- getValue := func(param uint8) (uint8, error) {
- value, err := proto.GetValue(via.RGBLight, param)
- if err != nil {
- return 0, err
- }
- if len(value) == 0 {
- return 0, fmt.Errorf("empty value response")
- }
- return value[0], nil
- }
- brightness, err := getValue(uint8(rgb.Brightness))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error getting brightness: %v\n", err)
- os.Exit(1)
- }
- speed, err := getValue(uint8(rgb.Speed))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error getting speed: %v\n", err)
- os.Exit(1)
- }
- mode, err := getValue(uint8(rgb.EffectID))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error getting mode: %v\n", err)
- os.Exit(1)
- }
- out := struct {
- Enabled bool `json:"enabled"`
- Mode string `json:"mode"`
- Brightness uint8 `json:"brightness"`
- Speed uint8 `json:"speed"`
- }{
- Enabled: lightingEnabled(mode, brightness),
- Mode: rgb.Effect(mode).String(),
- Brightness: brightness,
- Speed: speed,
- }
- data, _ := json.MarshalIndent(out, "", " ")
- fmt.Println(string(data))
- },
- }
- }
|