From 6d268d512452922a306c1101334091e154503437 Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Thu, 19 Nov 2026 20:50:23 -0600 Subject: [PATCH 1/3] Keep review log tests isolated from installed diff and pager tools The Homebrew Git shim prepends its own tools to PATH, which can make review tests run a real diff or interactive pager. Use Git's own helper search path for the test doubles and bound the wait for stale pager pipes so a regression fails instead of hanging the hook. Change-Id: I398840de109edf93386806fb2605d23850a8c159 --- internal/cli/review/log_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/cli/review/log_test.go b/internal/cli/review/log_test.go index d560b97..4783201 100644 --- a/internal/cli/review/log_test.go +++ b/internal/cli/review/log_test.go @@ -96,7 +96,8 @@ func TestLogDifft(t *testing.T) { tools := t.TempDir() difft := filepath.Join(tools, "difft") require.NoError(t, os.WriteFile(difft, []byte("#!/bin/sh\nprintf 'External diff: %s\\n' \"$1\"\ncat \"$5\"\n"), 0o700)) - t.Setenv("PATH", tools+string(os.PathListSeparator)+os.Getenv("PATH")) + // Git prepends its exec path after launch, even when a wrapper rewrites PATH. + t.Setenv("GIT_EXEC_PATH", tools) out, terminal, err := execute(t, dir, "log", "-p") require.NoError(t, err) @@ -129,7 +130,7 @@ func TestLogHunkPager(t *testing.T) { capture := filepath.Join(tools, "pager-output") require.NoError(t, os.WriteFile(filepath.Join(tools, "hunk"), []byte("#!/bin/sh\n[ \"$1\" = pager ] || exit 1\ncat > \"$HUNK_OUTPUT\"\n"), 0o700)) require.NoError(t, os.WriteFile(filepath.Join(tools, "difft"), []byte("#!/bin/sh\nprintf 'External diff: %s\\n' \"$1\"\ncat \"$5\"\n"), 0o700)) - t.Setenv("PATH", tools+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("GIT_EXEC_PATH", tools) t.Setenv("HUNK_OUTPUT", capture) t.Setenv("TERM", "xterm") @@ -158,6 +159,8 @@ func TestLogHunkPager(t *testing.T) { ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) defer cancel() cmd := exec.CommandContext(ctx, binary, append([]string{"log"}, tc.flags...)...) + // A pager can keep stderr open after cancellation kills Timber. + cmd.WaitDelay = time.Second cmd.Dir = dir cmd.Stdout = slave var stderr bytes.Buffer From 454bc1c399967da0b8a29654c3ee218892fbc050 Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Fri, 20 Nov 2026 14:16:52 -0600 Subject: [PATCH 2/3] Improve quota colors, especially on light themes Fixed colors can be hard to read on light backgrounds. Use the terminal palette and color both bars alike so users can compare usage with elapsed time. Show slightly ahead-of-pace usage in blue and retain the provider status when no clock is available. Change-Id: I5978e92f1577106b03b454d4357c11bbf94bec23 --- go.mod | 1 - go.sum | 2 - internal/quotas/render.go | 101 +++++++++++++-------------------- internal/quotas/render_test.go | 42 ++++++++++++++ 4 files changed, 80 insertions(+), 66 deletions(-) diff --git a/go.mod b/go.mod index 42d6536..da6354b 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,6 @@ require ( github.com/caarlos0/env/v11 v11.4.1 // indirect github.com/catppuccin/go v0.3.0 // indirect github.com/charmbracelet/glamour v1.0.0 // indirect - github.com/charmbracelet/harmonica v0.2.0 // indirect github.com/charmbracelet/keygen v0.5.4 // indirect github.com/charmbracelet/log v0.4.1 // indirect github.com/charmbracelet/ssh v0.0.0-20250128164007-98fd5ae11894 // indirect diff --git a/go.sum b/go.sum index 27b3320..e782e2b 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,6 @@ github.com/charmbracelet/fang v1.0.0 h1:jESBY40agJOlLYnnv9jE0mLqDGTxEk0hkOnx7YGy github.com/charmbracelet/fang v1.0.0/go.mod h1:P5/DNb9DddQ0Z0dbc0P3ol4/ix5Po7Ofr2KMBfAqoCo= github.com/charmbracelet/glamour v1.0.0 h1:AWMLOVFHTsysl4WV8T8QgkQ0s/ZNZo7CiE4WKhk8l08= github.com/charmbracelet/glamour v1.0.0/go.mod h1:DSdohgOBkMr2ZQNhw4LZxSGpx3SvpeujNoXrQyH2hxo= -github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ= -github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao= github.com/charmbracelet/huh v1.0.0 h1:wOnedH8G4qzJbmhftTqrpppyqHakl/zbbNdXIWJyIxw= github.com/charmbracelet/huh v1.0.0/go.mod h1:5YVc+SlZ1IhQALxRPpkGwwEKftN/+OlJlnJYlDRFqN4= github.com/charmbracelet/keygen v0.5.4 h1:XQYgf6UEaTGgQSSmiPpIQ78WfseNQp4Pz8N/c1OsrdA= diff --git a/internal/quotas/render.go b/internal/quotas/render.go index 27a7ce9..0074630 100644 --- a/internal/quotas/render.go +++ b/internal/quotas/render.go @@ -7,7 +7,6 @@ import ( "time" "charm.land/lipgloss/v2" - "github.com/charmbracelet/bubbles/progress" "github.com/charmbracelet/x/ansi" ) @@ -16,13 +15,14 @@ var ( percentStyle = lipgloss.NewStyle().Bold(true) mutedStyle = lipgloss.NewStyle().Faint(true) quietStyle = lipgloss.NewStyle().Faint(true) - criticalStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) + criticalStyle = lipgloss.NewStyle().Foreground(ansi.Red) ruleStyle = lipgloss.NewStyle().Faint(true) + trackStyle = lipgloss.NewStyle().Foreground(ansi.BrightBlack) ) const ( - lagPoints = 5 - lagFar = 15 + paceOKPoints = 10 + paceWarningPoints = 20 // minWidth is the narrowest report that still fits a name and a percent. minWidth = 24 // maxWidth keeps the name, the reset, and the percent on one line. @@ -35,12 +35,6 @@ const ( elapsedEmpty = '─' // elapsedTip sits on the last spent cell and turns up toward the usage bar. elapsedTip = '┛' - - trackColor = "#3A3A3A" - elapsedOnPace = "#E4E4E4" - okBar = "#5FBF6A" - warningBar = "#FFD75F" - criticalBar = "#FF5F5F" ) // Render draws the plans in the order to use them. @@ -103,14 +97,20 @@ func renderLimit(report Report, limit Limit, now time.Time, width int) string { label := limitLabel(report, limit) percentPlain := fmt.Sprintf("%d%%", int(math.Round(limit.Percent))) middle := limitMiddle(limit, now, width-ansi.StringWidth(label)-ansi.StringWidth(percentPlain)) + elapsed, hasClock := shareOf(limit, now) + color := levelColor(limit.Level) + if hasClock { + color = paceColor(limit.Percent - elapsed) + } + fill := lipgloss.NewStyle().Foreground(color) var out strings.Builder out.WriteString(placeWindowLine(label, middle, percentPlain, width)) out.WriteString("\n") - out.WriteString(renderGauge(limit, width)) - if share, points, ok := Lag(limit, now); ok { + out.WriteString(renderGauge(limit.Percent, width, fill)) + if hasClock { out.WriteString("\n") - out.WriteString(renderPace(share, points, width)) + out.WriteString(renderPace(elapsed, width, fill)) } if middle == "" { if meta := renderMeta(limit, now); meta != "" { @@ -220,74 +220,49 @@ func windowClock(limit Limit, now time.Time) string { return text + " " + clock } -func renderGauge(limit Limit, width int) string { - return meter(width, limit.Percent, usageFull, usageEmpty, usageColor(limit.Level)) +func renderGauge(percent float64, width int, fill lipgloss.Style) string { + filled := filledCells(width, percent) + used := fill.Render(strings.Repeat(string(usageFull), filled)) + remaining := trackStyle.Render(strings.Repeat(string(usageEmpty), width-filled)) + return used + remaining } // renderPace draws how far the window has run, on the same width as the usage bar. -// The mark for time still ahead is lighter, so a half-spent month does not look full. // An arrow at the tip of the time already spent points up at the usage. -func renderPace(share int, points float64, width int) string { - return paceBar(width, paceFilled(width, share), paceColor(points)) -} - -func paceFilled(width, share int) int { - filled := int(math.Round(float64(width) * shownPortion(float64(share)))) - return min(max(filled, 0), width) -} - -func paceBar(width, filled int, fill string) string { - if width < 1 { - return "" - } - filled = min(max(filled, 0), width) - if filled == 0 { - filled = 1 - } - spent := paceInk(fill).Render(strings.Repeat(string(elapsedFull), filled-1) + string(elapsedTip)) - ahead := paceInk(trackColor).Render(strings.Repeat(string(elapsedEmpty), width-filled)) +func renderPace(elapsed float64, width int, fill lipgloss.Style) string { + filled := max(1, filledCells(width, elapsed)) + spent := fill.Render(strings.Repeat(string(elapsedFull), filled-1) + string(elapsedTip)) + ahead := trackStyle.Render(strings.Repeat(string(elapsedEmpty), width-filled)) return spent + ahead } -func paceInk(color string) lipgloss.Style { - return lipgloss.NewStyle().Foreground(lipgloss.Color(color)) -} - -func meter(width int, percent float64, full, empty rune, fill string) string { - bar := progress.New( - progress.WithWidth(width), - progress.WithoutPercentage(), - progress.WithFillCharacters(full, empty), - progress.WithSolidFill(fill), - ) - bar.EmptyColor = trackColor - return bar.ViewAs(shownPortion(percent)) -} - -func shownPortion(percent float64) float64 { - return min(max(percent, 0), 100) / 100 +func filledCells(width int, percent float64) int { + portion := min(max(percent, 0), 100) / 100 + return min(max(int(math.Round(float64(width)*portion)), 0), width) } -func usageColor(level Level) string { +func levelColor(level Level) ansi.BasicColor { switch level { case LevelCritical: - return criticalBar + return ansi.Red case LevelWarning: - return warningBar + return ansi.Yellow default: - return okBar + return ansi.Green } } -func paceColor(points float64) string { - gap := int(math.Round(points)) +func paceColor(pointsAhead float64) ansi.BasicColor { + gap := math.Abs(pointsAhead) switch { - case gap >= lagFar: - return criticalBar - case gap >= lagPoints: - return warningBar + case gap > paceWarningPoints: + return ansi.Red + case gap > paceOKPoints: + return ansi.Yellow + case pointsAhead > 0: + return ansi.Blue default: - return elapsedOnPace + return ansi.Green } } diff --git a/internal/quotas/render_test.go b/internal/quotas/render_test.go index c32fcc0..7ea069d 100644 --- a/internal/quotas/render_test.go +++ b/internal/quotas/render_test.go @@ -105,6 +105,48 @@ func TestRenderShowsHowMuchOfEachWindowHasPassed(t *testing.T) { assert.Equal(t, 40, ansi.StringWidth(elapsed[1])) } +func TestRenderUsesTheSamePaceColorForUsageAndElapsed(t *testing.T) { + t.Parallel() + now := time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC) + week := 7 * 24 * time.Hour + cases := []struct { + name string + percent float64 + color string + }{ + {"10 points below", 40, "\x1b[32m"}, + {"exactly on pace", 50, "\x1b[32m"}, + {"just above pace", 50.1, "\x1b[34m"}, + {"10 points above", 60, "\x1b[34m"}, + {"just over 10 points below", 39.9, "\x1b[33m"}, + {"just over 10 points above", 60.1, "\x1b[33m"}, + {"20 points below", 30, "\x1b[33m"}, + {"20 points above", 70, "\x1b[33m"}, + {"just over 20 points below", 29.9, "\x1b[31m"}, + {"just over 20 points above", 70.1, "\x1b[31m"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + limit := Limit{Label: "Week", Percent: tc.percent, Level: LevelCritical, Window: week, ResetsAt: now.Add(week / 2)} + view := Render(Snapshot{Reports: []Report{plan("opencode-go", "OpenCode Go", limit)}}, now, 40) + gauge := lineContaining(view, "█") + elapsed := lineContaining(view, "━") + assert.Contains(t, gauge, tc.color+"█") + assert.Contains(t, elapsed, tc.color+"━") + assert.Contains(t, gauge, "\x1b[90m░") + assert.Contains(t, elapsed, "\x1b[90m─") + }) + } +} + +func TestRenderUsesLimitStatusWhenThereIsNoClock(t *testing.T) { + t.Parallel() + limit := Limit{Label: "Credits", Percent: 12, Level: LevelWarning} + view := Render(Snapshot{Reports: []Report{plan("example", "Example", limit)}}, time.Time{}, 40) + assert.Contains(t, lineContaining(view, "█"), "\x1b[33m█") + assert.NotContains(t, view, "┛") +} + func linesContaining(view, mark string) []string { var found []string for line := range strings.SplitSeq(view, "\n") { From e71b219fd2dd6f14bb024e3168e369041cec14b4 Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Fri, 20 Nov 2026 14:17:21 -0600 Subject: [PATCH 3/3] Refactor: Remove unused quota clock helpers The quota renderer now compares unrounded percentages directly. Remove the unused helpers so there is only one way to compute the elapsed share. Change-Id: I68766c131cd8714b8efcd792ff8319d0046b9143 --- internal/quotas/clock.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/internal/quotas/clock.go b/internal/quotas/clock.go index cbffdc8..d26e259 100644 --- a/internal/quotas/clock.go +++ b/internal/quotas/clock.go @@ -25,26 +25,6 @@ func Lit(percent float64) int { return gaugeCells } -// WindowShare is how far a limit is into its window, in percent. -// The second result is false when the service gives no clock. -func WindowShare(limit Limit, now time.Time) (int, bool) { - share, ok := shareOf(limit, now) - if !ok { - return 0, false - } - return int(math.Round(share)), true -} - -// Lag is how far a limit is behind its window: the share that has passed, -// and the points use is behind that share. -func Lag(limit Limit, now time.Time) (share int, points float64, ok bool) { - share, ok = WindowShare(limit, now) - if !ok { - return 0, 0, false - } - return share, float64(share) - limit.Percent, true -} - // UntilReset is "in 2d 4h", "in 3h 10m", "in 12m", or "now". func UntilReset(reset time.Time, now time.Time) (string, bool) { if reset.IsZero() {