mode.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func NewModeCmd() *cobra.Command {
  7. return &cobra.Command{
  8. Use: "mode <index>",
  9. Short: "Set effect mode by index",
  10. Long: "Set the effect mode by numeric index (0-255). The index is the\n" +
  11. "channel's own: 5 is light on rgblight and rainbow_beacon on rgb_matrix.\n" +
  12. "Use `effect <name>` where a catalog exists, which is unambiguous.\n" +
  13. "The index is read back, because a board that does not implement it\n" +
  14. "clamps it to the highest it does instead of refusing it.",
  15. Args: cobra.ExactArgs(1),
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. val, err := ParseUint8(args[0])
  18. if err != nil {
  19. return err
  20. }
  21. proto, target, channels, err := openTarget()
  22. if err != nil {
  23. return err
  24. }
  25. defer proto.Close()
  26. results, err := setModeVerified(proto, channels, target.Display, val)
  27. if err != nil {
  28. return fmt.Errorf("set mode: %w", err)
  29. }
  30. // Claiming an index the keyboard clamped away is the same
  31. // contradiction the other verified sets avoid.
  32. if anyMismatch(results) {
  33. fmt.Fprintln(cmd.OutOrStdout(), formatResults("Mode", results))
  34. return nil
  35. }
  36. fmt.Fprintf(cmd.OutOrStdout(), "Mode set to index %d\n", val)
  37. return nil
  38. },
  39. }
  40. }