From a26234b38a1c273f6f5ac57f62a83d15a461bcc9 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Mon, 16 Jan 2023 21:02:23 -0800 Subject: [PATCH] fancy next --- src/cmd/next.go | 15 ++++++++++++-- src/commands/commands.go | 45 ++++++++++++++++++++++++++++++++++++++-- src/tui/handlers.go | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/cmd/next.go b/src/cmd/next.go index 961358b..5b5070a 100644 --- a/src/cmd/next.go +++ b/src/cmd/next.go @@ -1,6 +1,8 @@ package cmd import ( + "strconv" + "gospt/src/commands" "github.com/spf13/cobra" @@ -13,9 +15,18 @@ func init() { var nextCmd = &cobra.Command{ Use: "next", Aliases: []string{"n"}, + Args: cobra.MatchAll(cobra.RangeArgs(0, 1)), Short: "Skip to next song", Long: `Skip to next song`, - Run: func(cmd *cobra.Command, args []string) { - commands.Next(ctx, client) + RunE: func(cmd *cobra.Command, args []string) error { + skipAmt := 1 + if len(args) >= 1 { + var err error + skipAmt, err = strconv.Atoi(args[0]) + if err != nil { + return err + } + } + return commands.Next(ctx, client, skipAmt) }, } diff --git a/src/commands/commands.go b/src/commands/commands.go index 0e76d74..de0de78 100644 --- a/src/commands/commands.go +++ b/src/commands/commands.go @@ -544,11 +544,52 @@ func Unlike(ctx *gctx.Context, client *spotify.Client) error { return nil } -func Next(ctx *gctx.Context, client *spotify.Client) error { - err := client.Next(ctx) +func Next(ctx *gctx.Context, client *spotify.Client, amt int) error { + if amt == 1 { + err := client.Next(ctx) + if err != nil { + return err + } + return nil + } + // found := false + // playingIndex := 0 + current, err := client.PlayerCurrentlyPlaying(ctx) if err != nil { return err } + playbackContext := current.PlaybackContext.Type + switch playbackContext { + case "playlist": + found := false + currentTrackIndex := 0 + for !found { + page := 1 + playlist, err := client.GetPlaylistItems(ctx, spotify.ID(strings.Split(string(current.PlaybackContext.URI), ":")[2]), spotify.Limit(50), spotify.Offset((page-1)*50)) + if err != nil { + return err + } + for idx, track := range playlist.Items { + if track.Track.Track.ID == current.Item.ID { + currentTrackIndex = idx + (50 * (page - 1)) + found = true + break + } + } + page++ + } + fmt.Println(currentTrackIndex) + client.PlayOpt(ctx, &spotify.PlayOptions{ + PlaybackContext: ¤t.PlaybackContext.URI, + PlaybackOffset: &spotify.PlaybackOffset{ + Position: currentTrackIndex + amt, + }, + }) + default: + for i := 0; i <= amt; i++ { + client.Next(ctx) + } + } return nil } diff --git a/src/tui/handlers.go b/src/tui/handlers.go index 6b483de..dcf6f36 100644 --- a/src/tui/handlers.go +++ b/src/tui/handlers.go @@ -92,7 +92,7 @@ func HandlePlayTrack(ctx *gctx.Context, client *spotify.Client, track spotify.ID fmt.Println(err.Error()) return } - err = commands.Next(ctx, client) + err = commands.Next(ctx, client, 1) if err != nil { fmt.Println(err.Error()) return