Embed tview.Table in some structs

This commit is contained in:
aditya-K2
2023-04-30 23:03:49 +05:30
parent 5492f5a2dd
commit 2be33ad27c
14 changed files with 53 additions and 57 deletions

View File

@@ -115,36 +115,36 @@ func NewApplication() *tview.Application {
root = NewRoot()
coverArt = newCoverArt()
Main = NewInteractiveView()
Main.Table.SetBorder(true)
Main.SetBorder(true)
progressBar := NewProgressBar().SetProgressFunc(progressFunc)
searchbar := NewSearchBar()
navMenu := NewNavMenu([]navItem{
{"Albums", NewAction(func(e *tcell.EventKey) *tcell.EventKey {
SetCurrentView(albumsView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil)},
{"Artists", NewAction(func(e *tcell.EventKey) *tcell.EventKey {
SetCurrentView(artistsView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil)},
{"Liked Songs", NewAction(func(e *tcell.EventKey) *tcell.EventKey {
SetCurrentView(likedSongsView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil)},
{"Recently Played", NewAction(func(e *tcell.EventKey) *tcell.EventKey {
recentlyPlayedView.RefreshState()
SetCurrentView(recentlyPlayedView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil)},
})
playlistNav := NewPlaylistNav()
root.AfterContextClose(func() { App.SetFocus(Main.Table) })
root.AfterContextClose(func() { App.SetFocus(Main) })
// Define Actions
openCurrentArtist := func() {
@@ -152,7 +152,7 @@ func NewApplication() *tview.Application {
if len(state.Item.Artists) != 0 {
artistView.SetArtist(&state.Item.Artists[0].ID)
SetCurrentView(artistView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
} else {
SendNotification("No Artist Found!")
}
@@ -162,7 +162,7 @@ func NewApplication() *tview.Application {
if state != nil && state.Item != nil {
albumView.SetAlbum(state.Item.Album.Name, &state.Item.Album.ID)
SetCurrentView(albumView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
}
}
globalActions := map[string]*Action{
@@ -181,15 +181,15 @@ func NewApplication() *tview.Application {
return nil
}, nil),
"focus_nav": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
App.SetFocus(navMenu.Table)
App.SetFocus(navMenu)
return nil
}, nil),
"focus_playlists": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
App.SetFocus(playlistNav.Table)
App.SetFocus(playlistNav)
return nil
}, nil),
"focus_main_view": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil),
"open_current_track_album": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
@@ -225,7 +225,7 @@ func NewApplication() *tview.Application {
}
playlistView.SetPlaylist(&p.SimplePlaylist)
SetCurrentView(playlistView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
}
default:
{
@@ -254,10 +254,10 @@ func NewApplication() *tview.Application {
"play_entry": NewAction(playlistNav.PlaySelectEntry,
progressBar),
"open_entry": NewAction(func(e *tcell.EventKey) *tcell.EventKey {
r, _ := playlistNav.Table.GetSelection()
r, _ := playlistNav.GetSelection()
playlistView.SetPlaylist(&(*playlistNav.Playlists)[r])
SetCurrentView(playlistView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
return nil
}, nil),
}))
@@ -372,9 +372,9 @@ func NewApplication() *tview.Application {
// Map Actions
playlistNav.SetMappings(mappings["playlist_nav"])
playlistNav.Table.SetInputCapture(playlistNav.ExternalInputCapture())
playlistNav.SetInputCapture(playlistNav.ExternalInputCapture())
navMenu.SetMappings(mappings["nav_menu"])
navMenu.Table.SetInputCapture(navMenu.ExternalInputCapture())
navMenu.SetInputCapture(navMenu.ExternalInputCapture())
playlistView.SetMappings(mappings["playlist_view"])
recentlyPlayedView.SetMappings(mappings["recently_played_view"])
topTracksView.SetMappings(mappings["top_tracks_view"])
@@ -387,8 +387,8 @@ func NewApplication() *tview.Application {
// Set up UI
navFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(navMenu.Table, 6, 3, false).
AddItem(playlistNav.Table, 0, 6, false)
AddItem(navMenu, 6, 3, false).
AddItem(playlistNav, 0, 6, false)
if !config.Config.HideImage {
navFlex.AddItem(coverArt, 9, 3, false)
@@ -397,7 +397,7 @@ func NewApplication() *tview.Application {
// mid
mFlex := tview.NewFlex().
AddItem(navFlex, 17, 1, false).
AddItem(Main.Table, 0, 4, false)
AddItem(Main, 0, 4, false)
// mid + top
tFlex := tview.NewFlex().SetDirection(tview.FlexRow).
@@ -409,7 +409,7 @@ func NewApplication() *tview.Application {
AddItem(progressBar, 5, 1, false)
root.Primitive("Main", mainFlex)
App.SetRoot(root.Root, true).SetFocus(Main.Table)
App.SetRoot(root.Root, true).SetFocus(Main)
// Start Routines
InitNotifier()

View File

@@ -25,7 +25,7 @@ type interactiveView struct {
visual bool
vrange *_range
baseSel int
Table *tview.Table
*tview.Table
}
func NewInteractiveView() *interactiveView {

View File

@@ -13,8 +13,8 @@ type navItem struct {
type NavMenu struct {
*defView
Table *tview.Table
m []navItem
*tview.Table
m []navItem
}
func NewNavMenu(m []navItem) *NavMenu {
@@ -46,7 +46,7 @@ func (n *NavMenu) SelectEntry(e *tcell.EventKey) *tcell.EventKey {
type PlaylistNav struct {
*defView
Table *tview.Table
*tview.Table
Playlists *spt.UserPlaylists
c chan bool
done func(error)
@@ -63,22 +63,18 @@ func NewPlaylistNav() *PlaylistNav {
}
}}
T.SetDrawFunc(func(s tcell.Screen, x, y, w, h int) (int, int, int, int) {
v.Draw()
if v.Playlists == nil {
v.RefreshState()
}
for k, p := range *v.Playlists {
v.Table.SetCell(k, 0,
GetCell(p.Name, PlaylistNavStyle))
}
return T.GetInnerRect()
})
return v
}
func (v *PlaylistNav) Draw() {
if v.Playlists == nil {
v.RefreshState()
}
for k, p := range *v.Playlists {
v.Table.SetCell(k, 0,
GetCell(p.Name, PlaylistNavStyle))
}
}
func (v *PlaylistNav) PlaySelectEntry(e *tcell.EventKey) *tcell.EventKey {
r, _ := v.Table.GetSelection()
if err := spt.PlayContext(&(*v.Playlists)[r].URI); err != nil {

View File

@@ -140,7 +140,7 @@ func notify(n *notification) {
n.position = npos
root.Root.AddPage(currentTime, n, false, true)
App.Draw()
App.SetFocus(Main.Table)
App.SetFocus(Main)
if n.msg != nil {
n.text = <-n.msg
App.Draw()
@@ -148,7 +148,7 @@ func notify(n *notification) {
time.Sleep(n.timer)
root.Root.RemovePage(currentTime)
posArr.Free(npos)
App.SetFocus(Main.Table)
App.SetFocus(Main)
App.Draw()
}()
}

View File

@@ -22,14 +22,14 @@ func NewSearchBar() *tview.InputField {
switch k {
case tcell.KeyEscape:
{
App.SetFocus(Main.Table)
App.SetFocus(Main)
T.SetText("")
}
case tcell.KeyEnter:
{
searchView.SetSearch(T.GetText())
SetCurrentView(searchView)
App.SetFocus(Main.Table)
App.SetFocus(Main)
T.SetText("")
}
}

View File

@@ -61,7 +61,7 @@ func (a *AlbumView) Content() func() [][]Content {
}
func (a *AlbumView) AddToPlaylist() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
track := (*(*a.currentFullAlbum).Tracks)[r]
addToPlaylist([]spotify.ID{track.ID})
}
@@ -77,7 +77,7 @@ func (a *AlbumView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *tcel
}
func (a *AlbumView) PlaySelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if err := spt.PlaySongWithContext(&a.currentFullAlbum.URI, r); err != nil {
SendNotification(err.Error())
}

View File

@@ -47,20 +47,20 @@ func (a *AlbumsView) Content() func() [][]Content {
}
func (a *AlbumsView) OpenAlbum() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
albumView.SetAlbum((*a.savedAlbums)[r].Name, &(*a.savedAlbums)[r].ID)
SetCurrentView(albumView)
}
func (a *AlbumsView) PlaySelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if err := spt.PlayContext(&(*a.savedAlbums)[r].URI); err != nil {
SendNotification(err.Error())
}
}
func (a *AlbumsView) QueueSelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
alb := (*a.savedAlbums)[r]
msg := SendNotificationWithChan("Queueing " + alb.Name + "...")
go func() {

View File

@@ -72,7 +72,7 @@ func (a *ArtistView) Content() func() [][]Content {
}
func (a *ArtistView) PlayEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if r > 0 {
if r < (len(a.albums) + 1) {
if err := spt.PlayContext(&a.albums[r-1].URI); err != nil {
@@ -83,7 +83,7 @@ func (a *ArtistView) PlayEntry() {
}
func (a *ArtistView) OpenEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if r > 0 {
if r < (len(a.albums)+1) && len(a.albums) > 0 {
albumView.SetAlbum(a.albums[r-1].Name, &a.albums[r-1].ID)

View File

@@ -46,7 +46,7 @@ func (a *ArtistsView) Content() func() [][]Content {
}
func (a *ArtistsView) OpenArtist() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
artistView.SetArtist(&(*a.followedArtists)[r].ID)
SetCurrentView(artistView)
}

View File

@@ -46,7 +46,7 @@ func (p *LikedSongsView) Content() func() [][]Content {
}
func (l *LikedSongsView) AddToPlaylist() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
addToPlaylist([]spotify.ID{(*l.likedSongs)[r].ID})
}
@@ -60,7 +60,7 @@ func (l *LikedSongsView) AddToPlaylistVisual(start, end int, e *tcell.EventKey)
}
func (l *LikedSongsView) OpenEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if err := spt.PlaySong((*l.likedSongs)[r].URI); err != nil {
SendNotification(err.Error())
}

View File

@@ -60,7 +60,7 @@ func (p *PlaylistView) Content() func() [][]Content {
}
func (p *PlaylistView) AddToPlaylist() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
addToPlaylist([]spotify.ID{(*(*p.currentUserFullPlaylist).Tracks)[r].Track.ID})
}
@@ -75,7 +75,7 @@ func (p *PlaylistView) AddToPlaylistVisual(start, end int, e *tcell.EventKey) *t
}
func (p *PlaylistView) PlaySelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if err := spt.PlaySongWithContext(&p.currentPlaylist.URI, r); err != nil {
SendNotification(err.Error())
}

View File

@@ -41,7 +41,7 @@ func (r *RecentlyPlayedView) Content() func() [][]Content {
}
func (r *RecentlyPlayedView) AddToPlaylist() {
_r, _ := Main.Table.GetSelection()
_r, _ := Main.GetSelection()
addToPlaylist([]spotify.ID{r.recentlyPlayed[_r].Track.ID})
}
@@ -66,7 +66,7 @@ func (r *RecentlyPlayedView) RefreshState() {
}
func (re *RecentlyPlayedView) SelectEntry(e *tcell.EventKey) *tcell.EventKey {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
trackUri := re.recentlyPlayed[r].Track.URI
contextUri := re.recentlyPlayed[r].PlaybackContext.URI
if string(contextUri) != "" {

View File

@@ -102,7 +102,7 @@ func (a *SearchView) Content() func() [][]Content {
}
func (a *SearchView) SelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
switch a.searchContent[r].Type {
case "track":
{
@@ -134,7 +134,7 @@ func (a *SearchView) SelectEntry() {
}
func (a *SearchView) PlaySelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
switch a.searchContent[r].Type {
case "album", "artist", "playlist":
{

View File

@@ -70,7 +70,7 @@ func (a *TopTracksView) Content() func() [][]Content {
}
func (a *TopTracksView) PlaySelectedEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if r > 0 {
if r < (len(a.topArtists) + 1) {
if err := spt.PlayContext(&a.topArtists[r-1].URI); err != nil {
@@ -82,7 +82,7 @@ func (a *TopTracksView) PlaySelectedEntry() {
}
func (a *TopTracksView) OpenSelectEntry() {
r, _ := Main.Table.GetSelection()
r, _ := Main.GetSelection()
if r > 0 {
if r < (len(a.topArtists) + 1) {
artistView.SetArtist(&(a.topArtists)[r-1].ID)