66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
type Application struct {
|
|
*tview.Application
|
|
}
|
|
|
|
func newApplication() *Application {
|
|
app := &Application{
|
|
Application: tview.NewApplication(),
|
|
}
|
|
|
|
app.EnableMouse(cfg.Mouse)
|
|
app.SetInputCapture(app.onInputCapture)
|
|
return app
|
|
}
|
|
|
|
func (app *Application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
|
|
switch event.Name() {
|
|
case cfg.Keys.Quit:
|
|
app.Stop()
|
|
case "Ctrl+C":
|
|
// https://github.com/rivo/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
|
|
return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
|
|
}
|
|
|
|
return event
|
|
}
|
|
|
|
func (app *Application) Show(token string) error {
|
|
if token == "" {
|
|
loginForm := NewLoginForm(func(token string, err error) {
|
|
if err != nil {
|
|
slog.Error("failed to login", "err", err)
|
|
return
|
|
}
|
|
|
|
if err := app.Show(token); err != nil {
|
|
slog.Error("failed to show app", "err", err)
|
|
}
|
|
})
|
|
app.SetRoot(loginForm, true)
|
|
} else {
|
|
if err := openState(token); err != nil {
|
|
return err
|
|
}
|
|
|
|
app.SetRoot(mainFlex, true)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (app *Application) Run(token string) error {
|
|
if err := app.Show(token); err != nil {
|
|
return err
|
|
}
|
|
|
|
return app.Application.Run()
|
|
}
|