From 162574aa623e4e39dd3b59896e78cf09e7809df9 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 10:46:59 -0800 Subject: [PATCH] better --- src/components/cli/cli.go | 103 ++++++++++++++++++++++++++--- src/components/commands/devices.go | 25 +++++++ src/components/commands/link.go | 9 +++ src/components/commands/volume.go | 35 ++++++++++ 4 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 src/components/commands/devices.go create mode 100644 src/components/commands/volume.go diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index 57ea3d6..652c78b 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -3,6 +3,7 @@ package cli import ( "log/slog" "os" + "strconv" "github.com/urfave/cli/v2" "go.uber.org/fx" @@ -55,6 +56,14 @@ func Run(c *commands.Commander, s fx.Shutdowner) { return c.PrintLink() }, }, + { + Name: "linkcontext", + Aliases: []string{"lc"}, + Usage: "Prints the current album or playlist", + Action: func(cCtx *cli.Context) error { + return c.PrintLinkContext() + }, + }, { Name: "next", Aliases: []string{"n", "skip"}, @@ -96,19 +105,76 @@ func Run(c *commands.Commander, s fx.Shutdowner) { }, }, { - Name: "download_cover", - Usage: "Downloads the cover of the current song", - Aliases: []string{"dl"}, - Flags: []cli.Flag{ - &cli.PathFlag{ - Name: "path", - Aliases: []string{"p"}, - Usage: "Path to save the cover", - DefaultText: "./cover.png", + Name: "volume", + Aliases: []string{"v"}, + Usage: "Control the volume", + Subcommands: []*cli.Command{ + { + Name: "up", + Usage: "Increase the volume", + Args: true, + ArgsUsage: "percent", + Action: func(cCtx *cli.Context) error { + amt, err := strconv.Atoi(cCtx.Args().First()) + if err != nil { + return err + } + return c.ChangeVolume(amt) + }, + }, + { + Name: "down", + Aliases: []string{"dn"}, + Usage: "Decrease the volume", + Args: true, + ArgsUsage: "percent", + Action: func(cCtx *cli.Context) error { + amt, err := strconv.Atoi(cCtx.Args().First()) + if err != nil { + return err + } + return c.ChangeVolume(-amt) + }, + }, + { + Name: "mute", + Aliases: []string{"m"}, + Usage: "Mute", + Action: func(cCtx *cli.Context) error { + return c.Mute() + }, + }, + { + Name: "unmute", + Aliases: []string{"um"}, + Usage: "Unmute", + Action: func(cCtx *cli.Context) error { + return c.UnMute() + }, + }, + { + Name: "togglemute", + Aliases: []string{"tm"}, + Usage: "Toggle mute", + Action: func(cCtx *cli.Context) error { + return c.ToggleMute() + }, }, }, + }, + { + Name: "download_cover", + Usage: "Downloads the cover of the current song", + Aliases: []string{"dl"}, + Args: true, + ArgsUsage: "path", + BashComplete: func(cCtx *cli.Context) { + if cCtx.NArg() > 0 { + return + } + }, Action: func(cCtx *cli.Context) error { - return c.DownloadCover(cCtx.Path("path")) + return c.DownloadCover(cCtx.Args().First()) }, }, { @@ -119,9 +185,24 @@ func Run(c *commands.Commander, s fx.Shutdowner) { return c.Radio() }, }, + { + Name: "clearradio", + Usage: "Clears the radio queue", + Aliases: []string{"cr"}, + Action: func(ctx *cli.Context) error { + return c.ClearRadio() + }, + }, + { + Name: "devices", + Usage: "Lists available devices", + Aliases: []string{"d"}, + Action: func(ctx *cli.Context) error { + return c.ListDevices() + }, + }, }, } - app.Suggest = true if err := app.Run(os.Args); err != nil { slog.Error("COMMANDER", "run error", err) os.Exit(1) diff --git a/src/components/commands/devices.go b/src/components/commands/devices.go new file mode 100644 index 0000000..7db729a --- /dev/null +++ b/src/components/commands/devices.go @@ -0,0 +1,25 @@ +package commands + +import ( + "encoding/json" + "fmt" + + "github.com/zmb3/spotify/v2" +) + +func (c *Commander) ListDevices() error { + devices, err := c.Client.PlayerDevices(c.Context) + if err != nil { + return err + } + return PrintDevices(devices) +} + +func PrintDevices(devices []spotify.PlayerDevice) error { + out, err := json.MarshalIndent(devices, "", " ") + if err != nil { + return err + } + fmt.Println(string(out)) + return nil +} diff --git a/src/components/commands/link.go b/src/components/commands/link.go index e3353e3..b8eceec 100644 --- a/src/components/commands/link.go +++ b/src/components/commands/link.go @@ -10,3 +10,12 @@ func (c *Commander) PrintLink() error { fmt.Println(state.Item.ExternalURLs["spotify"]) return nil } + +func (c *Commander) PrintLinkContext() error { + state, err := c.Client.PlayerState(c.Context) + if err != nil { + return err + } + fmt.Println(state.PlaybackContext.ExternalURLs["spotify"]) + return nil +} diff --git a/src/components/commands/volume.go b/src/components/commands/volume.go new file mode 100644 index 0000000..420aa27 --- /dev/null +++ b/src/components/commands/volume.go @@ -0,0 +1,35 @@ +package commands + +func (c *Commander) ChangeVolume(amount int) error { + state, err := c.Client.PlayerState(c.Context) + if err != nil { + return err + } + newVolume := state.Device.Volume + amount + if newVolume > 100 { + newVolume = 100 + } + if newVolume < 0 { + newVolume = 0 + } + return c.Client.Volume(c.Context, newVolume) +} + +func (c *Commander) Mute() error { + return c.ChangeVolume(-100) +} + +func (c *Commander) UnMute() error { + return c.ChangeVolume(100) +} + +func (c *Commander) ToggleMute() error { + state, err := c.Client.PlayerState(c.Context) + if err != nil { + return err + } + if state.Device.Volume == 0 { + return c.ChangeVolume(100) + } + return c.ChangeVolume(-100) +}