Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 5 additions & 2 deletions internal/cli/review/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions internal/quotas/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
101 changes: 38 additions & 63 deletions internal/quotas/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"charm.land/lipgloss/v2"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/x/ansi"
)

Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
}
}

Expand Down
42 changes: 42 additions & 0 deletions internal/quotas/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Loading