2024-01-16 15:21:35 +00:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/client"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/config"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/playback"
|
|
|
|
"git.dayanhub.com/sagi/subsonic-tui/internal/tui/views"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TUI struct {
|
|
|
|
app *tview.Application
|
|
|
|
layout views.View
|
|
|
|
|
|
|
|
client *client.Client
|
|
|
|
playbackCtl *playback.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogin() *TUI {
|
|
|
|
app := tview.NewApplication()
|
|
|
|
layout := views.NewLoginView(func(u, p, url string) {
|
|
|
|
c := client.NewClient(url)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
err := c.Authenticate(u, p)
|
|
|
|
if err != nil {
|
|
|
|
app.Stop()
|
|
|
|
fmt.Printf("[Error] Failed to login. Aborting %e", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
config.SetURL(url)
|
|
|
|
config.SetUsername(u)
|
|
|
|
config.SetPassword(p)
|
|
|
|
config.SaveConfig()
|
|
|
|
app.Stop()
|
|
|
|
}, func() {
|
|
|
|
app.Stop()
|
|
|
|
os.Exit(0)
|
|
|
|
})
|
|
|
|
app.EnableMouse(true).
|
|
|
|
SetRoot(layout.GetView(), true)
|
|
|
|
|
|
|
|
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if event.Key() == tcell.KeyEsc {
|
|
|
|
app.Stop()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return event
|
|
|
|
})
|
|
|
|
|
|
|
|
return &TUI{
|
|
|
|
app: app,
|
|
|
|
layout: layout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlayer(client *client.Client, playbackCtl *playback.Controller) *TUI {
|
|
|
|
app := tview.NewApplication()
|
|
|
|
|
|
|
|
layout := views.NewLayout(client, playbackCtl, func() {
|
|
|
|
go app.Draw()
|
|
|
|
})
|
|
|
|
help := views.NewHelp()
|
|
|
|
|
|
|
|
pages := tview.NewPages()
|
|
|
|
pages.AddPage("app", layout.GetView(), true, true)
|
|
|
|
pages.AddPage("help", help.GetView(), true, false)
|
|
|
|
|
|
|
|
help.SetKeyPressedFunc(func() {
|
|
|
|
pages.SwitchToPage("app")
|
|
|
|
})
|
|
|
|
app.EnableMouse(true).
|
|
|
|
SetRoot(pages, true)
|
|
|
|
|
|
|
|
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if currentPage, _ := pages.GetFrontPage(); currentPage == "help" {
|
|
|
|
pages.SwitchToPage("app")
|
|
|
|
help.GetView().Blur()
|
|
|
|
layout.GetView().Focus(nil)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if layout.Mode() == views.StatusModeSearch {
|
|
|
|
return event
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
|
|
|
switch event.Rune() {
|
|
|
|
case 'q':
|
2024-01-16 15:21:35 +00:00
|
|
|
app.Stop()
|
|
|
|
fmt.Println("Exiting..")
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return nil
|
2024-12-18 15:29:16 +00:00
|
|
|
case 'h':
|
2024-01-16 15:21:35 +00:00
|
|
|
return tcell.NewEventKey(tcell.KeyLeft, rune(tcell.KeyLeft), event.Modifiers())
|
2024-12-18 15:29:16 +00:00
|
|
|
case 'j':
|
2024-01-16 15:21:35 +00:00
|
|
|
return tcell.NewEventKey(tcell.KeyDown, rune(tcell.KeyDown), event.Modifiers())
|
2024-12-18 15:29:16 +00:00
|
|
|
case 'k':
|
2024-01-16 15:21:35 +00:00
|
|
|
return tcell.NewEventKey(tcell.KeyUp, rune(tcell.KeyUp), event.Modifiers())
|
2024-12-18 15:29:16 +00:00
|
|
|
case 'l':
|
2024-01-16 15:21:35 +00:00
|
|
|
return tcell.NewEventKey(tcell.KeyRight, rune(tcell.KeyRight), event.Modifiers())
|
2024-12-18 15:29:16 +00:00
|
|
|
case '?':
|
2024-01-16 15:21:35 +00:00
|
|
|
pages.SwitchToPage("help")
|
|
|
|
layout.GetView().Blur()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
go app.Draw()
|
2024-12-18 15:29:16 +00:00
|
|
|
default:
|
|
|
|
return event
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return event
|
|
|
|
})
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
app.SetAfterDrawFunc(func(screen tcell.Screen) {
|
|
|
|
layout.Update()
|
|
|
|
})
|
|
|
|
app.GetFocus().Blur()
|
|
|
|
|
2024-12-18 15:29:16 +00:00
|
|
|
// app.SetFocus(layout.GetView())
|
2024-01-16 15:21:35 +00:00
|
|
|
|
|
|
|
return &TUI{
|
|
|
|
app: app,
|
|
|
|
layout: layout,
|
|
|
|
client: client,
|
|
|
|
playbackCtl: playbackCtl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUI) Run() error {
|
|
|
|
return t.app.Run()
|
|
|
|
}
|