Files
terminalShop/pkg/tui/menu.go
marcus06 dee6172875 refactor(tui): port to charm v2 (#9)
* refactor(tui): port to charm v2

bumped bubbletea, lipgloss, bubbles, wish, and huh to v2. Mostly
mechanical chur, new import paths, vewport getter/setter api, view()
returns tea.view now, huh.theme became an interface so the theme package
needed a small reshape. Render plumbing is gone (no more lipgloss.Render
field on the Model or MakeRenderer in main.go). All pages still work
over ssh. setting up for the refund composer next

* style(tui): gofmt main.go and menu.go
2026-05-25 19:09:16 +02:00

87 lines
1.8 KiB
Go

package tui
import (
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"fmt"
)
func (m Model) BuildMenuView() string {
bold := m.theme.TextAccent().Bold(true)
base := m.theme.TextBody()
rowStyle := lipgloss.NewStyle().Padding(0, 1)
rows := []string{
rowStyle.Render(bold.Render("s") + " " + base.Render("shop")),
rowStyle.Render(bold.Render("a") + " " + base.Render("account")),
rowStyle.Render(bold.Render("c") + " " + base.Render("cart")),
rowStyle.Render(""),
rowStyle.Render(bold.Render("q") + " " + base.Render("quit")),
}
menuContent := ""
for _, row := range rows {
menuContent += row + "\n"
}
logoStyle := m.theme.TextAccent().Bold(true)
logo := logoStyle.Render("terminal coffee")
modal := lipgloss.NewStyle().Border(lipgloss.NormalBorder()).BorderForeground(m.theme.Border()).Padding(1, 2).Render(menuContent)
hint := base.Render("press esc to close")
total := m.CalculateSubtotal()
itemCount := m.CartItemCount()
cartHint := base.Render(fmt.Sprintf("cart $%.2f [%d]", float64(total)/100, itemCount))
assembled := lipgloss.JoinVertical(
lipgloss.Center,
logo,
"",
modal,
"",
hint,
cartHint,
)
return lipgloss.Place(
m.viewportWidth,
m.viewportHeight,
lipgloss.Center,
lipgloss.Center,
assembled,
)
}
func (m Model) MenuUpdate(msg tea.Msg) (Model, tea.Cmd) {
keyMsg, ok := msg.(tea.KeyMsg)
if !ok {
return m, nil
}
switch keyMsg.String() {
case "s":
m.ShowingMenu = false
m, cmd := m.ShopSwitch()
return m, cmd
case "a":
m.ShowingMenu = false
return m.AccountSwitch()
case "c":
m.ShowingMenu = false
m, cmd := m.CartSwitch()
return m, cmd
case "esc":
m.ShowingMenu = false
case "?":
m.ShowingMenu = false
m.ShowingHelp = true
case "q", "ctrl+c":
return m, tea.Quit
}
return m, nil
}