info.go 1.6 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. getValue := func(param uint8) (uint8, error) {
  22. value, err := proto.GetValue(via.RGBLight, param)
  23. if err != nil {
  24. return 0, err
  25. }
  26. if len(value) == 0 {
  27. return 0, fmt.Errorf("empty value response")
  28. }
  29. return value[0], nil
  30. }
  31. brightness, err := getValue(uint8(rgb.Brightness))
  32. if err != nil {
  33. fmt.Fprintf(os.Stderr, "Error getting brightness: %v\n", err)
  34. os.Exit(1)
  35. }
  36. speed, err := getValue(uint8(rgb.Speed))
  37. if err != nil {
  38. fmt.Fprintf(os.Stderr, "Error getting speed: %v\n", err)
  39. os.Exit(1)
  40. }
  41. mode, err := getValue(uint8(rgb.EffectID))
  42. if err != nil {
  43. fmt.Fprintf(os.Stderr, "Error getting mode: %v\n", err)
  44. os.Exit(1)
  45. }
  46. out := struct {
  47. Enabled bool `json:"enabled"`
  48. Mode string `json:"mode"`
  49. Brightness uint8 `json:"brightness"`
  50. Speed uint8 `json:"speed"`
  51. }{
  52. Enabled: lightingEnabled(mode, brightness),
  53. Mode: rgb.Effect(mode).String(),
  54. Brightness: brightness,
  55. Speed: speed,
  56. }
  57. data, _ := json.MarshalIndent(out, "", " ")
  58. fmt.Println(string(data))
  59. },
  60. }
  61. }