brightness.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. proto, target, channels, err := openTarget()
  19. if err != nil {
  20. return err
  21. }
  22. defer proto.Close()
  23. results, err := setBrightnessVerified(proto, channels, target.Display, val)
  24. if err != nil {
  25. return fmt.Errorf("set brightness: %w", err)
  26. }
  27. // Claiming "set to N" when the keyboard holds something else is
  28. // the contradiction this read-back exists to prevent.
  29. if anyMismatch(results) {
  30. fmt.Fprintln(cmd.OutOrStdout(), formatResults("Brightness", results))
  31. return nil
  32. }
  33. fmt.Fprintf(cmd.OutOrStdout(), "Brightness set to %d\n", val)
  34. return nil
  35. },
  36. }
  37. }