initial commit

This commit is contained in:
2024-01-18 00:03:13 -06:00
commit 8654ded7a0
34 changed files with 2650 additions and 0 deletions

112
internal/app/app.go Normal file
View File

@@ -0,0 +1,112 @@
package app
import (
"os"
"git.brettb.xyz/goinv/client/internal/api"
"git.brettb.xyz/goinv/client/internal/ui/assets"
"git.brettb.xyz/goinv/client/internal/ui/help"
"git.brettb.xyz/goinv/client/internal/ui/utils"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"go.uber.org/zap"
)
type App struct {
*tview.Application
APIClient *api.APIClient
pages *tview.Pages
logger *zap.Logger
help *help.Help
assets *assets.Assets
menu *tview.TextView
currentPage string
needInitUI bool
}
func NewApp(name, version, host string, logger *zap.Logger) *App {
sugar := logger.Sugar()
sugar.Debug("creating app")
app := App{
Application: tview.NewApplication(),
APIClient: api.NewAPIClient(host),
pages: tview.NewPages(),
logger: logger,
needInitUI: false,
}
app.assets = assets.NewAssets(logger, app.APIClient)
app.help = help.NewHelp(name, version)
menuItems := [][]string{
{utils.HelpScreenKey.Label(), app.help.GetTitle()},
{utils.AssetsScreenKey.Label(), app.assets.GetTitle()},
}
app.menu = makeMenu(menuItems)
app.pages.AddPage(app.help.GetTitle(), app.help, true, false)
app.pages.AddPage(app.assets.GetTitle(), app.assets, true, false)
return &app
}
func (app *App) Run() error {
app.logger.Info("starting app")
flex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(app.pages, 0, 1, false).
AddItem(app.menu, 1, 1, false)
app.initUI()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == utils.AppExitKey.Key {
app.logger.Info("stopping app")
app.Stop()
os.Exit(0)
}
if !app.frontScreenHasActiveDialog() {
event = utils.ParseKeyEventKey(app.logger, event)
// Next & Previous Screen
switch event.Rune() {
case utils.NextScreenKey.Rune():
app.switchToNextScreen()
return nil
case utils.PreviousScreenKey.Rune():
app.switchToPreviousScreen()
}
// Normal page key switch
switch event.Key() {
case utils.HelpScreenKey.EventKey():
app.switchToScreen(app.help.GetTitle())
return nil
case utils.AssetsScreenKey.EventKey():
app.switchToScreen(app.assets.GetTitle())
return nil
}
}
return event
})
app.currentPage = app.assets.GetTitle()
app.pages.SwitchToPage(app.currentPage)
// start the refresh loop
go app.refresh()
if err := app.SetRoot(flex, true).SetFocus(app.assets).EnableMouse(false).Run(); err != nil {
return err
}
return nil
}

8
internal/app/init.go Normal file
View File

@@ -0,0 +1,8 @@
package app
func (app *App) initUI() {
// Assets page
app.assets.UpdateShelfData()
app.assets.UpdateAssetData()
}

44
internal/app/menu.go Normal file
View File

@@ -0,0 +1,44 @@
package app
import (
"fmt"
"strings"
"git.brettb.xyz/goinv/client/internal/ui/style"
"github.com/rivo/tview"
)
func makeMenu(menuItems [][]string) *tview.TextView {
menu := tview.NewTextView().
SetDynamicColors(true).
SetWrap(true).
SetTextAlign(tview.AlignCenter)
menu.SetBackgroundColor(style.BgColor)
var menuList []string
for i, v := range menuItems {
key, item := genMenuItem(v)
if i == len(menuItems)-1 {
item += " "
}
menuList = append(menuList, key+item)
}
fmt.Fprintf(menu, "%s", strings.Join(menuList, " "))
return menu
}
func genMenuItem(items []string) (string, string) {
key := fmt.Sprintf("[%s::b] <%s>[-:-:-]", style.GetColorHex(style.MenuBgColor), items[0])
desc := fmt.Sprintf("[%s:%s:b] %s [-:-:-]",
style.GetColorHex(style.MenuFgColor),
style.GetColorHex(style.MenuBgColor),
strings.ToUpper(items[1]))
return key, desc
}

19
internal/app/refresh.go Normal file
View File

@@ -0,0 +1,19 @@
package app
import (
"time"
"git.brettb.xyz/goinv/client/internal/ui/utils"
)
func (app *App) refresh() {
app.logger.Sugar().Debugf("starting app refresh loop (interval=%v)", utils.RefreshInterval)
tick := time.NewTicker(utils.RefreshInterval)
for {
<-tick.C
app.Application.Draw()
}
}

56
internal/app/screens.go Normal file
View File

@@ -0,0 +1,56 @@
package app
func (app *App) switchToScreen(name string) {
app.logger.Sugar().Debugf("switching to %s screen", name)
app.pages.SwitchToPage(name)
app.setPageFocus(name)
app.updatePageData(name)
app.currentPage = name
}
func (app *App) frontScreenHasActiveDialog() bool {
switch app.currentPage {
case app.assets.GetTitle():
return app.assets.SubDialogHasFocus()
}
return false
}
func (app *App) switchToPreviousScreen() {
var previousScreen string
switch app.currentPage {
case app.help.GetTitle():
previousScreen = app.assets.GetTitle()
case app.assets.GetTitle():
previousScreen = app.help.GetTitle()
}
app.switchToScreen(previousScreen)
}
func (app *App) switchToNextScreen() {
var nextScreen string
switch app.currentPage {
case app.help.GetTitle():
nextScreen = app.assets.GetTitle()
case app.assets.GetTitle():
nextScreen = app.help.GetTitle()
}
app.switchToScreen(nextScreen)
}
func (app *App) setPageFocus(page string) {
switch page {
case app.help.GetTitle():
app.Application.SetFocus(app.help)
case app.assets.GetTitle():
app.Application.SetFocus(app.assets)
}
}
func (app *App) updatePageData(page string) {
switch page {
case app.assets.GetTitle():
app.assets.UpdateData()
}
}