mirror of
https://github.com/aditya-K2/gspt.git
synced 2026-01-09 13:58:05 -05:00
Implement defView for mapping actions
This commit is contained in:
18
ui/view.go
18
ui/view.go
@@ -4,15 +4,15 @@ import "github.com/gdamore/tcell/v2"
|
||||
|
||||
var (
|
||||
CurrentView View
|
||||
playlistView = &PlaylistView{}
|
||||
albumView = &AlbumView{}
|
||||
albumsView = &AlbumsView{}
|
||||
likedSongsView = &LikedSongsView{}
|
||||
recentlyPlayedView = &RecentlyPlayedView{}
|
||||
topTracksView = &TopTracksView{}
|
||||
artistView = &ArtistView{}
|
||||
artistsView = &ArtistsView{}
|
||||
searchView = &SearchView{}
|
||||
playlistView = NewPlaylistView()
|
||||
albumView = NewAlbumView()
|
||||
albumsView = NewAlbumsView()
|
||||
likedSongsView = NewLikedSongsView()
|
||||
recentlyPlayedView = NewRecentlyPlayedView()
|
||||
topTracksView = NewTopTracksView()
|
||||
artistView = NewArtistView()
|
||||
artistsView = NewArtistsView()
|
||||
searchView = NewSearchView()
|
||||
)
|
||||
|
||||
type View interface {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -15,6 +14,14 @@ type AlbumView struct {
|
||||
currentFullAlbum *spt.Album
|
||||
}
|
||||
|
||||
func NewAlbumView() *AlbumView {
|
||||
t := &AlbumView{
|
||||
&DefaultView{&defView{}},
|
||||
nil, "", nil,
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (a *AlbumView) SetAlbum(name string, al *spotify.ID) {
|
||||
a.currentAlbumID = al
|
||||
a.currentAlbumName = name
|
||||
@@ -80,15 +87,10 @@ func (a *AlbumView) ContextHandler() func(start, end, sel int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AlbumView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySongWithContext(&a.currentFullAlbum.URI, r); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
return e
|
||||
func (a *AlbumView) PlaySelectEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySongWithContext(&a.currentFullAlbum.URI, r); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type AlbumsView struct {
|
||||
@@ -10,6 +9,14 @@ type AlbumsView struct {
|
||||
savedAlbums *spt.SavedAlbums
|
||||
}
|
||||
|
||||
func NewAlbumsView() *AlbumsView {
|
||||
a := &AlbumsView{
|
||||
&DefaultViewNone{&defView{}},
|
||||
nil,
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AlbumsView) Content() func() [][]Content {
|
||||
return func() [][]Content {
|
||||
c := make([][]Content, 0)
|
||||
@@ -40,14 +47,10 @@ func (a *AlbumsView) Content() func() [][]Content {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AlbumsView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
albumView.SetAlbum((*a.savedAlbums)[r].Name, &(*a.savedAlbums)[r].ID)
|
||||
SetCurrentView(albumView)
|
||||
}
|
||||
return e
|
||||
}
|
||||
func (a *AlbumsView) OpenAlbum() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
albumView.SetAlbum((*a.savedAlbums)[r].Name, &(*a.savedAlbums)[r].ID)
|
||||
SetCurrentView(albumView)
|
||||
}
|
||||
|
||||
func (a *AlbumsView) Name() string { return "AlbumsView" }
|
||||
|
||||
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -13,6 +12,16 @@ type ArtistView struct {
|
||||
albums []spotify.SimpleAlbum
|
||||
}
|
||||
|
||||
func NewArtistView() *ArtistView {
|
||||
a := &ArtistView{
|
||||
&DefaultViewNone{&defView{}},
|
||||
nil,
|
||||
[]spotify.FullTrack{},
|
||||
[]spotify.SimpleAlbum{},
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *ArtistView) SetArtist(id *spotify.ID) {
|
||||
a.artistID = id
|
||||
}
|
||||
@@ -57,32 +66,28 @@ func (a *ArtistView) Content() func() [][]Content {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ArtistView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyCtrlP {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.albums) + 1) {
|
||||
if err := spt.PlayContext(&a.albums[r-1].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
func (a *ArtistView) PlayArtistAlbum() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.albums) + 1) {
|
||||
if err := spt.PlayContext(&a.albums[r-1].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.albums) + 1) {
|
||||
albumView.SetAlbum(a.albums[r-1].Name, &a.albums[r-1].ID)
|
||||
SetCurrentView(albumView)
|
||||
} else if r != len(a.albums)+1 {
|
||||
if err := spt.PlaySong(a.topTracks[r-2-len(a.albums)].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ArtistView) OpenSelectEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.albums) + 1) {
|
||||
albumView.SetAlbum(a.albums[r-1].Name, &a.albums[r-1].ID)
|
||||
SetCurrentView(albumView)
|
||||
} else if r != len(a.albums)+1 {
|
||||
if err := spt.PlaySong(a.topTracks[r-2-len(a.albums)].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type ArtistsView struct {
|
||||
@@ -10,6 +9,14 @@ type ArtistsView struct {
|
||||
followedArtists *spt.FollowedArtists
|
||||
}
|
||||
|
||||
func NewArtistsView() *ArtistsView {
|
||||
a := &ArtistsView{
|
||||
&DefaultViewNone{&defView{}},
|
||||
nil,
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *ArtistsView) Content() func() [][]Content {
|
||||
return func() [][]Content {
|
||||
c := make([][]Content, 0)
|
||||
@@ -39,16 +46,11 @@ func (a *ArtistsView) Content() func() [][]Content {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ArtistsView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
artistView.SetArtist(&(*a.followedArtists)[r].ID)
|
||||
artistView.RefreshState()
|
||||
SetCurrentView(artistView)
|
||||
}
|
||||
return e
|
||||
}
|
||||
func (a *ArtistsView) OpenArtist() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
artistView.SetArtist(&(*a.followedArtists)[r].ID)
|
||||
artistView.RefreshState()
|
||||
SetCurrentView(artistView)
|
||||
}
|
||||
|
||||
func (a *ArtistsView) Name() string { return "ArtistsView" }
|
||||
|
||||
@@ -1,8 +1,38 @@
|
||||
package ui
|
||||
|
||||
import "github.com/aditya-K2/gspt/spt"
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type defView struct {
|
||||
m map[tcell.Key]string
|
||||
actions map[string]*Action
|
||||
}
|
||||
|
||||
func (d *defView) SetMappings(m map[tcell.Key]string) {
|
||||
d.m = m
|
||||
}
|
||||
|
||||
func (d *defView) SetActions(a map[string]*Action) {
|
||||
d.actions = a
|
||||
}
|
||||
|
||||
func (d *defView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if d.m != nil {
|
||||
if val, ok := d.m[e.Key()]; ok {
|
||||
if d.actions != nil {
|
||||
return d.actions[val].Func()(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
type DefaultViewNone struct {
|
||||
*defView
|
||||
}
|
||||
|
||||
func (a *DefaultViewNone) ContextOpener() func(m *Root, s func(s int)) { return nil }
|
||||
@@ -11,6 +41,7 @@ func (a *DefaultViewNone) ContextKey() rune { return
|
||||
func (a *DefaultViewNone) DisableVisualMode() bool { return true }
|
||||
|
||||
type DefaultView struct {
|
||||
*defView
|
||||
}
|
||||
|
||||
func (d *DefaultView) ContextOpener() func(m *Root, s func(s int)) {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -13,6 +12,14 @@ type LikedSongsView struct {
|
||||
likedSongs *spt.LikedSongs
|
||||
}
|
||||
|
||||
func NewLikedSongsView() *LikedSongsView {
|
||||
l := &LikedSongsView{
|
||||
&DefaultView{&defView{}},
|
||||
nil,
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (p *LikedSongsView) Content() func() [][]Content {
|
||||
return func() [][]Content {
|
||||
c := make([][]Content, 0)
|
||||
@@ -67,15 +74,10 @@ func (l *LikedSongsView) ContextHandler() func(start, end, sel int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LikedSongsView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySong((*l.likedSongs)[r].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
return e
|
||||
func (l *LikedSongsView) PlaySelectEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySong((*l.likedSongs)[r].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -12,7 +11,15 @@ type PlaylistView struct {
|
||||
*DefaultView
|
||||
currentPlaylist *spotify.SimplePlaylist
|
||||
currentUserFullPlaylist *spt.Playlist
|
||||
I *interactiveView
|
||||
}
|
||||
|
||||
func NewPlaylistView() *PlaylistView {
|
||||
p := &PlaylistView{
|
||||
&DefaultView{&defView{}},
|
||||
nil,
|
||||
nil,
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *PlaylistView) SetPlaylist(pl *spotify.SimplePlaylist) {
|
||||
@@ -78,15 +85,10 @@ func (p *PlaylistView) ContextHandler() func(start, end, sel int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PlaylistView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySongWithContext(&p.currentPlaylist.URI, r); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
return e
|
||||
func (p *PlaylistView) PlaySelectEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if err := spt.PlaySongWithContext(&p.currentPlaylist.URI, r); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,14 @@ var (
|
||||
type RecentlyPlayedView struct {
|
||||
*DefaultView
|
||||
recentlyPlayed []spotify.RecentlyPlayedItem
|
||||
funcMap map[tcell.Key]string
|
||||
}
|
||||
|
||||
func NewRecentlyPlayedView() *RecentlyPlayedView {
|
||||
r := &RecentlyPlayedView{
|
||||
&DefaultView{&defView{}},
|
||||
[]spotify.RecentlyPlayedItem{},
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func format(t time.Duration) string {
|
||||
@@ -65,15 +72,6 @@ func (r *RecentlyPlayedView) ContextHandler() func(start, end, sel int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (re *RecentlyPlayedView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if action, ok := re.funcMap[e.Key()]; ok {
|
||||
RecentlyPlayedViewActions[action].Func()(e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RecentlyPlayedView) Name() string { return "RecentlyPlayedView" }
|
||||
|
||||
func (r *RecentlyPlayedView) RefreshState() {
|
||||
@@ -99,7 +97,3 @@ func (re *RecentlyPlayedView) SelectEntry(e *tcell.EventKey) *tcell.EventKey {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (re *RecentlyPlayedView) MapActions(f map[tcell.Key]string) {
|
||||
re.funcMap = f
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -12,6 +11,14 @@ type SearchView struct {
|
||||
results *spotify.SearchResult
|
||||
}
|
||||
|
||||
func NewSearchView() *SearchView {
|
||||
s := &SearchView{
|
||||
&DefaultViewNone{&defView{}},
|
||||
"", nil,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (a *SearchView) Content() func() [][]Content {
|
||||
return func() [][]Content {
|
||||
c := make([][]Content, 0)
|
||||
@@ -69,12 +76,6 @@ func (a *SearchView) RefreshState() {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *SearchView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
func (a *SearchView) SetSearch(s string) {
|
||||
a.search = s
|
||||
a.RefreshState()
|
||||
|
||||
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/aditya-K2/gspt/spt"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/zmb3/spotify/v2"
|
||||
)
|
||||
|
||||
@@ -12,6 +11,15 @@ type TopTracksView struct {
|
||||
topArtists []spotify.FullArtist
|
||||
}
|
||||
|
||||
func NewTopTracksView() *TopTracksView {
|
||||
t := &TopTracksView{
|
||||
&DefaultViewNone{&defView{}},
|
||||
[]spotify.FullTrack{},
|
||||
[]spotify.FullArtist{},
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (a *TopTracksView) RefreshState() {
|
||||
topTracks, err := spt.GetTopTracks()
|
||||
if err != nil {
|
||||
@@ -49,33 +57,30 @@ func (a *TopTracksView) Content() func() [][]Content {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *TopTracksView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.EventKey {
|
||||
return func(e *tcell.EventKey) *tcell.EventKey {
|
||||
if e.Key() == tcell.KeyCtrlP {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.topArtists) + 1) {
|
||||
if err := spt.PlayContext(&a.topArtists[r-1].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
func (a *TopTracksView) PlaySelectedEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.topArtists) + 1) {
|
||||
if err := spt.PlayContext(&a.topArtists[r-1].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
if e.Key() == tcell.KeyEnter {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.topArtists) + 1) {
|
||||
artistView.SetArtist(&(a.topArtists)[r-1].ID)
|
||||
artistView.RefreshState()
|
||||
SetCurrentView(artistView)
|
||||
} else if r != len(a.topArtists)+1 {
|
||||
if err := spt.PlaySong(a.topTracks[r-2-len(a.topArtists)].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (a *TopTracksView) OpenSelectEntry() {
|
||||
r, _ := Ui.Main.Table.GetSelection()
|
||||
if r > 0 {
|
||||
if r < (len(a.topArtists) + 1) {
|
||||
artistView.SetArtist(&(a.topArtists)[r-1].ID)
|
||||
artistView.RefreshState()
|
||||
SetCurrentView(artistView)
|
||||
} else if r != len(a.topArtists)+1 {
|
||||
if err := spt.PlaySong(a.topTracks[r-2-len(a.topArtists)].URI); err != nil {
|
||||
SendNotification(err.Error())
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user