Refactor UI state management and improve error handling in the application. Added success and error messages to enhance user feedback during package creation and device connection status updates.

This commit is contained in:
2026-01-29 10:45:12 -06:00
parent a44f433bba
commit f5a688e741
7 changed files with 120 additions and 77 deletions
+40 -2
View File
@@ -1,12 +1,16 @@
package main
import (
"os"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type UIState struct {
Page string
Page string
Error string
Success string
}
func AppUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *tview.Frame {
@@ -18,6 +22,7 @@ func AppUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *
app.SetRoot(newFrame, true)
})
list.AddItem("Upload to Cardputer", "Creates a package and uploads it to the connected Cardputer", 'u', nil)
list.AddItem("Live Refresh", "Monitors for changes and automatically uploads to the Cardputer (WIP)", 'l', nil)
frame := tview.NewFrame(list)
frame.SetBorders(0, 0, 0, 0, 0, 0)
frame.AddText("Parchment - v0.0.1", true, tview.AlignLeft, tcell.ColorWhite)
@@ -26,9 +31,36 @@ func AppUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *
return frame
}
func SetError(state *UIState, text string) {
state.Success = ""
state.Error = text
}
func SetSuccess(state *UIState, text string) {
state.Error = ""
state.Success = text
}
func PackageUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *tview.Frame {
list := tview.NewList()
list.AddItem("Create Package", "Creates a .cpkg file in the running directory", 'c', nil)
list.AddItem("Create Package", "Creates a .cpkg file in the running directory", 'c', func() {
fsys := os.DirFS(".")
pkgData, err := loadPackageJSON(fsys)
if err != nil {
SetError(state, "Failed to load package.json")
return
}
if pkgData == nil {
SetError(state, "Failed to load package.json")
return
}
pkg, err := createCpkg(fsys, pkgData)
if err != nil {
SetError(state, err.Error())
} else {
SetSuccess(state, "Created file "+pkg.packageName+".cpkg")
}
})
list.AddItem("Unpack Package", "Unpacks a chosen .cpkg file into a subdirectory", 'u', nil)
list.AddItem("Back", "Go back to the home page", 'b', func() {
newFrame := AppUI(app, currentFrame, state)
@@ -52,6 +84,12 @@ func UpdateDeviceStatus(app *tview.Application, frame *tview.Frame, state *UISta
case "package":
frame.AddText("Package Tools", true, tview.AlignLeft, tcell.ColorWhite)
}
if state.Error != "" {
frame.AddText(state.Error, false, tview.AlignRight, tcell.ColorRed)
}
if state.Success != "" {
frame.AddText(state.Success, false, tview.AlignRight, tcell.ColorGreen)
}
frame.AddText(status, false, tview.AlignLeft, color)
})
}