From f8f5ec9f3b37464094b5264954a60c551304257a Mon Sep 17 00:00:00 2001 From: Simon Garrelou Date: Tue, 6 Dec 2022 19:26:34 +0100 Subject: change focus w/ left-right, quit w/ q, F1-F3 --- src/app.go | 32 ++++++++++++++++++++++++++++---- src/page_artists.go | 19 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/app.go b/src/app.go index 58fc0db..c7e38a6 100644 --- a/src/app.go +++ b/src/app.go @@ -5,6 +5,7 @@ import ( "os" "github.com/delucks/go-subsonic" + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) @@ -38,7 +39,7 @@ func Run(cfg *Config) { SetChangedFunc(func() { a.tv.Draw() }). - SetHighlightedFunc(func(added, removed, remaining []string) { + SetHighlightedFunc(func(added, _, _ []string) { hl := added[0] cur, _ := a.pages.GetFrontPage() @@ -70,6 +71,28 @@ func Run(cfg *Config) { } } + // Keyboard shortcuts + a.tv.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyF1: + switchToPage(a, "artists") + return nil + case tcell.KeyF2: + switchToPage(a, "playlists") + return nil + case tcell.KeyF3: + switchToPage(a, "config") + return nil + } + + switch event.Rune() { + case 'q': + a.tv.Stop() + } + + return event + }) + if err := a.tv.SetRoot(mainLayout, true).EnableMouse(true).SetFocus(mainLayout).Run(); err != nil { fmt.Printf("Error running termsonic: %v", err) os.Exit(1) @@ -77,13 +100,14 @@ func Run(cfg *Config) { } func switchToPage(a *app, name string) { - if name == "artists" { + switch name { + case "artists": a.pages.SwitchToPage("artists") a.header.Highlight("artists") - } else if name == "playlists" { + case "playlists": a.pages.SwitchToPage("playlists") a.header.Highlight("playlists") - } else if name == "config" { + case "config": a.pages.SwitchToPage("config") a.header.Highlight("config") } diff --git a/src/page_artists.go b/src/page_artists.go index c064785..0e1cc72 100644 --- a/src/page_artists.go +++ b/src/page_artists.go @@ -18,6 +18,7 @@ func artistsPage(a *app) tview.Primitive { SetColumns(40, 0). SetBorders(true) + // Artist & album list root := tview.NewTreeNode("Subsonic server").SetColor(tcell.ColorYellow) a.artistsTree = tview.NewTreeView(). SetRoot(root). @@ -38,9 +39,27 @@ func artistsPage(a *app) tview.Primitive { a.tv.SetFocus(a.songsList) }) + // Songs list for the selected album a.songsList = tview.NewList() a.songsList.ShowSecondaryText(false) + // Change the left-right keys to switch between the panels + a.artistsTree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { + a.tv.SetFocus(a.songsList) + return nil + } + return event + }) + + a.songsList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { + a.tv.SetFocus(a.artistsTree) + return nil + } + return event + }) + grid.AddItem(a.artistsTree, 0, 0, 1, 1, 0, 0, true) grid.AddItem(a.songsList, 0, 1, 1, 2, 0, 0, false) -- cgit v1.2.3