From d2af418d3ea2e4ba867bfaee8249875226a84978 Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 11 Aug 2026 14:24:52 +0100 Subject: [PATCH 1/2] Add responsive style tabs --- README.md | 21 +++--- internal/ui/model.go | 69 ++++++++++++++--- internal/ui/model_test.go | 40 +++++----- internal/ui/stopwatch.go | 6 +- internal/ui/stopwatch_test.go | 14 ++-- internal/ui/tabs.go | 125 +++++++++++++++++++++++++++++++ internal/ui/tabs_test.go | 101 +++++++++++++++++++++++++ internal/ui/theme.go | 4 + internal/ui/view.go | 60 ++++++++++----- internal/ui/view_test.go | 30 ++++++++ internal/version/version.go | 2 +- internal/version/version_test.go | 4 +- main_test.go | 2 +- 13 files changed, 405 insertions(+), 73 deletions(-) create mode 100644 internal/ui/tabs.go create mode 100644 internal/ui/tabs_test.go diff --git a/README.md b/README.md index ec8aac5c..fa4acbb2 100644 --- a/README.md +++ b/README.md @@ -143,18 +143,21 @@ codexometer --codex /path/to/codex | Key | Action | | --- | --- | | `t` | Cycle color themes | -| `s` | Cycle meter styles | +| `Tab` | Select the next meter-style tab | +| `Shift+Tab` | Select the previous meter-style tab | | `r` | Refresh quota data immediately | -| `g` | Go: start the Stopwatch recorder (Stopwatch view only) | +| `s` | Start the Stopwatch recorder (Stopwatch view only) | | `p` | Stop and take a final up-to-date reading (Stopwatch view only) | | `q` | Quit | | `Esc` | Quit | | `Ctrl+C` | Quit | -The footer presents the same actions as clickable buttons. Move the pointer -over a button to highlight it, or click it to activate it. Buttons pulse +The responsive tab rail below the account status selects meter styles by mouse, +`Tab`, or `Shift+Tab`; labels condense automatically as the terminal narrows. +The footer presents the remaining actions as clickable buttons. Move the pointer +over a tab or button to highlight it, or click it to activate it. Controls pulse briefly when activated by mouse or keyboard. The keyboard assignments remain -available in terminals without mouse support. Theme and style changes are +available in terminals without mouse support. Theme and tab changes are immediate and do not trigger a network refresh. ## Themes @@ -171,7 +174,7 @@ The default remains the original green hacker-terminal presentation. ## Meter styles -Press `s` to cycle: +Select a tab with the mouse, `Tab`, or `Shift+Tab`: 1. **Bars** — chunky quota bars, with one full-width rate-limit window per row. 2. **Stopwatch** — establishes a zero baseline across locally active Codex @@ -273,9 +276,9 @@ need Go or Codexometer's source dependencies. ## Versioning -Codexometer follows semantic versioning; the current source version is `v0.1.1`. The Git tag is +Codexometer follows semantic versioning; the current source version is `v0.1.2`. The Git tag is the release source of truth. Go automatically embeds that tag in binaries built -with `go install github.com/merefield/codexometer@v0.1.1`; direct source builds +with `go install github.com/merefield/codexometer@v0.1.2`; direct source builds fall back to the maintained value in `internal/version/version.go`. Both forms report the embedded version and exit without starting the interface: @@ -288,7 +291,7 @@ codexometer --version Release automation can override the source-build fallback without editing code: ```sh -go build -ldflags="-s -w -X github.com/merefield/codexometer/internal/version.Fallback=0.1.1" . +go build -ldflags="-s -w -X github.com/merefield/codexometer/internal/version.Fallback=0.1.2" . ``` ## How refresh works diff --git a/internal/ui/model.go b/internal/ui/model.go index d6b7f995..df9be9d3 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -37,6 +37,11 @@ type Model struct { phase int theme themeID meterStyle meterStyleID + hoveredStyle meterStyleID + styleHovered bool + flashedStyle meterStyleID + styleFlashing bool + styleSequence uint64 hoveredButton footerButtonID flashedButton footerButtonID flashSequence uint64 @@ -100,6 +105,11 @@ type footerButtonFlashExpiredMsg struct { sequence uint64 } +type styleTabFlashExpiredMsg struct { + style meterStyleID + sequence uint64 +} + const footerButtonFlashDuration = 150 * time.Millisecond type footerButtonID int @@ -107,7 +117,6 @@ type footerButtonID int const ( footerButtonNone footerButtonID = iota footerButtonTheme - footerButtonStyle footerButtonRefresh footerButtonQuit footerButtonStopwatchGo @@ -122,7 +131,6 @@ type footerButton struct { var footerButtonDefinitions = []footerButton{ {id: footerButtonTheme, label: "[ (T)HEME ]", compact: "[T]"}, - {id: footerButtonStyle, label: "[ (S)TYLE ]", compact: "[S]"}, {id: footerButtonRefresh, label: "[ (R)EFRESH ]", compact: "[R]"}, {id: footerButtonQuit, label: "[ (Q)UIT ]", compact: "[Q]"}, } @@ -156,21 +164,33 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { case "t": return m.pressFooterButton(footerButtonTheme) case "s": - return m.pressFooterButton(footerButtonStyle) + if m.meterStyle == styleStopwatch { + return m.pressFooterButton(footerButtonStopwatchGo) + } + case "tab": + return m.pressStyleTab(m.meterStyle.next()) + case "shift+tab": + return m.pressStyleTab(m.meterStyle.previous()) case "r": return m.pressFooterButton(footerButtonRefresh) case "q": return m.pressFooterButton(footerButtonQuit) - case "g": - if m.meterStyle == styleStopwatch { - return m.pressFooterButton(footerButtonStopwatchGo) - } case "p": if m.meterStyle == styleStopwatch { return m.pressFooterButton(footerButtonStopwatchStop) } } case tea.MouseMsg: + if style, ok := m.styleTabAt(message.X, message.Y); ok { + m.styleHovered = true + m.hoveredStyle = style + m.hoveredButton = footerButtonNone + if message.Button == tea.MouseButtonLeft && message.Action == tea.MouseActionPress { + return m.pressStyleTab(style) + } + return m, nil + } + m.styleHovered = false button := m.footerButtonAt(message.X, message.Y) m.hoveredButton = button if message.Button == tea.MouseButtonLeft && message.Action == tea.MouseActionPress && button != footerButtonNone { @@ -220,10 +240,28 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) { if message.sequence == m.flashSequence && message.button == m.flashedButton { m.flashedButton = footerButtonNone } + case styleTabFlashExpiredMsg: + if message.sequence == m.styleSequence && message.style == m.flashedStyle { + m.styleFlashing = false + } } return m, nil } +func (m Model) pressStyleTab(style meterStyleID) (tea.Model, tea.Cmd) { + if style < styleBars || style >= styleCount { + return m, nil + } + m.meterStyle = style + m.styleSequence++ + m.flashedStyle = style + m.styleFlashing = true + sequence := m.styleSequence + return m, tea.Tick(footerButtonFlashDuration, func(time.Time) tea.Msg { + return styleTabFlashExpiredMsg{style: style, sequence: sequence} + }) +} + func (m Model) pressFooterButton(button footerButtonID) (tea.Model, tea.Cmd) { m.flashSequence++ m.flashedButton = button @@ -246,8 +284,6 @@ func (m Model) activateFooterButton(button footerButtonID) (Model, tea.Cmd) { switch button { case footerButtonTheme: m.theme = m.theme.next() - case footerButtonStyle: - m.meterStyle = m.meterStyle.next() case footerButtonRefresh: if !m.loading { m.loading = true @@ -294,7 +330,7 @@ func (m Model) footerButtonAt(x, y int) footerButtonID { return footerButtonNone } localX := x - 2 - buttons, separator := footerButtonLayout(layout.contentWidth) + buttons, separator := footerButtonLayoutWithTheme(layout.contentWidth, paletteFor(m.theme).name) buttonX := 0 for _, button := range buttons { if localX >= buttonX && localX < buttonX+len(button.label) { @@ -307,6 +343,7 @@ func (m Model) footerButtonAt(x, y int) footerButtonID { type dashboardGeometry struct { contentWidth int + tabsY int meterHeight int meterY int footerY int @@ -328,6 +365,7 @@ func (m Model) dashboardLayout() dashboardGeometry { headerHeight = 2 } const statusHeight = 1 + const tabsHeight = 1 const framedErrorHeight = 4 const footerHeight = 2 extraHeight := 0 @@ -337,14 +375,16 @@ func (m Model) dashboardLayout() dashboardGeometry { if len(m.snapshot.Meters()) == 0 { extraHeight += framedErrorHeight } - meterY := 1 + headerHeight + statusHeight + extraHeight - meterHeight := max(contentHeight-headerHeight-statusHeight-extraHeight-footerHeight, 1) + tabsY := 1 + headerHeight + statusHeight + meterY := tabsY + tabsHeight + extraHeight + meterHeight := max(contentHeight-headerHeight-statusHeight-tabsHeight-extraHeight-footerHeight, 1) footerY := meterY if m.meterStyle == styleStopwatch || len(m.snapshot.Meters()) > 0 { footerY += meterHeight } return dashboardGeometry{ contentWidth: contentWidth, + tabsY: tabsY, meterHeight: meterHeight, meterY: meterY, footerY: footerY, @@ -377,6 +417,11 @@ func footerButtonLayout(width int) ([]footerButton, string) { return buttons, separator } +func footerButtonLayoutWithTheme(width int, themeName string) ([]footerButton, string) { + themeWidth := len("THEME // ") + len(themeName) + return footerButtonLayout(max(width-themeWidth-1, 1)) +} + func (m Model) fetch() tea.Cmd { return func() tea.Msg { snapshot, err := m.fetcher.Fetch(context.Background()) diff --git a/internal/ui/model_test.go b/internal/ui/model_test.go index 158c1f54..1213ea3f 100644 --- a/internal/ui/model_test.go +++ b/internal/ui/model_test.go @@ -39,20 +39,25 @@ func TestThemeHotkeyCyclesAndWraps(t *testing.T) { } } -func TestStyleHotkeyCyclesAndWraps(t *testing.T) { +func TestTabCyclesStylesAndShiftTabMovesBack(t *testing.T) { model := New(nil, time.Minute) for want := styleStopwatch; want < styleCount; want++ { - updated, _ := model.Update(key('s')) + updated, _ := model.Update(tea.KeyMsg{Type: tea.KeyTab}) model = updated.(Model) if model.meterStyle != want { t.Fatalf("got meter style %d, want %d", model.meterStyle, want) } } - updated, _ := model.Update(key('s')) + updated, _ := model.Update(tea.KeyMsg{Type: tea.KeyTab}) model = updated.(Model) if model.meterStyle != styleBars { t.Fatalf("meter style did not wrap: got %d", model.meterStyle) } + updated, _ = model.Update(tea.KeyMsg{Type: tea.KeyShiftTab}) + model = updated.(Model) + if model.meterStyle != styleFuel { + t.Fatalf("reverse style navigation got %d, want %d", model.meterStyle, styleFuel) + } } func key(r rune) tea.KeyMsg { @@ -173,12 +178,6 @@ func TestFooterButtonsSupportHoverAndMouseClicks(t *testing.T) { t.Fatalf("theme click selected theme=%d flash=%d, want theme=%d flash=%d", model.theme, model.flashedButton, themeRust, footerButtonTheme) } - updated, _ = model.Update(footerMouseMessage(t, model, footerButtonStyle, tea.MouseActionPress)) - model = updated.(Model) - if model.meterStyle != styleStopwatch { - t.Fatalf("style click selected %d, want %d", model.meterStyle, styleStopwatch) - } - updated, command = model.Update(footerMouseMessage(t, model, footerButtonRefresh, tea.MouseActionPress)) model = updated.(Model) if !model.loading || command == nil { @@ -219,7 +218,7 @@ func TestFooterHitGeometryMatchesRenderedButtonsAcrossSizes(t *testing.T) { model.err = errors.New("stale quota signal") } layout := model.dashboardLayout() - for _, id := range []footerButtonID{footerButtonTheme, footerButtonStyle, footerButtonRefresh, footerButtonQuit} { + for _, id := range []footerButtonID{footerButtonTheme, footerButtonRefresh, footerButtonQuit} { mouse := footerMouseMessage(t, model, id, tea.MouseActionMotion) if mouse.Y != layout.footerY+1 { t.Errorf("rendered footer y=%d, calculated y=%d", mouse.Y, layout.footerY+1) @@ -252,28 +251,29 @@ func TestFooterButtonHotkeyFlashesAndLatestPulseWins(t *testing.T) { t.Fatal("theme hotkey pulse was not rendered") } - updated, _ = model.Update(key('s')) + model.loading = false + updated, _ = model.Update(key('r')) model = updated.(Model) - if model.flashedButton != footerButtonStyle || model.flashSequence == themeSequence { - t.Fatal("newer style pulse did not replace theme pulse") + if model.flashedButton != footerButtonRefresh || model.flashSequence == themeSequence { + t.Fatal("newer refresh pulse did not replace theme pulse") } updated, _ = model.Update(footerButtonFlashExpiredMsg{button: footerButtonTheme, sequence: themeSequence}) model = updated.(Model) - if model.flashedButton != footerButtonStyle { - t.Fatal("stale theme expiry cleared the newer style pulse") + if model.flashedButton != footerButtonRefresh { + t.Fatal("stale theme expiry cleared the newer refresh pulse") } - updated, _ = model.Update(footerButtonFlashExpiredMsg{button: footerButtonStyle, sequence: model.flashSequence}) + updated, _ = model.Update(footerButtonFlashExpiredMsg{button: footerButtonRefresh, sequence: model.flashSequence}) model = updated.(Model) if model.flashedButton != footerButtonNone { - t.Fatal("current style pulse did not expire") + t.Fatal("current refresh pulse did not expire") } } func TestFooterButtonLabelsEmbedHotkeysWithPadding(t *testing.T) { buttons, separator := footerButtonLayout(100) - want := []string{"[ (T)HEME ]", "[ (S)TYLE ]", "[ (R)EFRESH ]", "[ (Q)UIT ]"} + want := []string{"[ (T)HEME ]", "[ (R)EFRESH ]", "[ (Q)UIT ]"} if separator != " " || len(buttons) != len(want) { t.Fatalf("unexpected full footer layout: separator=%q buttons=%#v", separator, buttons) } @@ -287,7 +287,7 @@ func TestFooterButtonLabelsEmbedHotkeysWithPadding(t *testing.T) { if separator != " " { t.Fatalf("compact separator = %q, want one space", separator) } - for index, label := range []string{"[T]", "[S]", "[R]", "[Q]"} { + for index, label := range []string{"[T]", "[R]", "[Q]"} { if compact[index].label != label { t.Errorf("compact button %d label = %q, want %q", index, compact[index].label, label) } @@ -310,7 +310,7 @@ func footerMouseMessage(t *testing.T, model Model, id footerButtonID, action tea } func footerButtonByID(model Model, id footerButtonID) (footerButton, bool) { - buttons, _ := footerButtonLayout(model.contentWidth()) + buttons, _ := footerButtonLayoutWithTheme(model.contentWidth(), paletteFor(model.theme).name) for _, button := range buttons { if button.id == id { return button, true diff --git a/internal/ui/stopwatch.go b/internal/ui/stopwatch.go index e12c5b03..bb413624 100644 --- a/internal/ui/stopwatch.go +++ b/internal/ui/stopwatch.go @@ -77,7 +77,7 @@ func (m Model) renderStopwatchArea(width, height int, colors palette) stopwatchV layout := layoutStopwatchArea(width, height) readout := m.renderStopwatchReadout(layout.readoutWidth, layout.topHeight, colors) - goButton := m.renderStopwatchButton(layout.buttonWidths[0], layout.topHeight, "(G)O", footerButtonStopwatchGo, m.stopwatchGoEnabled(), colors) + goButton := m.renderStopwatchButton(layout.buttonWidths[0], layout.topHeight, "(S)TART", footerButtonStopwatchGo, m.stopwatchGoEnabled(), colors) stopButton := m.renderStopwatchButton(layout.buttonWidths[1], layout.topHeight, "STO(P)", footerButtonStopwatchStop, m.stopwatchStopEnabled(), colors) controls := lipgloss.JoinHorizontal(lipgloss.Top, goButton, strings.Repeat(" ", layout.gap), stopButton) top := lipgloss.JoinHorizontal(lipgloss.Top, readout, strings.Repeat(" ", layout.gap), controls) @@ -94,7 +94,7 @@ func (m Model) renderStopwatchArea(width, height int, colors palette) stopwatchV func (m Model) renderStopwatchReadout(width, height int, colors palette) string { state := "READY" - hint := "PRESS G OR CLICK GO // LOCAL SESSIONS" + hint := "PRESS S OR CLICK START // LOCAL SESSIONS" switch m.stopwatchState { case stopwatchStarting: state, hint = "ZEROING COUNTER", "SCANNING LOCAL CODEX TELEMETRY" @@ -197,7 +197,7 @@ func (m Model) renderStopwatchGraph(width, height int, colors palette) string { if len(samples) == 0 { message := "WAITING FOR FIRST SAMPLE" if m.stopwatchState == stopwatchIdle { - message = "PRESS GO TO ARM RECORDER" + message = "PRESS START TO ARM RECORDER" } else if m.stopwatchState == stopwatchStopped { message = "NO COMPLETE 30 SEC SAMPLE" } diff --git a/internal/ui/stopwatch_test.go b/internal/ui/stopwatch_test.go index 698ad7f6..ec5f39c1 100644 --- a/internal/ui/stopwatch_test.go +++ b/internal/ui/stopwatch_test.go @@ -22,15 +22,15 @@ func TestStopwatchGoSampleAndStopLifecycle(t *testing.T) { model.loading = false model.meterStyle = styleStopwatch - updated, command := model.Update(key('g')) + updated, command := model.Update(key('s')) model = updated.(Model) if command == nil || model.stopwatchState != stopwatchStarting || model.flashedButton != footerButtonStopwatchGo { t.Fatalf("Go did not arm stopwatch: state=%d flash=%d command=%v", model.stopwatchState, model.flashedButton, command) } - flashed := model.renderStopwatchButton(14, 6, "(G)O", footerButtonStopwatchGo, model.stopwatchGoEnabled(), paletteFor(themeHacker)) - wantFlash := lipgloss.NewStyle().Bold(true).Foreground(paletteFor(themeHacker).background).Background(paletteFor(themeHacker).primary).Render("(G)O") + flashed := model.renderStopwatchButton(14, 6, "(S)TART", footerButtonStopwatchGo, model.stopwatchGoEnabled(), paletteFor(themeHacker)) + wantFlash := lipgloss.NewStyle().Bold(true).Foreground(paletteFor(themeHacker).background).Background(paletteFor(themeHacker).primary).Render("(S)TART") if !strings.Contains(flashed, wantFlash) { - t.Fatal("Go hotkey did not visibly pulse after entering the starting state") + t.Fatal("Start hotkey did not visibly pulse after entering the starting state") } sequence := model.stopwatchRequest startedAt := time.Unix(100, 0) @@ -164,7 +164,7 @@ func TestStopwatchViewIsResponsiveAndGraphAutoScales(t *testing.T) { } output := model.View() plain := ansi.Strip(output) - for _, want := range []string{"SESSION READOUT", "6,250 TOKENS", "ELAPSED", "(G)O", "STO(P)", "LOCAL TOKEN BARS", "AUTO 0-10K", "█", "░"} { + for _, want := range []string{"SESSION READOUT", "6,250 TOKENS", "ELAPSED", "(S)TART", "STO(P)", "LOCAL TOKEN BARS", "AUTO 0-10K", "█", "░"} { if !strings.Contains(plain, want) { t.Errorf("%dx%d stopwatch missing %q:\n%s", size.width, size.height, want, plain) } @@ -222,8 +222,8 @@ func TestStopwatchLargeButtonsAreClickableAcrossTheirBoxes(t *testing.T) { if command != nil || model.hoveredButton != footerButtonStopwatchGo { t.Fatal("hovering the Go box did not select it") } - hovered := model.renderStopwatchButton(14, 6, "(G)O", footerButtonStopwatchGo, true, colors) - wantHover := lipgloss.NewStyle().Bold(true).Foreground(colors.accent).Background(colors.background).Render("(G)O") + hovered := model.renderStopwatchButton(14, 6, "(S)TART", footerButtonStopwatchGo, true, colors) + wantHover := lipgloss.NewStyle().Bold(true).Foreground(colors.accent).Background(colors.background).Render("(S)TART") if !strings.Contains(hovered, wantHover) { t.Fatal("hovering the Go box did not highlight its label") } diff --git a/internal/ui/tabs.go b/internal/ui/tabs.go new file mode 100644 index 00000000..e52c362f --- /dev/null +++ b/internal/ui/tabs.go @@ -0,0 +1,125 @@ +package ui + +import ( + "strings" + + "github.com/charmbracelet/lipgloss" +) + +type styleTab struct { + style meterStyleID + label string + x int + width int +} + +func styleTabLayout(width int, recording bool) ([]styleTab, string) { + stopwatchFull := "╭ STOPWATCH ╮" + stopwatchCompact := "╭TIMER╮" + stopwatchMinimal := "[S]" + microStopwatch := "S" + if recording { + stopwatchFull = "╭ STOPWATCH ●╮" + stopwatchCompact = "╭TIMER●╮" + stopwatchMinimal = "[S●]" + microStopwatch = "●" + } + tiers := [][]string{ + {"╭ BARS ╮", stopwatchFull, "╭ PIE ╮", "╭ CONSUMPTION PACE ╮", "╭ FUEL TANK ╮"}, + {"╭BAR╮", stopwatchCompact, "╭PIE╮", "╭PACE╮", "╭FUEL╮"}, + {"[B]", stopwatchMinimal, "[P]", "[C]", "[F]"}, + {"B", microStopwatch, "P", "C", "F"}, + } + separator := " " + labels := tiers[len(tiers)-1] + for _, candidate := range tiers { + total := len(candidate) - 1 + for _, label := range candidate { + total += lipgloss.Width(label) + } + if total <= width { + labels = candidate + break + } + } + + tabs := make([]styleTab, 0, styleCount) + x := 0 + for style, label := range labels { + tabWidth := lipgloss.Width(label) + if x+tabWidth > width { + break + } + tabs = append(tabs, styleTab{style: meterStyleID(style), label: label, x: x, width: tabWidth}) + x += tabWidth + len(separator) + } + return tabs, separator +} + +func (m Model) renderStyleTabs(width int, colors palette) string { + recording := m.stopwatchState == stopwatchRunning + tabs, separator := styleTabLayout(width, recording) + parts := make([]string, 0, len(tabs)) + used := 0 + for _, tab := range tabs { + active := tab.style == m.meterStyle + hovered := m.styleHovered && tab.style == m.hoveredStyle + flashed := m.styleFlashing && tab.style == m.flashedStyle + foreground, background := colors.dim, colors.background + bold := false + if active { + foreground, background, bold = colors.background, colors.primary, true + } + if hovered { + foreground, bold = colors.accent, true + if active { + foreground, background = colors.background, colors.accent + } + } + if flashed { + foreground, background, bold = colors.background, colors.accent, true + } + base := lipgloss.NewStyle().Foreground(foreground).Background(background).Bold(bold) + parts = append(parts, renderStyleTabLabel(tab.label, base, background, recording && tab.style == styleStopwatch, m.phase, colors)) + used += tab.width + } + if len(parts) > 1 { + used += (len(parts) - 1) * len(separator) + } + return strings.Join(parts, colors.dimmed().Render(separator)) + strings.Repeat(" ", max(width-used, 0)) +} + +func renderStyleTabLabel(label string, base lipgloss.Style, background lipgloss.Color, recording bool, phase int, colors palette) string { + if !recording || !strings.Contains(label, "●") { + return base.Render(label) + } + pieces := strings.SplitN(label, "●", 2) + dotColor := recordingDotColor(phase, colors) + dot := lipgloss.NewStyle().Bold(true).Foreground(dotColor).Background(background).Render("●") + return base.Render(pieces[0]) + dot + base.Render(pieces[1]) +} + +func recordingDotColor(phase int, colors palette) lipgloss.Color { + if phase%2 == 0 { + return colors.danger + } + return lipgloss.Color("#7A2633") +} + +func (m Model) styleTabAt(x, y int) (meterStyleID, bool) { + if x < 0 || y < 0 || (m.loading && len(m.snapshot.Meters()) == 0) { + return styleBars, false + } + layout := m.dashboardLayout() + if y != layout.tabsY { + return styleBars, false + } + localX := x - 2 + tabs, _ := styleTabLayout(layout.contentWidth, m.stopwatchState == stopwatchRunning) + for _, tab := range tabs { + if localX >= tab.x && localX < tab.x+tab.width { + return tab.style, true + } + } + return styleBars, false +} diff --git a/internal/ui/tabs_test.go b/internal/ui/tabs_test.go new file mode 100644 index 00000000..0e9910cb --- /dev/null +++ b/internal/ui/tabs_test.go @@ -0,0 +1,101 @@ +package ui + +import ( + "strings" + "testing" + "time" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" + + "github.com/merefield/codexometer/internal/codex" +) + +func TestStyleTabsChooseResponsiveLabels(t *testing.T) { + for _, test := range []struct { + width int + want string + }{ + {width: 100, want: "CONSUMPTION PACE"}, + {width: 44, want: "TIMER"}, + {width: 19, want: "[S]"}, + {width: 9, want: "S"}, + } { + t.Run(test.want, func(t *testing.T) { + tabs, _ := styleTabLayout(test.width, false) + if len(tabs) != int(styleCount) { + t.Fatalf("width %d displayed %d tabs, want %d", test.width, len(tabs), styleCount) + } + var labels strings.Builder + for _, tab := range tabs { + labels.WriteString(tab.label) + if tab.x+tab.width > test.width { + t.Fatalf("tab %q overflows width %d", tab.label, test.width) + } + } + if !strings.Contains(labels.String(), test.want) { + t.Fatalf("width %d labels %q do not contain %q", test.width, labels.String(), test.want) + } + }) + } +} + +func TestStyleTabsSupportHoverClickAndPulse(t *testing.T) { + model := New(stubFetcher{snapshot: codex.DemoSnapshot()}, time.Minute) + model.snapshot = codex.DemoSnapshot() + model.loading = false + model.width, model.height = 100, 30 + tabs, _ := styleTabLayout(model.contentWidth(), false) + target := tabs[stylePie] + mouse := tea.MouseMsg{ + X: 2 + target.x + target.width/2, + Y: model.dashboardLayout().tabsY, + Button: tea.MouseButtonLeft, + Action: tea.MouseActionMotion, + } + updated, command := model.Update(mouse) + model = updated.(Model) + if command != nil || !model.styleHovered || model.hoveredStyle != stylePie { + t.Fatalf("pie hover was not recorded: hovered=%v style=%d", model.styleHovered, model.hoveredStyle) + } + + mouse.Action = tea.MouseActionPress + updated, command = model.Update(mouse) + model = updated.(Model) + if command == nil || model.meterStyle != stylePie || !model.styleFlashing || model.flashedStyle != stylePie { + t.Fatalf("pie click did not select and pulse: style=%d flashing=%v", model.meterStyle, model.styleFlashing) + } + updated, _ = model.Update(styleTabFlashExpiredMsg{style: stylePie, sequence: model.styleSequence}) + model = updated.(Model) + if model.styleFlashing { + t.Fatal("current tab pulse did not expire") + } +} + +func TestStopwatchTabShowsPulsingRecordingDotWhileAnotherTabIsActive(t *testing.T) { + model := Model{meterStyle: styleBars, stopwatchState: stopwatchRunning} + colors := paletteFor(themeHacker) + model.phase = 0 + bright := model.renderStyleTabs(100, colors) + model.phase = 1 + dark := model.renderStyleTabs(100, colors) + if !strings.Contains(ansi.Strip(bright), "●") { + t.Fatal("recording tab did not retain a pulsing dot away from the Stopwatch view") + } + if recordingDotColor(0, colors) == recordingDotColor(1, colors) { + t.Fatal("recording tab dot does not alternate between bright and dark red") + } + if lipgloss.Width(bright) != 100 || lipgloss.Width(dark) != 100 { + t.Fatalf("tab rail did not fill its responsive width: bright=%d dark=%d", lipgloss.Width(bright), lipgloss.Width(dark)) + } +} + +func TestSOnlyStartsStopwatch(t *testing.T) { + model := Model{meterStyle: styleBars} + updated, command := model.Update(key('s')) + model = updated.(Model) + if command != nil || model.meterStyle != styleBars || model.stopwatchState != stopwatchIdle { + t.Fatal("S changed state outside the Stopwatch tab") + } +} diff --git a/internal/ui/theme.go b/internal/ui/theme.go index 4c20d028..1b8f1645 100644 --- a/internal/ui/theme.go +++ b/internal/ui/theme.go @@ -32,6 +32,10 @@ func (s meterStyleID) next() meterStyleID { return (s + 1) % styleCount } +func (s meterStyleID) previous() meterStyleID { + return (s - 1 + styleCount) % styleCount +} + func (s meterStyleID) name() string { return [...]string{ "BARS", diff --git a/internal/ui/view.go b/internal/ui/view.go index 72555fc7..0fb277b8 100644 --- a/internal/ui/view.go +++ b/internal/ui/view.go @@ -7,6 +7,7 @@ import ( "time" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" "github.com/merefield/codexometer/internal/codex" ) @@ -16,13 +17,16 @@ func (m Model) View() string { layout := m.dashboardLayout() contentWidth := layout.contentWidth - header := renderHeader(contentWidth, m.phase, colors) + account := "" + if !m.loading || len(m.snapshot.Meters()) > 0 { + account = m.renderAccount(colors) + } + header := renderHeader(contentWidth, m.phase, m.renderSignalStatus(colors), account, colors) parts := []string{header} if m.loading && len(m.snapshot.Meters()) == 0 { parts = append(parts, renderBoot(contentWidth, m.phase, colors)) } else { - status := m.renderStatus(contentWidth, colors) - parts = append(parts, status) + parts = append(parts, strings.Repeat(" ", contentWidth), m.renderStyleTabs(contentWidth, colors)) if m.err != nil { errorView := renderError(contentWidth, m.err, colors) parts = append(parts, errorView) @@ -47,17 +51,21 @@ func (m Model) View() string { return lipgloss.NewStyle().Margin(1, 2).Render(panel) } -func renderHeader(width, phase int, colors palette) string { +func renderHeader(width, phase int, signal, account string, colors palette) string { if width < 64 { - return colors.header().Render("▰ CODEXOMETER ▰") + "\n" + colors.dimmed().Render("QUOTA TELEMETRY CONSOLE // CRT-01") + title := colors.header().Render("▰ CODEXOMETER ▰") + subtitle := colors.dimmed().Render("QUOTA TELEMETRY CONSOLE // CRT-01") + return joinRight(title, signal, width) + "\n" + joinRight(subtitle, account, width) } logo := []string{ "█▀▀ █▀█ █▀▄ █▀▀ ▀▄▀ █▀█ █▀▄▀█ █▀▀ ▀█▀ █▀▀ █▀█", "█▄▄ █▄█ █▄▀ ██▄ █ █ █▄█ █ ▀ █ ██▄ █ ██▄ █▀▄", } beacon := []string{"◉", "◎", "◌", "◎"}[phase%4] - return colors.header().Render(strings.Join(logo, "\n")) + "\n" + - colors.dimmed().Render(fmt.Sprintf("%s QUOTA TELEMETRY CONSOLE // CRT-01 // SIGNAL LOCKED", beacon)) + subtitle := colors.dimmed().Render(fmt.Sprintf("%s QUOTA TELEMETRY CONSOLE // CRT-01 // SIGNAL LOCKED", beacon)) + return joinRight(colors.header().Render(logo[0]), signal, width) + "\n" + + colors.header().Render(logo[1]) + "\n" + + joinRight(subtitle, account, width) } func renderBoot(width, phase int, colors palette) string { @@ -67,7 +75,15 @@ func renderBoot(width, phase int, colors palette) string { return frame(width, "ACQUIRING SIGNAL", colors.label().Render(track)+"\n"+colors.dimmed().Render("HANDSHAKE WITH CODEX APP-SERVER IN PROGRESS"), colors.primary, colors) } -func (m Model) renderStatus(width int, colors palette) string { +func (m Model) renderAccount(colors palette) string { + plan := "UNKNOWN PLAN" + if m.snapshot.RateLimits.PlanType != nil { + plan = codex.DisplayName(*m.snapshot.RateLimits.PlanType) + } + return colors.dimmed().Render("ACCOUNT // " + plan) +} + +func (m Model) renderSignalStatus(colors palette) string { status := "ONLINE" color := colors.primary if m.err != nil { @@ -78,12 +94,20 @@ func (m Model) renderStatus(width int, colors palette) string { status = "SCANNING" color = colors.accent } - plan := "UNKNOWN PLAN" - if m.snapshot.RateLimits.PlanType != nil { - plan = codex.DisplayName(*m.snapshot.RateLimits.PlanType) + return lipgloss.NewStyle().Bold(true).Foreground(color).Render("● " + status) +} + +func joinRight(left, right string, width int) string { + if right == "" { + return left + } + if lipgloss.Width(right) >= width { + return ansi.Truncate(right, width, "") + } + availableLeft := max(width-lipgloss.Width(right)-1, 0) + if lipgloss.Width(left) > availableLeft { + left = ansi.Truncate(left, availableLeft, "") } - left := lipgloss.NewStyle().Bold(true).Foreground(color).Render("● " + status) - right := colors.dimmed().Render("ACCOUNT // " + plan + " // " + colors.name + " // " + m.meterStyle.name()) gap := max(width-lipgloss.Width(left)-lipgloss.Width(right), 1) return left + strings.Repeat(" ", gap) + right } @@ -97,10 +121,8 @@ func (m Model) renderFooter(width int, colors palette) string { if m.snapshot.RateLimitResetCredits != nil && m.snapshot.RateLimitResetCredits.AvailableCount > 0 { left += fmt.Sprintf(" // RESET TOKENS %d", m.snapshot.RateLimitResetCredits.AvailableCount) } - mode := fmt.Sprintf("THEME %s // VIEW %s", colors.name, m.meterStyle.name()) - gap := max(width-len(left)-len(mode), 1) - status := left + strings.Repeat(" ", gap) + mode - buttons, separator := footerButtonLayout(width) + status := left + buttons, separator := footerButtonLayoutWithTheme(width, colors.name) controls := make([]string, 0, len(buttons)) for _, button := range buttons { controls = append(controls, footerButtonAppearance( @@ -109,7 +131,9 @@ func (m Model) renderFooter(width int, colors palette) string { button.id == m.flashedButton, ).Render(button.label)) } - return colors.dimmed().Render(status) + "\n" + strings.Join(controls, separator) + theme := colors.dimmed().Render(fmt.Sprintf("THEME // %s", colors.name)) + controlRow := joinRight(strings.Join(controls, separator), theme, width) + return colors.dimmed().Render(status) + "\n" + controlRow } func footerButtonAppearance(colors palette, hovered, flashed bool) lipgloss.Style { diff --git a/internal/ui/view_test.go b/internal/ui/view_test.go index 5f684015..02c5481d 100644 --- a/internal/ui/view_test.go +++ b/internal/ui/view_test.go @@ -66,6 +66,36 @@ func TestViewReflowsAcrossTerminalDimensions(t *testing.T) { } } +func TestStatusAndFooterKeepOnlyEssentialMetadata(t *testing.T) { + model := Model{ + snapshot: codex.DemoSnapshot(), + nextRefresh: time.Now().Add(time.Minute), + meterStyle: styleBars, + } + colors := paletteFor(themeHacker) + account := ansi.Strip(model.renderAccount(colors)) + if account != "ACCOUNT // PLUS" { + t.Fatalf("account metadata = %q", account) + } + header := ansi.Strip(renderHeader(100, 0, model.renderSignalStatus(colors), model.renderAccount(colors), colors)) + headerLines := strings.Split(header, "\n") + firstLine := headerLines[0] + if !strings.HasSuffix(firstLine, "● ONLINE") { + t.Fatalf("online status is not at the top right of the header: %q", firstLine) + } + if !strings.HasSuffix(headerLines[len(headerLines)-1], "ACCOUNT // PLUS") { + t.Fatalf("account is not aligned with the subtitle: %q", headerLines[len(headerLines)-1]) + } + footer := ansi.Strip(model.renderFooter(100, colors)) + if !strings.Contains(footer, "THEME // HACKER") || strings.Contains(footer, "VIEW") { + t.Fatalf("footer theme readout = %q", footer) + } + footerLines := strings.Split(footer, "\n") + if !strings.HasSuffix(footerLines[len(footerLines)-1], "THEME // HACKER") { + t.Fatalf("theme is not at the bottom right of the footer: %q", footerLines[len(footerLines)-1]) + } +} + func TestPieViewPlacesQuotaWindowsSideBySide(t *testing.T) { model := Model{ snapshot: codex.DemoSnapshot(), diff --git a/internal/version/version.go b/internal/version/version.go index 0e4605ea..f9cbedb4 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -10,7 +10,7 @@ import ( // checkout. Release automation may override it with: // // -X github.com/merefield/codexometer/internal/version.Fallback=1.2.3 -var Fallback = "0.1.1" +var Fallback = "0.1.2" // Current returns the module version embedded by Go for tagged installs, or // Fallback for ordinary source builds. The leading tag prefix is omitted so diff --git a/internal/version/version_test.go b/internal/version/version_test.go index 7544c126..116a1262 100644 --- a/internal/version/version_test.go +++ b/internal/version/version_test.go @@ -4,7 +4,7 @@ import "testing" func TestCurrentReportsMaintainedFallbackForSourceBuild(t *testing.T) { got := Current() - if got != "0.1.1" { - t.Fatalf("Current() = %q, want 0.1.1", got) + if got != "0.1.2" { + t.Fatalf("Current() = %q, want 0.1.2", got) } } diff --git a/main_test.go b/main_test.go index b991a8fc..5127aa99 100644 --- a/main_test.go +++ b/main_test.go @@ -16,7 +16,7 @@ func TestRunVersion(t *testing.T) { for _, flag := range []string{"-v", "--version"} { var stdout, stderr bytes.Buffer code := run([]string{flag}, &stdout, &stderr, dependencies{}) - if code != 0 || stdout.String() != "codexometer v0.1.1\n" || stderr.Len() != 0 { + if code != 0 || stdout.String() != "codexometer v0.1.2\n" || stderr.Len() != 0 { t.Errorf("flag=%s code=%d stdout=%q stderr=%q", flag, code, stdout.String(), stderr.String()) } } From f467f884ded3aecb653dcbc555a3c8a010c2f52d Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 11 Aug 2026 16:03:52 +0100 Subject: [PATCH 2/2] Fix narrow footer hit targets --- README.md | 4 ++-- internal/ui/model.go | 20 +++++++++++++++++++- internal/ui/model_test.go | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fa4acbb2..3be7a15c 100644 --- a/README.md +++ b/README.md @@ -178,9 +178,9 @@ Select a tab with the mouse, `Tab`, or `Shift+Tab`: 1. **Bars** — chunky quota bars, with one full-width rate-limit window per row. 2. **Stopwatch** — establishes a zero baseline across locally active Codex - sessions when you press Go. A large readout follows newly appended token + sessions when you press Start. A large readout follows newly appended token telemetry once per second and shows total observed tokens, elapsed time, and - average rate; clickable Go and Stop controls sit beside it. The full-width + average rate; clickable Start and Stop controls sit beside it. The full-width plot adds one thin vertical block bar every 30 seconds for tokens observed in that interval. New bars enter on the right and push older bars left, and the Y axis automatically expands or contracts to fit the visible samples. Stop performs diff --git a/internal/ui/model.go b/internal/ui/model.go index df9be9d3..24d961d4 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -419,7 +419,25 @@ func footerButtonLayout(width int) ([]footerButton, string) { func footerButtonLayoutWithTheme(width int, themeName string) ([]footerButton, string) { themeWidth := len("THEME // ") + len(themeName) - return footerButtonLayout(max(width-themeWidth-1, 1)) + available := width - themeWidth - 1 + if available <= 0 { + return nil, " " + } + buttons, separator := footerButtonLayout(available) + visible := buttons[:0] + used := 0 + for _, button := range buttons { + required := len(button.label) + if len(visible) > 0 { + required += len(separator) + } + if used+required > available { + break + } + visible = append(visible, button) + used += required + } + return visible, separator } func (m Model) fetch() tea.Cmd { diff --git a/internal/ui/model_test.go b/internal/ui/model_test.go index 1213ea3f..0be88a80 100644 --- a/internal/ui/model_test.go +++ b/internal/ui/model_test.go @@ -294,6 +294,25 @@ func TestFooterButtonLabelsEmbedHotkeysWithPadding(t *testing.T) { } } +func TestFooterLayoutNeverHitTestsControlsTruncatedByTheme(t *testing.T) { + for _, themeName := range []string{"HACKER", "BLUE STEEL", "ULTRAVIOLET"} { + for width := 1; width <= 60; width++ { + buttons, separator := footerButtonLayoutWithTheme(width, themeName) + controlsWidth := 0 + for index, button := range buttons { + if index > 0 { + controlsWidth += len(separator) + } + controlsWidth += len(button.label) + } + available := max(width-len("THEME // ")-len(themeName)-1, 0) + if controlsWidth > available { + t.Fatalf("theme=%q width=%d exposes %d cells of controls in %d available cells", themeName, width, controlsWidth, available) + } + } + } +} + func footerMouseMessage(t *testing.T, model Model, id footerButtonID, action tea.MouseAction) tea.MouseMsg { t.Helper() button, ok := footerButtonByID(model, id)