effect.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package rgb
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/spf13/cobra"
  6. intrgb "github.com/wobkey/rgb/internal/rgb"
  7. "github.com/wobkey/rgb/internal/via"
  8. )
  9. func resolveEffectTargets(name string, zones []intrgb.Zone) ([]intrgb.EffectTarget, []intrgb.Zone, error) {
  10. if name == "static" {
  11. name = "solid"
  12. }
  13. return intrgb.ResolveEffect(name, zones)
  14. }
  15. func NewEffectCmd() *cobra.Command {
  16. return &cobra.Command{
  17. Use: "effect <name>",
  18. Short: "Set RGB effect",
  19. Long: "Set the RGB lighting effect on the connected keyboard.",
  20. Args: cobra.ExactArgs(1),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. zones, err := selectedZones()
  23. if err != nil {
  24. return err
  25. }
  26. targets, skipped, err := resolveEffectTargets(args[0], zones)
  27. if err != nil {
  28. return err
  29. }
  30. if len(skipped) > 0 {
  31. skippedNames := make([]string, len(skipped))
  32. for i, zone := range skipped {
  33. skippedNames[i] = string(zone)
  34. }
  35. fmt.Fprintf(cmd.ErrOrStderr(), "Warning: effect not supported on zone(s): %s\n", strings.Join(skippedNames, ", "))
  36. }
  37. proto, err := openRGBProtocol()
  38. if err != nil {
  39. return err
  40. }
  41. defer proto.Close()
  42. for _, target := range targets {
  43. if err := proto.SetValue(via.LEDType(target.Zone.Channel()), uint8(intrgb.EffectID), target.ID); err != nil {
  44. return fmt.Errorf("set effect on %s: %w", target.Zone, err)
  45. }
  46. }
  47. fmt.Fprintf(cmd.OutOrStdout(), "Effect set to %q\n", args[0])
  48. return nil
  49. },
  50. }
  51. }