brightness.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. func NewBrightnessCmd() *cobra.Command {
  7. return withZoneArgs(&cobra.Command{
  8. Use: "brightness <zone> <val>",
  9. Short: "Set RGB brightness on a zone",
  10. Long: writesLighting("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. RunE: func(cmd *cobra.Command, args []string) error {
  13. val, err := ParseUint8(args[1])
  14. if err != nil {
  15. return err
  16. }
  17. proto, target, channels, err := openTarget(args[0])
  18. if err != nil {
  19. return err
  20. }
  21. defer proto.Close()
  22. results, err := setBrightnessVerified(proto, channels, target.Display, val)
  23. if err != nil {
  24. return fmt.Errorf("set brightness: %w", err)
  25. }
  26. // Claiming "set to N" when the keyboard holds something else is
  27. // the contradiction this read-back exists to prevent.
  28. if anyMismatch(results) {
  29. fmt.Fprintln(cmd.OutOrStdout(), formatResults("Brightness", results))
  30. return nil
  31. }
  32. fmt.Fprintf(cmd.OutOrStdout(), "Brightness set to %d\n", val)
  33. return nil
  34. },
  35. }, 2, 2, "a zone and a brightness value")
  36. }