info.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package rgb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/wobkey/rgb/internal/rgb"
  8. "github.com/wobkey/rgb/internal/via"
  9. )
  10. func NewInfoCmd() *cobra.Command {
  11. return &cobra.Command{
  12. Use: "info",
  13. Short: "Show current RGB state",
  14. Run: func(cmd *cobra.Command, args []string) {
  15. proto, _, err := OpenDevice()
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  18. os.Exit(1)
  19. }
  20. defer proto.Close()
  21. if err := proto.EnableHandshake(); err != nil {
  22. fmt.Fprintf(os.Stderr, "Handshake failed: %v\n", err)
  23. os.Exit(1)
  24. }
  25. var state rgb.State
  26. resp, err := proto.GetValue(via.RGBLight, uint8(rgb.Brightness))
  27. if err == nil && len(resp) >= 5 {
  28. state.Brightness = uint8(resp[4])
  29. }
  30. resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Speed))
  31. if err == nil && len(resp) >= 5 {
  32. state.Speed = uint8(resp[4])
  33. }
  34. resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Mode))
  35. if err == nil && len(resp) >= 5 {
  36. state.Mode = rgb.Effect(resp[4])
  37. }
  38. resp, err = proto.GetValue(via.RGBLight, uint8(rgb.Enable))
  39. if err == nil && len(resp) >= 5 {
  40. state.Enabled = resp[4] != 0
  41. }
  42. out := struct {
  43. Enabled bool `json:"enabled"`
  44. Mode string `json:"mode"`
  45. Brightness uint8 `json:"brightness"`
  46. Speed uint8 `json:"speed"`
  47. }{
  48. Enabled: state.Enabled,
  49. Mode: state.Mode.String(),
  50. Brightness: state.Brightness,
  51. Speed: state.Speed,
  52. }
  53. data, _ := json.MarshalIndent(out, "", " ")
  54. fmt.Println(string(data))
  55. },
  56. }
  57. }