Started working on TUI

This commit is contained in:
2026-01-08 18:56:38 +00:00
parent 8d1e7c962f
commit 54e1f3b424
7 changed files with 350 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
// Package tui
package tui
import (
dom "esdi/t/internal/dom"
"esdi/t/internal/views"
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type TUI struct {
App *tview.Application
Dom *dom.DOM
}
var (
application *TUI
initiated = false
)
func NewTUI() *TUI {
return &TUI{
App: tview.NewApplication(),
Dom: dom.NewDOM(),
}
}
func (t *TUI) Start() error {
// The app starts running here!
//
// Set the event capture for the global app itself here
t.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlC || event.Rune() == 'q' {
t.App.Stop()
return nil
}
return event
})
ctx := &views.UIContext{
Redraw: func() {
t.App.QueueUpdateDraw(func() {})
},
}
// Set the main view
mainUINode, err := views.BuildMainFlex(t.Dom, ctx)
if err != nil {
panic(fmt.Errorf("failed to build main flex - %s", err.Error()))
}
rootNode, err := t.Dom.NewUINode("root", nil, mainUINode)
if err != nil {
panic(fmt.Errorf("failed to create root node - %s", err.Error()))
}
t.Dom.SetRoot(rootNode)
firstFocus := t.Dom.GetElemByID("device-api-list")
if firstFocus == nil {
panic(fmt.Errorf("`list-window` isn't registered"))
}
// Star the app
err = t.App.SetRoot(t.Dom.GetRootElem(), true).SetFocus(firstFocus).Run()
if err != nil {
return err
}
return nil
}
func Init() {
initiated = true
}