Files
terminalShop/pkg/tui/menu.go
marcusDenslow 6006f9617d refactor: add dedicated *Switch() methods
added dedicated *Switch() methods (ShopSwitch, CartSwitc, AccountSwitch, ...) for page transitions and remove the catch-all resetPageState(). Each switch owns its pages zero-init, viewport setup, and async data fetching
2026-04-24 09:25:31 +02:00

91 lines
1.9 KiB
Go

package tui
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
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")
itemCount := 0
total := 0
for _, item := range m.Cart {
total += item.Quantity * item.Coffee.Price
itemCount += item.Quantity
}
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
}