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
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -171,13 +174,13 @@ 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
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
87 changes: 75 additions & 12 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,14 +105,18 @@ type footerButtonFlashExpiredMsg struct {
sequence uint64
}

type styleTabFlashExpiredMsg struct {
style meterStyleID
sequence uint64
}

const footerButtonFlashDuration = 150 * time.Millisecond

type footerButtonID int

const (
footerButtonNone footerButtonID = iota
footerButtonTheme
footerButtonStyle
footerButtonRefresh
footerButtonQuit
footerButtonStopwatchGo
Expand All @@ -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]"},
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -377,6 +417,29 @@ func footerButtonLayout(width int) ([]footerButton, string) {
return buttons, separator
}

func footerButtonLayoutWithTheme(width int, themeName string) ([]footerButton, string) {
themeWidth := len("THEME // ") + len(themeName)
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 {
return func() tea.Msg {
snapshot, err := m.fetcher.Fetch(context.Background())
Expand Down
59 changes: 39 additions & 20 deletions internal/ui/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -287,13 +287,32 @@ 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)
}
}
}

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)
Expand All @@ -310,7 +329,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
Expand Down
Loading
Loading