brightness.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func NewBrightnessCmd() *cobra.Command {
  7. return &cobra.Command{
  8. Use: "brightness <val>",
  9. Short: "Set RGB brightness",
  10. Long: "Set the RGB brightness value (0-255) and read it back, so a value the\n" +
  11. "keyboard clamps or rescales is reported instead of silently applied.",
  12. Args: cobra.ExactArgs(1),
  13. RunE: func(cmd *cobra.Command, args []string) error {
  14. val, err := ParseUint8(args[0])
  15. if err != nil {
  16. return err
  17. }
  18. zones, err := selectedZones()
  19. if err != nil {
  20. return err
  21. }
  22. proto, err := openRGBProtocol()
  23. if err != nil {
  24. return err
  25. }
  26. defer proto.Close()
  27. results, err := setBrightnessVerified(proto, zones, val)
  28. if err != nil {
  29. return fmt.Errorf("set brightness: %w", err)
  30. }
  31. // Claiming "set to N" when the keyboard holds something else is
  32. // the contradiction this read-back exists to prevent.
  33. if anyMismatch(results) {
  34. fmt.Fprintln(cmd.OutOrStdout(), formatResults("Brightness", results))
  35. return nil
  36. }
  37. fmt.Fprintf(cmd.OutOrStdout(), "Brightness set to %d\n", val)
  38. return nil
  39. },
  40. }
  41. }