diff --git a/src/cmd/linkcontext.go b/src/cmd/linkcontext.go new file mode 100644 index 0000000..6b1c09c --- /dev/null +++ b/src/cmd/linkcontext.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "fmt" + "os" + + "gospt/src/commands" + + "github.com/spf13/cobra" +) + +// linkCmd represents the link command +var linkContextCmd = &cobra.Command{ + Use: "linkcontext", + Aliases: []string{"lc"}, + Short: "Get url to current context(album, playlist)", + Long: `Get url to current context(album, playlist)`, + Run: func(cmd *cobra.Command, args []string) { + link, err := commands.LinkContext(ctx, client) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Print(link) + }, +} + +func init() { + rootCmd.AddCommand(linkContextCmd) +} diff --git a/src/commands/commands.go b/src/commands/commands.go index 3a0edf1..89717f8 100644 --- a/src/commands/commands.go +++ b/src/commands/commands.go @@ -797,6 +797,14 @@ func Link(ctx *gctx.Context, client *spotify.Client) (string, error) { return state.Item.ExternalURLs["spotify"], nil } +func LinkContext(ctx *gctx.Context, client *spotify.Client) (string, error) { + state, err := client.PlayerState(ctx) + if err != nil { + return "", err + } + return string(state.PlaybackContext.ExternalURLs["spotify"]), nil +} + func NowPlaying(ctx *gctx.Context, client *spotify.Client) error { current, err := client.PlayerCurrentlyPlaying(ctx) if err != nil { diff --git a/src/tui/main.go b/src/tui/main.go index a5819fa..99aff16 100644 --- a/src/tui/main.go +++ b/src/tui/main.go @@ -6,6 +6,7 @@ import ( "gospt/src/gctx" + "github.com/atotto/clipboard" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/textinput" @@ -192,6 +193,38 @@ func (m *mainModel) GoBack() (tea.Cmd, error) { return nil, nil } +type SpotifyUrl struct { + ExternalURLs map[string]string +} + +func (m *mainModel) CopyToClipboard() error { + item := m.list.SelectedItem().(mainItem).SpotifyItem + m.list.NewStatusMessage("Copying link to " + item.(mainItem).Title()) + switch item.(type) { + case spotify.SimplePlaylist: + clipboard.WriteAll(item.(spotify.SimplePlaylist).ExternalURLs["spotify"]) + case *spotify.FullPlaylist: + clipboard.WriteAll(item.(*spotify.FullPlaylist).ExternalURLs["spotify"]) + case spotify.SimpleAlbum: + clipboard.WriteAll(item.(spotify.SimpleAlbum).ExternalURLs["spotify"]) + case *spotify.FullAlbum: + clipboard.WriteAll(item.(*spotify.FullAlbum).ExternalURLs["spotify"]) + case spotify.SimpleArtist: + clipboard.WriteAll(item.(spotify.SimpleArtist).ExternalURLs["spotify"]) + case *spotify.FullArtist: + clipboard.WriteAll(item.(*spotify.FullArtist).ExternalURLs["spotify"]) + case spotify.SimpleTrack: + clipboard.WriteAll(item.(spotify.SimpleTrack).ExternalURLs["spotify"]) + case spotify.PlaylistTrack: + clipboard.WriteAll(item.(spotify.PlaylistTrack).Track.ExternalURLs["spotify"]) + case spotify.SavedTrack: + clipboard.WriteAll(item.(spotify.SavedTrack).ExternalURLs["spotify"]) + case spotify.FullTrack: + clipboard.WriteAll(item.(spotify.FullTrack).ExternalURLs["spotify"]) + } + return nil +} + func (m *mainModel) SelectItem() error { switch m.mode { case Search: @@ -457,6 +490,12 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.String() == "ctrl+c" { return m, tea.Quit } + if msg.String() == "c" { + err := m.CopyToClipboard() + if err != nil { + return m, tea.Quit + } + } if msg.String() == ">" { go HandleSeek(m.ctx, m.client, true) } @@ -579,6 +618,7 @@ func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model, key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "search")), key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")), key.NewBinding(key.WithKeys("ctrl"+"r"), key.WithHelp("ctrl+r", "start radio")), + key.NewBinding(key.WithKeys("ctrl"+"shift"+"c"), key.WithHelp("ctrl+shift+c", "copy url")), key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "select device")), } } @@ -592,6 +632,7 @@ func InitMain(ctx *gctx.Context, client *spotify.Client, mode Mode) (tea.Model, key.NewBinding(key.WithKeys("-"), key.WithHelp("-", "volume down")), key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")), key.NewBinding(key.WithKeys("ctrl"+"r"), key.WithHelp("ctrl+r", "start radio")), + key.NewBinding(key.WithKeys("ctrl"+"shift"+"c"), key.WithHelp("ctrl+shift+c", "copy url")), key.NewBinding(key.WithKeys("d"), key.WithHelp("d", "select device")), } } diff --git a/src/tui/views.go b/src/tui/views.go index a5968c3..895e0ab 100644 --- a/src/tui/views.go +++ b/src/tui/views.go @@ -36,11 +36,12 @@ func PlaylistView(ctx *gctx.Context, client *spotify.Client, playlist spotify.Si } for _, track := range tracks.Tracks { items = append(items, mainItem{ - Name: track.Track.Name, - Artist: track.Track.Artists[0], - Duration: track.Track.TimeDuration().Round(time.Second).String(), - ID: track.Track.ID, - Desc: track.Track.Artists[0].Name + " - " + track.Track.TimeDuration().Round(time.Second).String(), + Name: track.Track.Name, + Artist: track.Track.Artists[0], + Duration: track.Track.TimeDuration().Round(time.Second).String(), + ID: track.Track.ID, + Desc: track.Track.Artists[0].Name + " - " + track.Track.TimeDuration().Round(time.Second).String(), + SpotifyItem: track, }) } return items, nil