1package spotify_playlists 2 3import ( 4 "fmt" 5 6 "github.com/ambientsound/visp/list" 7 "github.com/ambientsound/visp/options" 8 "github.com/ambientsound/visp/utils" 9 "github.com/zmb3/spotify" 10) 11 12type List struct { 13 list.Base 14 playlists map[string]spotify.SimplePlaylist 15} 16 17var _ list.List = &List{} 18 19func New(client spotify.Client, source *spotify.SimplePlaylistPage) (*List, error) { 20 var err error 21 22 playlists := make([]spotify.SimplePlaylist, 0, source.Total) 23 24 for err == nil { 25 playlists = append(playlists, source.Playlists...) 26 err = client.NextPage(source) 27 } 28 29 if err != spotify.ErrNoMorePages { 30 return nil, err 31 } 32 33 return NewFromPlaylists(playlists), nil 34} 35 36func NewFromPlaylists(playlists []spotify.SimplePlaylist) *List { 37 this := &List{ 38 playlists: make(map[string]spotify.SimplePlaylist, len(playlists)), 39 } 40 this.Clear() 41 for _, playlist := range playlists { 42 this.playlists[playlist.ID.String()] = playlist 43 this.Add(Row(playlist)) 44 } 45 this.SetVisibleColumns(options.GetList(options.ColumnsPlaylists)) 46 return this 47} 48 49func Row(playlist spotify.SimplePlaylist) list.Row { 50 return list.NewRow(playlist.ID.String(), map[string]string{ 51 "name": playlist.Name, 52 "tracks": fmt.Sprintf("%d", playlist.Tracks.Total), 53 "owner": playlist.Owner.DisplayName, 54 "public": utils.HumanFormatBool(playlist.IsPublic), 55 "collaborative": utils.HumanFormatBool(playlist.Collaborative), 56 }) 57} 58 59// CursorPlaylist returns the playlist currently selected by the cursor. 60func (l *List) CursorPlaylist() *spotify.SimplePlaylist { 61 return l.Playlist(l.Cursor()) 62} 63 64// Song returns the song at a specific index. 65func (l *List) Playlist(index int) *spotify.SimplePlaylist { 66 row := l.Row(index) 67 if row == nil { 68 return nil 69 } 70 playlist := l.playlists[row.ID()] 71 return &playlist 72} 73 74// Selection returns all the selected songs as a new playlist list. 75func (l *List) Selection() List { 76 indices := l.SelectionIndices() 77 playlists := make([]spotify.SimplePlaylist, len(indices)) 78 79 for i, index := range indices { 80 playlists[i] = *l.Playlist(index) 81 } 82 83 return *NewFromPlaylists(playlists) 84} 85 86func (l *List) Playlists() []spotify.SimplePlaylist { 87 tracks := make([]spotify.SimplePlaylist, len(l.playlists)) 88 for i := 0; i < l.Len(); i++ { 89 tracks[i] = *l.Playlist(i) 90 } 91 return tracks 92} 93