From 73e549cec15633f3cbfd121d5c234eb4821f8875 Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 11 Aug 2026 13:45:13 +0100 Subject: [PATCH] Address stopwatch performance review --- README.md | 28 +++++----- internal/codex/live_usage.go | 12 +++-- internal/codex/live_usage_test.go | 33 ++++++++++++ internal/ui/meters.go | 7 ++- internal/ui/meters_test.go | 6 ++- internal/ui/model.go | 62 ++++++++++++++++++--- internal/ui/model_test.go | 46 ++++++++++++++-- internal/ui/stopwatch.go | 90 +++++++++++++++---------------- internal/ui/stopwatch_test.go | 22 ++++---- internal/ui/theme.go | 4 +- internal/ui/view.go | 23 ++------ internal/ui/view_test.go | 2 +- internal/version/version.go | 2 +- internal/version/version_test.go | 4 +- main_test.go | 2 +- 15 files changed, 231 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 9597437f..ec8aac5c 100644 --- a/README.md +++ b/README.md @@ -174,23 +174,23 @@ The default remains the original green hacker-terminal presentation. Press `s` to cycle: 1. **Bars** — chunky quota bars, with one full-width rate-limit window per row. -2. **Pie** — clockwise-filled circles rendered on a 2×4 sub-cell Braille canvas +2. **Stopwatch** — establishes a zero baseline across locally active Codex + sessions when you press Go. 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 + 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 + an immediate final local read instead of relying on the latest graph sample. +3. **Pie** — clockwise-filled circles rendered on a 2×4 sub-cell Braille canvas for clean curves at any size. -3. **Consumption Pace** — a signed horizontal scale comparing elapsed window +4. **Consumption Pace** — a signed horizontal scale comparing elapsed window time with quota consumed. Positive headroom means consumption is behind elapsed time; a negative deficit means quota is being used too quickly. -4. **Fuel Tank** — a reverse gauge whose bright segment shows remaining range +5. **Fuel Tank** — a reverse gauge whose bright segment shows remaining range and whose dark segment shows consumed capacity, labelled from Empty to Full; one full-width tank appears per row. Its reset-cycle comparison also drains backward and aligns exactly with the tank's first and last inner cells. -5. **Stopwatch** — establishes a zero baseline across locally active Codex - sessions when you press Go. 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 - 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 - an immediate final local read instead of relying on the latest graph sample. The layout responds to both terminal dimensions and the number of rate limits returned by Codex. Header, status, errors, footer, and meter grid divide the @@ -273,9 +273,9 @@ need Go or Codexometer's source dependencies. ## Versioning -Codexometer follows semantic versioning, beginning with `v0.1.0`. The Git tag is +Codexometer follows semantic versioning; the current source version is `v0.1.1`. 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.0`; direct source builds +with `go install github.com/merefield/codexometer@v0.1.1`; 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 +288,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.0" . +go build -ldflags="-s -w -X github.com/merefield/codexometer/internal/version.Fallback=0.1.1" . ``` ## How refresh works diff --git a/internal/codex/live_usage.go b/internal/codex/live_usage.go index 8207dbb7..8136c324 100644 --- a/internal/codex/live_usage.go +++ b/internal/codex/live_usage.go @@ -17,7 +17,8 @@ import ( const ( activeRolloutHorizon = 24 * time.Hour - fullDiscoveryEvery = 30 * time.Second + discoveryEvery = 5 * time.Second + fullDiscoveryEvery = 5 * time.Minute ) // LiveUsageSnapshot is a process-local, monotonically increasing count built @@ -36,6 +37,7 @@ type LiveUsageReader struct { mu sync.Mutex initialized bool startedAt time.Time + lastDiscovery time.Time lastFullDiscovery time.Time files map[string]*rolloutCursor totalTokens int64 @@ -112,8 +114,11 @@ func (r *LiveUsageReader) fetchTokenUsage(ctx context.Context, forceFullDiscover if err := r.initialize(ctx, now); err != nil { return LiveUsageSnapshot{}, err } - } else if err := r.discover(ctx, now, forceFullDiscovery || now.Sub(r.lastFullDiscovery) >= fullDiscoveryEvery); err != nil { - return LiveUsageSnapshot{}, err + } else if forceFullDiscovery || now.Sub(r.lastDiscovery) >= discoveryEvery { + full := forceFullDiscovery || now.Sub(r.lastFullDiscovery) >= fullDiscoveryEvery + if err := r.discover(ctx, now, full); err != nil { + return LiveUsageSnapshot{}, err + } } var firstReadErr error @@ -217,6 +222,7 @@ func (r *LiveUsageReader) discover(ctx context.Context, now time.Time, full bool if firstFileErr != nil && len(r.files) == 0 { return fmt.Errorf("read local Codex session telemetry: %w", firstFileErr) } + r.lastDiscovery = now if full { r.lastFullDiscovery = now } diff --git a/internal/codex/live_usage_test.go b/internal/codex/live_usage_test.go index 990833cb..c353e24b 100644 --- a/internal/codex/live_usage_test.go +++ b/internal/codex/live_usage_test.go @@ -73,6 +73,7 @@ func TestLiveUsageReaderHandlesNewOldAndResetSessions(t *testing.T) { newPath := testRolloutPath(t, home, now.Add(2*time.Second), "new") writeRollout(t, newPath, tokenCountLine(now.Add(2*time.Second), 40)+"\n") + reader.lastDiscovery = time.Time{} usage, err := reader.FetchTokenUsage(context.Background()) if err != nil || usage.TotalTokens != 40 { t.Fatalf("new session usage = %#v, %v; want 40", usage, err) @@ -80,6 +81,7 @@ func TestLiveUsageReaderHandlesNewOldAndResetSessions(t *testing.T) { oldPath := testRolloutPath(t, home, now.Add(-2*time.Hour), "resumed") writeRollout(t, oldPath, tokenCountLine(now.Add(-2*time.Hour), 900)+"\n") + reader.lastDiscovery = time.Time{} usage, err = reader.FetchTokenUsage(context.Background()) if err != nil || usage.TotalTokens != 40 { t.Fatalf("late-discovered old baseline counted historical usage: %#v, %v", usage, err) @@ -98,6 +100,37 @@ func TestLiveUsageReaderHandlesNewOldAndResetSessions(t *testing.T) { } } +func TestLiveUsageReaderThrottlesDiscoveryButConsumesKnownFiles(t *testing.T) { + home := t.TempDir() + now := time.Now() + knownPath := testRolloutPath(t, home, now.Add(-time.Hour), "known-throttle") + writeRollout(t, knownPath, tokenCountLine(now.Add(-time.Hour), 100)+"\n") + reader, err := NewLiveUsageReader(home) + if err != nil { + t.Fatal(err) + } + if _, err := reader.FetchTokenUsage(context.Background()); err != nil { + t.Fatal(err) + } + + appendRollout(t, knownPath, tokenCountLine(now, 150)+"\n") + newPath := testRolloutPath(t, home, now.Add(time.Second), "new-throttle") + writeRollout(t, newPath, tokenCountLine(now.Add(time.Second), 40)+"\n") + usage, err := reader.FetchTokenUsage(context.Background()) + if err != nil || usage.TotalTokens != 50 { + t.Fatalf("known cursor was not consumed independently of discovery: %#v, %v", usage, err) + } + if _, exists := reader.files[newPath]; exists { + t.Fatal("new rollout was discovered before the discovery interval elapsed") + } + + reader.lastDiscovery = time.Now().Add(-discoveryEvery) + usage, err = reader.FetchTokenUsage(context.Background()) + if err != nil || usage.TotalTokens != 90 { + t.Fatalf("due discovery did not add new rollout usage: %#v, %v", usage, err) + } +} + func TestFreshLiveUsageReadNoticesAResumedDormantSession(t *testing.T) { home := t.TempDir() now := time.Now() diff --git a/internal/ui/meters.go b/internal/ui/meters.go index 5f36a498..527df5c8 100644 --- a/internal/ui/meters.go +++ b/internal/ui/meters.go @@ -13,7 +13,12 @@ import ( ) func usesMeterGrid(style meterStyleID) bool { - return style >= styleBars && style < styleStopwatch + switch style { + case styleBars, stylePie, styleConsumptionPace, styleFuel: + return true + default: + return false + } } func renderMeterGrid(width, height int, meters []codex.Meter, style meterStyleID, colors palette) string { diff --git a/internal/ui/meters_test.go b/internal/ui/meters_test.go index 5d081611..616e7b4f 100644 --- a/internal/ui/meters_test.go +++ b/internal/ui/meters_test.go @@ -12,6 +12,8 @@ import ( "github.com/merefield/codexometer/internal/codex" ) +var quotaMeterStyles = []meterStyleID{styleBars, stylePie, styleConsumptionPace, styleFuel} + func TestEveryMeterStyleHasDistinctiveOutput(t *testing.T) { colors := paletteFor(themeHacker) tests := []struct { @@ -33,7 +35,7 @@ func TestEveryMeterStyleHasDistinctiveOutput(t *testing.T) { func TestEveryMeterStyleUsesAllocatedRectangle(t *testing.T) { colors := paletteFor(themeHacker) - for style := styleBars; style < styleStopwatch; style++ { + for _, style := range quotaMeterStyles { output := renderVisualizationSized(50, 12, 62, style, colors.primary, colors) if got := lipgloss.Width(output); got > 50 { t.Errorf("%s width = %d, exceeds allocation 50", style.name(), got) @@ -53,7 +55,7 @@ func TestEveryStyleIncludesResetCycleGauge(t *testing.T) { Name: "1 HOUR", Window: codex.Window{UsedPercent: 40, WindowDurationMins: &duration, ResetsAt: &reset}, } - for style := styleBars; style < styleStopwatch; style++ { + for _, style := range quotaMeterStyles { output := ansi.Strip(renderMeterArea(80, 18, meter, style, colors)) if !strings.Contains(output, "RESET CYCLE 50%") { t.Errorf("%s missing reset-cycle comparison gauge:\n%s", style.name(), output) diff --git a/internal/ui/model.go b/internal/ui/model.go index bdd966aa..d6b7f995 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -7,7 +7,6 @@ import ( "time" tea "github.com/charmbracelet/bubbletea" - "github.com/charmbracelet/x/ansi" "github.com/merefield/codexometer/internal/codex" ) @@ -282,8 +281,7 @@ func (m Model) footerButtonAt(x, y int) footerButtonID { if x < 0 || y < 0 { return footerButtonNone } - lines := strings.Split(ansi.Strip(m.View()), "\n") - if y >= len(lines) { + if m.loading && len(m.snapshot.Meters()) == 0 { return footerButtonNone } if m.meterStyle == styleStopwatch { @@ -291,16 +289,68 @@ func (m Model) footerButtonAt(x, y int) footerButtonID { return button } } - buttons, _ := footerButtonLayout(m.contentWidth()) + layout := m.dashboardLayout() + if y != layout.footerY+1 { + return footerButtonNone + } + localX := x - 2 + buttons, separator := footerButtonLayout(layout.contentWidth) + buttonX := 0 for _, button := range buttons { - start := strings.Index(lines[y], button.label) - if start >= 0 && x >= start && x < start+len(button.label) { + if localX >= buttonX && localX < buttonX+len(button.label) { return button.id } + buttonX += len(button.label) + len(separator) } return footerButtonNone } +type dashboardGeometry struct { + contentWidth int + meterHeight int + meterY int + footerY int +} + +func (m Model) dashboardLayout() dashboardGeometry { + width := m.width + if width == 0 { + width = 80 + } + height := m.height + if height == 0 { + height = 24 + } + contentWidth := max(width-4, 1) + contentHeight := max(height-2, 1) + headerHeight := 3 + if contentWidth < 64 { + headerHeight = 2 + } + const statusHeight = 1 + const framedErrorHeight = 4 + const footerHeight = 2 + extraHeight := 0 + if m.err != nil { + extraHeight += framedErrorHeight + } + if len(m.snapshot.Meters()) == 0 { + extraHeight += framedErrorHeight + } + meterY := 1 + headerHeight + statusHeight + extraHeight + meterHeight := max(contentHeight-headerHeight-statusHeight-extraHeight-footerHeight, 1) + footerY := meterY + if m.meterStyle == styleStopwatch || len(m.snapshot.Meters()) > 0 { + footerY += meterHeight + } + return dashboardGeometry{ + contentWidth: contentWidth, + meterHeight: meterHeight, + meterY: meterY, + footerY: footerY, + } +} + func (m Model) contentWidth() int { width := m.width if width == 0 { diff --git a/internal/ui/model_test.go b/internal/ui/model_test.go index 713eb72f..158c1f54 100644 --- a/internal/ui/model_test.go +++ b/internal/ui/model_test.go @@ -41,7 +41,7 @@ func TestThemeHotkeyCyclesAndWraps(t *testing.T) { func TestStyleHotkeyCyclesAndWraps(t *testing.T) { model := New(nil, time.Minute) - for want := stylePie; want < styleCount; want++ { + for want := styleStopwatch; want < styleCount; want++ { updated, _ := model.Update(key('s')) model = updated.(Model) if model.meterStyle != want { @@ -175,8 +175,8 @@ func TestFooterButtonsSupportHoverAndMouseClicks(t *testing.T) { updated, _ = model.Update(footerMouseMessage(t, model, footerButtonStyle, tea.MouseActionPress)) model = updated.(Model) - if model.meterStyle != stylePie { - t.Fatalf("style click selected %d, want %d", model.meterStyle, stylePie) + 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)) @@ -192,6 +192,46 @@ func TestFooterButtonsSupportHoverAndMouseClicks(t *testing.T) { } } +func TestFooterHitGeometryMatchesRenderedButtonsAcrossSizes(t *testing.T) { + for _, test := range []struct { + name string + width, height int + empty bool + withError bool + style meterStyleID + }{ + {name: "compact", width: 40, height: 24}, + {name: "standard", width: 80, height: 24}, + {name: "large error", width: 120, height: 40, withError: true}, + {name: "empty quota", width: 80, height: 24, empty: true}, + {name: "empty stopwatch", width: 80, height: 24, empty: true, style: styleStopwatch}, + } { + t.Run(test.name, func(t *testing.T) { + model := New(stubFetcher{snapshot: codex.DemoSnapshot()}, time.Minute) + if !test.empty { + model.snapshot = codex.DemoSnapshot() + } + model.loading = false + model.width = test.width + model.height = test.height + model.meterStyle = test.style + if test.withError { + model.err = errors.New("stale quota signal") + } + layout := model.dashboardLayout() + for _, id := range []footerButtonID{footerButtonTheme, footerButtonStyle, 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) + } + if got := model.footerButtonAt(mouse.X, mouse.Y); got != id { + t.Errorf("hit button %d, want %d", got, id) + } + } + }) + } +} + func TestFooterButtonHotkeyFlashesAndLatestPulseWins(t *testing.T) { model := New(stubFetcher{snapshot: codex.DemoSnapshot()}, time.Minute) model.snapshot = codex.DemoSnapshot() diff --git a/internal/ui/stopwatch.go b/internal/ui/stopwatch.go index e04e0b27..e12c5b03 100644 --- a/internal/ui/stopwatch.go +++ b/internal/ui/stopwatch.go @@ -28,7 +28,19 @@ type stopwatchView struct { stopRect stopwatchRect } -func (m Model) renderStopwatchArea(width, height int, colors palette) stopwatchView { +type stopwatchGeometry struct { + width int + height int + gap int + topHeight int + graphHeight int + readoutWidth int + buttonWidths [2]int + goRect stopwatchRect + stopRect stopwatchRect +} + +func layoutStopwatchArea(width, height int) stopwatchGeometry { width = max(width, 1) height = max(height, 1) gap := 1 @@ -45,28 +57,38 @@ func (m Model) renderStopwatchArea(width, height int, colors palette) stopwatchV readoutWidth = max(width-controlsWidth-gap, 1) } buttonWidths := distributeSpace(max(controlsWidth-gap, 2), 2) + goX := readoutWidth + gap + return stopwatchGeometry{ + width: width, + height: height, + gap: gap, + topHeight: topHeight, + graphHeight: graphHeight, + readoutWidth: readoutWidth, + buttonWidths: [2]int{buttonWidths[0], buttonWidths[1]}, + goRect: stopwatchRect{x: goX, width: buttonWidths[0], height: topHeight}, + stopRect: stopwatchRect{ + x: goX + buttonWidths[0] + gap, width: buttonWidths[1], height: topHeight, + }, + } +} + +func (m Model) renderStopwatchArea(width, height int, colors palette) stopwatchView { + layout := layoutStopwatchArea(width, height) - readout := m.renderStopwatchReadout(readoutWidth, topHeight, colors) - goButton := m.renderStopwatchButton(buttonWidths[0], topHeight, "(G)O", footerButtonStopwatchGo, m.stopwatchGoEnabled(), colors) - stopButton := m.renderStopwatchButton(buttonWidths[1], topHeight, "STO(P)", footerButtonStopwatchStop, m.stopwatchStopEnabled(), colors) - controls := lipgloss.JoinHorizontal(lipgloss.Top, goButton, strings.Repeat(" ", gap), stopButton) - top := lipgloss.JoinHorizontal(lipgloss.Top, readout, strings.Repeat(" ", gap), controls) - graph := m.renderStopwatchGraph(width, graphHeight, colors) - view := lipgloss.JoinVertical(lipgloss.Left, top, strings.Repeat("\n", gap-1)+graph) - if padding := height - lipgloss.Height(view); padding > 0 { + readout := m.renderStopwatchReadout(layout.readoutWidth, layout.topHeight, colors) + goButton := m.renderStopwatchButton(layout.buttonWidths[0], layout.topHeight, "(G)O", 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) + graph := m.renderStopwatchGraph(layout.width, layout.graphHeight, colors) + view := lipgloss.JoinVertical(lipgloss.Left, top, strings.Repeat("\n", layout.gap-1)+graph) + if padding := layout.height - lipgloss.Height(view); padding > 0 { view += strings.Repeat("\n", padding) } - goX := lipgloss.Width(readout) + gap return stopwatchView{ - view: view, - goRect: stopwatchRect{ - x: goX, y: 0, width: lipgloss.Width(goButton), height: lipgloss.Height(goButton), - }, - stopRect: stopwatchRect{ - x: goX + lipgloss.Width(goButton) + gap, - y: 0, width: lipgloss.Width(stopButton), height: lipgloss.Height(stopButton), - }, + view: view, goRect: layout.goRect, stopRect: layout.stopRect, } } @@ -238,35 +260,9 @@ func (m Model) stopwatchButtonAt(x, y int) footerButtonID { if m.loading && len(m.snapshot.Meters()) == 0 { return footerButtonNone } - width := m.width - if width == 0 { - width = 80 - } - height := m.height - if height == 0 { - height = 24 - } - contentWidth := max(width-4, 1) - contentHeight := max(height-2, 1) - colors := paletteFor(m.theme) - header := renderHeader(contentWidth, m.phase, colors) - status := m.renderStatus(contentWidth, colors) - originY := 1 + lipgloss.Height(header) + lipgloss.Height(status) - reservedHeight := lipgloss.Height(header) + lipgloss.Height(status) - if m.err != nil { - errorView := renderError(contentWidth, m.err, colors) - originY += lipgloss.Height(errorView) - reservedHeight += lipgloss.Height(errorView) - } - if len(m.snapshot.Meters()) == 0 { - emptyView := renderError(contentWidth, fmt.Errorf("no quota windows returned"), colors) - originY += lipgloss.Height(emptyView) - reservedHeight += lipgloss.Height(emptyView) - } - footer := m.renderFooter(contentWidth, colors) - reservedHeight += lipgloss.Height(footer) - area := m.renderStopwatchArea(contentWidth, max(contentHeight-reservedHeight, 1), colors) - localX, localY := x-2, y-originY + dashboard := m.dashboardLayout() + area := layoutStopwatchArea(dashboard.contentWidth, dashboard.meterHeight) + localX, localY := x-2, y-dashboard.meterY if area.goRect.contains(localX, localY) { return footerButtonStopwatchGo } diff --git a/internal/ui/stopwatch_test.go b/internal/ui/stopwatch_test.go index c0f9a6a6..698ad7f6 100644 --- a/internal/ui/stopwatch_test.go +++ b/internal/ui/stopwatch_test.go @@ -195,19 +195,19 @@ func TestStopwatchLargeButtonsAreClickableAcrossTheirBoxes(t *testing.T) { meterStyle: styleStopwatch, stopwatchState: stopwatchIdle, } colors := paletteFor(model.theme) - contentWidth := model.contentWidth() - contentHeight := model.height - 2 - header := renderHeader(contentWidth, model.phase, colors) - status := model.renderStatus(contentWidth, colors) - footer := model.renderFooter(contentWidth, colors) - area := model.renderStopwatchArea(contentWidth, contentHeight-lipgloss.Height(header)-lipgloss.Height(status)-lipgloss.Height(footer), colors) + dashboard := model.dashboardLayout() + geometry := layoutStopwatchArea(dashboard.contentWidth, dashboard.meterHeight) + area := model.renderStopwatchArea(dashboard.contentWidth, dashboard.meterHeight, colors) + if area.goRect != geometry.goRect || area.stopRect != geometry.stopRect { + t.Fatalf("rendered controls diverged from pure layout: rendered=%#v layout=%#v", area, geometry) + } originX := 2 - originY := 1 + lipgloss.Height(header) + lipgloss.Height(status) + originY := dashboard.meterY for _, test := range []struct { rect stopwatchRect want footerButtonID - }{{area.goRect, footerButtonStopwatchGo}, {area.stopRect, footerButtonStopwatchStop}} { + }{{geometry.goRect, footerButtonStopwatchGo}, {geometry.stopRect, footerButtonStopwatchStop}} { // Hit an otherwise blank spot inside the large box, not merely its label. x := originX + test.rect.x + 1 y := originY + test.rect.y + 1 @@ -215,8 +215,8 @@ func TestStopwatchLargeButtonsAreClickableAcrossTheirBoxes(t *testing.T) { t.Errorf("button hit = %d, want %d at %d,%d", got, test.want, x, y) } } - hoverX := originX + area.goRect.x + 1 - hoverY := originY + area.goRect.y + 1 + hoverX := originX + geometry.goRect.x + 1 + hoverY := originY + geometry.goRect.y + 1 updated, command := model.Update(tea.MouseMsg{X: hoverX, Y: hoverY, Action: tea.MouseActionMotion}) model = updated.(Model) if command != nil || model.hoveredButton != footerButtonStopwatchGo { @@ -229,7 +229,7 @@ func TestStopwatchLargeButtonsAreClickableAcrossTheirBoxes(t *testing.T) { } updated, command = model.Update(tea.MouseMsg{ - X: originX + area.goRect.x + 1, Y: originY + area.goRect.y + 1, + X: originX + geometry.goRect.x + 1, Y: originY + geometry.goRect.y + 1, Button: tea.MouseButtonLeft, Action: tea.MouseActionPress, }) model = updated.(Model) diff --git a/internal/ui/theme.go b/internal/ui/theme.go index 94c72c09..4c20d028 100644 --- a/internal/ui/theme.go +++ b/internal/ui/theme.go @@ -21,10 +21,10 @@ type meterStyleID int const ( styleBars meterStyleID = iota + styleStopwatch stylePie styleConsumptionPace styleFuel - styleStopwatch styleCount ) @@ -35,10 +35,10 @@ func (s meterStyleID) next() meterStyleID { func (s meterStyleID) name() string { return [...]string{ "BARS", + "STOPWATCH", "PIE", "CONSUMPTION PACE", "FUEL TANK", - "STOPWATCH", }[s] } diff --git a/internal/ui/view.go b/internal/ui/view.go index 07e16dff..72555fc7 100644 --- a/internal/ui/view.go +++ b/internal/ui/view.go @@ -13,16 +13,8 @@ import ( func (m Model) View() string { colors := paletteFor(m.theme) - width := m.width - if width == 0 { - width = 80 - } - height := m.height - if height == 0 { - height = 24 - } - contentWidth := max(width-4, 1) - contentHeight := max(height-2, 1) + layout := m.dashboardLayout() + contentWidth := layout.contentWidth header := renderHeader(contentWidth, m.phase, colors) parts := []string{header} @@ -31,27 +23,22 @@ func (m Model) View() string { } else { status := m.renderStatus(contentWidth, colors) parts = append(parts, status) - reservedHeight := lipgloss.Height(header) + lipgloss.Height(status) if m.err != nil { errorView := renderError(contentWidth, m.err, colors) parts = append(parts, errorView) - reservedHeight += lipgloss.Height(errorView) } meters := m.snapshot.Meters() if len(meters) == 0 { emptyView := renderError(contentWidth, fmt.Errorf("no quota windows returned"), colors) parts = append(parts, emptyView) - reservedHeight += lipgloss.Height(emptyView) } footer := m.renderFooter(contentWidth, colors) - reservedHeight += lipgloss.Height(footer) - meterHeight := max(contentHeight-reservedHeight, 1) if m.meterStyle == styleStopwatch { - parts = append(parts, m.renderStopwatchArea(contentWidth, meterHeight, colors).view) + parts = append(parts, m.renderStopwatchArea(contentWidth, layout.meterHeight, colors).view) } else if len(meters) > 1 { - parts = append(parts, renderMeterGrid(contentWidth, meterHeight, meters, m.meterStyle, colors)) + parts = append(parts, renderMeterGrid(contentWidth, layout.meterHeight, meters, m.meterStyle, colors)) } else if len(meters) == 1 { - parts = append(parts, renderMeterArea(contentWidth, meterHeight, meters[0], m.meterStyle, colors)) + parts = append(parts, renderMeterArea(contentWidth, layout.meterHeight, meters[0], m.meterStyle, colors)) } parts = append(parts, footer) } diff --git a/internal/ui/view_test.go b/internal/ui/view_test.go index 702ca920..5f684015 100644 --- a/internal/ui/view_test.go +++ b/internal/ui/view_test.go @@ -88,7 +88,7 @@ func TestPieViewPlacesQuotaWindowsSideBySide(t *testing.T) { } func TestGaugeGridStyleClassification(t *testing.T) { - for style := styleBars; style < styleStopwatch; style++ { + for _, style := range []meterStyleID{styleBars, stylePie, styleConsumptionPace, styleFuel} { if got := usesMeterGrid(style); !got { t.Errorf("usesMeterGrid(%s) = false, want true", style.name()) } diff --git a/internal/version/version.go b/internal/version/version.go index 417b6705..0e4605ea 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.0" +var Fallback = "0.1.1" // 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 f8568b70..7544c126 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.0" { - t.Fatalf("Current() = %q, want 0.1.0", got) + if got != "0.1.1" { + t.Fatalf("Current() = %q, want 0.1.1", got) } } diff --git a/main_test.go b/main_test.go index bcd2f553..b991a8fc 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.0\n" || stderr.Len() != 0 { + if code != 0 || stdout.String() != "codexometer v0.1.1\n" || stderr.Len() != 0 { t.Errorf("flag=%s code=%d stdout=%q stderr=%q", flag, code, stdout.String(), stderr.String()) } }