Skip to content

Commit

Permalink
autocomplete playlist names
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed Oct 8, 2021
1 parent 5880d1c commit 9a48839
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions internal/p/p.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"spotify/internal"
"spotify/internal/pause"
"spotify/internal/play"
"spotify/internal/playlist"
"strings"
)

Expand Down Expand Up @@ -49,6 +50,8 @@ func NewCommand() *cobra.Command {
cmd.Flags().String("playlist", "", "playlist name from 'spotify playlist list'")
cmd.Flags().String("album", "", "album name")

_ = cmd.RegisterFlagCompletionFunc("playlist", playlist.AutocompletePlaylist)

return cmd
}

Expand Down
4 changes: 1 addition & 3 deletions internal/pause/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func NewCommand() *cobra.Command {
cmd := &cobra.Command{
return &cobra.Command{
Use: "pause",
Short: "pause music",
RunE: func(cmd *cobra.Command, _ []string) error {
Expand All @@ -28,8 +28,6 @@ func NewCommand() *cobra.Command {
return nil
},
}

return cmd
}

func Pause(api internal.APIInterface) (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package play
import (
"errors"
"spotify/internal"
"spotify/internal/playlist"
"spotify/internal/status"
"strings"

Expand Down Expand Up @@ -49,6 +50,8 @@ func NewCommand() *cobra.Command {
cmd.Flags().String("playlist", "", "playlist name from 'spotify playlist list'")
cmd.Flags().String("album", "", "album name that you wish to play")

_ = cmd.RegisterFlagCompletionFunc("playlist", playlist.AutocompletePlaylist)

return cmd
}

Expand Down
25 changes: 22 additions & 3 deletions internal/playlist/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

func NewShowCommand() *cobra.Command {
return &cobra.Command{
Use: "show [playlist]",
Short: "show artist and songs",
Args: cobra.MinimumNArgs(1),
Use: "show [playlist]",
Short: "show artist and songs",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: AutocompletePlaylist,
RunE: func(cmd *cobra.Command, args []string) error {
api, err := internal.Authenticate()
if err != nil {
Expand Down Expand Up @@ -70,3 +71,21 @@ func formatPlaylist(playlist *spotify.Playlist) (string, error) {

return output.String(), nil
}

func AutocompletePlaylist(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
api, err := internal.Authenticate()
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}

playlists, err := api.GetPlaylists()
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}

var completions []string
for _, playlist := range playlists {
completions = append(completions, playlist.Name)
}
return completions, cobra.ShellCompDirectiveDefault
}

0 comments on commit 9a48839

Please sign in to comment.