diff --git a/assets/cathode-question.svg b/assets/cathode-question.svg index 693f556..62142b4 100644 --- a/assets/cathode-question.svg +++ b/assets/cathode-question.svg @@ -1,5 +1,5 @@ - - + + ╔══════════════════════════════════════════════════════════════════════════════════════════╗ @@ -33,71 +33,80 @@ ║ - refactor add() to take a third arg and update the caller +╔════════════════════════════════════════════════════════════════════════════════════════╗ + + + +║ + How should add() take the third argument? +║ ▪ -╔════════════════════════════════════════════════════════════════════════════════════════╗ +║ + › +f +ilter… +║ - - - + + + H ║ - How should add() take the third argument? + Required positional +│ ║ + + + ║ - › -f -ilter… + add(a, b, c int) — update every caller +│ ║ - - - ╔ ║ - Required positional add(a, b, c int) — update every caller + Variadic │ ║ ║ ║ - Variadic -add(a int, rest ...int) — sum any number of args + add(a int, rest ...int) — sum any number of args │ ║ ║ ║ - Options struct -add(Args{...}) — future-proof, more verbose + Options struct │ ║ ║ ║ - [↑↓] move [enter] choose [esc] cancel + add(Args{...}) — future-proof, more verbose +│ ║ ║ -╚════════════════════════════════════════════════════════════════════════════════════════╝ +║ + [↑↓] move [enter] choose [esc] cancel +║ ║ - 2 │ -- return a + b -║ +╚════════════════════════════════════════════════════════════════════════════════════════╝ ║ @@ -133,26 +142,22 @@ - - - - - - - - - + + + + + + + + + - - - - - + + + - - - - + + MDL sonnet @@ -163,19 +168,16 @@ N0D3 a1b2c3d4 ▪ -BR -main - ▪ -CTX -24.0K -░░░░░░░░ -12% - ▪ -OUT -1.2K +CTX +24.0K +░░░░░░░░ +12% + ▪ +OUT +1.2K + ▪ +$0.0042 ▪ -$0.0042 - ▪ -[█▒▒▒] W0RK1NG +[█▒▒▒] W0RK1NG \ No newline at end of file diff --git a/picker.go b/picker.go index 077d9bd..3ad3a39 100644 --- a/picker.go +++ b/picker.go @@ -12,7 +12,6 @@ import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/charmbracelet/x/ansi" ) // fuzzyScore returns (score, matched). A higher score is better. Subsequence @@ -82,6 +81,10 @@ type picker struct { // the title is the part you are reading the list for. twoLine bool + // wrap shows the whole subtitle, wrapped onto rows under the title, and + // lets the box grow to the terminal height — see pickerwrap.go. + wrap bool + // Checklist mode — see pickermulti.go. multi bool marked map[int]bool // marked item indices @@ -218,7 +221,7 @@ func (p *picker) View() string { if maxRows < 4 { maxRows = 4 } - if maxRows > 16 { + if maxRows > 16 && !p.wrap { maxRows = 16 } // maxRows is screen rows; the window is measured in ITEMS. A two-line item @@ -242,7 +245,11 @@ func (p *picker) View() string { } section := cDim.Render(" (no matches)") - if len(p.filtered) > 0 { + if len(p.filtered) > 0 && p.wrap { + rows, start, end := p.wrapRows(listW, maxRows) + bar := bbsScrollbar(len(rows), len(p.filtered), end-start, start, true) + section = lipgloss.JoinHorizontal(lipgloss.Top, strings.Join(rows, "\n"), bar) + } else if len(p.filtered) > 0 { start := 0 if p.cursor >= maxItems { start = p.cursor - maxItems + 1 @@ -251,18 +258,7 @@ func (p *picker) View() string { if end > len(p.filtered) { end = len(p.filtered) } - // ANSI-aware truncate + space-pad to a fixed width so the scrollbar - // lands as a straight column regardless of styled content. - // "…" rather than a bare cut: a row trimmed to fit should say so, or a - // truncated title reads as the whole of a shorter one. - pad := func(line string, w int) string { - line = ansi.Truncate(line, w, "…") - if n := w - lipgloss.Width(line); n > 0 { - line += strings.Repeat(" ", n) - } - return line - } - fit := func(line string) string { return pad(line, listW) } + fit := func(line string) string { return padRow(line, listW) } rows := make([]string, 0, (end-start)*2) for i := start; i < end; i++ { idx := p.filtered[i] @@ -296,8 +292,8 @@ func (p *picker) View() string { if inner < 1 { inner = 1 } - rows = append(rows, approveBar.Render(pad(head, inner))) - rows = append(rows, approveBar.Render(pad(sub, inner))) + rows = append(rows, approveBar.Render(padRow(head, inner))) + rows = append(rows, approveBar.Render(padRow(sub, inner))) continue } rows = append(rows, fit(head), fit(cDim.Render(sub))) diff --git a/pickerwrap.go b/pickerwrap.go new file mode 100644 index 0000000..5143afe --- /dev/null +++ b/pickerwrap.go @@ -0,0 +1,103 @@ +// Copyright 2026 Triple Down AB +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "strings" + + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" +) + +// Wrap mode for the picker. +// +// A question's options carry a description that explains the choice, and a +// one-line row cut it off at the box edge, which left the user choosing +// without the explanation. In wrap mode the title has its own row and the +// description wraps onto as many rows under it as it needs. Items therefore +// differ in height, so the visible window is measured in rows, not items. + +// wrapIndent is how far a description sits in from the box edge, under its title. +const wrapIndent = " " + +// wrapItemLines is one item as unstyled lines: the title, then the wrapped +// description. Each line fits in width cells. +func (p *picker) wrapItemLines(idx, width int) []string { + it := p.items[idx] + lines := []string{" " + p.mark(idx) + it.title} + textW := width - len(wrapIndent) + if textW < 1 || strings.TrimSpace(it.subtitle) == "" { + return lines + } + for _, l := range strings.Split(ansi.Wrap(it.subtitle, textW, ""), "\n") { + lines = append(lines, wrapIndent+l) + } + return lines +} + +// wrapWindow picks the items to draw so the cursor item is visible and the +// drawn rows fit in maxRows. It walks back from the cursor as far as the rows +// allow, then fills forward. heights holds the row count of each filtered item. +func wrapWindow(heights []int, cursor, maxRows int) (start, end int) { + start, used := cursor, heights[cursor] + for start > 0 && used+heights[start-1] <= maxRows { + start-- + used += heights[start] + } + end = cursor + 1 + for end < len(heights) && used+heights[end] <= maxRows { + used += heights[end] + end++ + } + return start, end +} + +// wrapRows renders the visible items in wrap mode, each line padded to listW. +// It returns the rows and the window of filtered items they show. +func (p *picker) wrapRows(listW, maxRows int) (rows []string, start, end int) { + // The selected item renders inside the lightbar's padding, so it wraps to + // the narrower width. Wrap every item to that width, so an item keeps the + // same height when the cursor moves onto it and the window does not jump. + inner := listW - approveBar.GetHorizontalFrameSize() + if inner < 1 { + inner = 1 + } + items := make([][]string, len(p.filtered)) + heights := make([]int, len(p.filtered)) + for i, idx := range p.filtered { + items[i] = p.wrapItemLines(idx, inner) + heights[i] = len(items[i]) + } + start, end = wrapWindow(heights, p.cursor, maxRows) + for i := start; i < end; i++ { + lines := items[i] + // One item taller than the whole window is cut, not drawn past the box. + if len(lines) > maxRows { + lines = append(lines[:maxRows-1:maxRows-1], lines[maxRows-1]+"…") + } + for j, l := range lines { + switch { + case i == p.cursor: + rows = append(rows, approveBar.Render(padRow(l, inner))) + case j == 0: + rows = append(rows, padRow(l, listW)) + default: + rows = append(rows, padRow(cDim.Render(l), listW)) + } + } + } + return rows, start, end +} + +// padRow truncates line to w cells and pads it with spaces to exactly w, so +// the scrollbar lands as a straight column whatever the styled content is. +// "…" rather than a bare cut: a row trimmed to fit should say so, or a +// truncated title reads as the whole of a shorter one. +func padRow(line string, w int) string { + line = ansi.Truncate(line, w, "…") + if n := w - lipgloss.Width(line); n > 0 { + line += strings.Repeat(" ", n) + } + return line +} diff --git a/pickerwrap_test.go b/pickerwrap_test.go new file mode 100644 index 0000000..1ed8942 --- /dev/null +++ b/pickerwrap_test.go @@ -0,0 +1,90 @@ +// Copyright 2026 Triple Down AB +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "strings" + "testing" +) + +// longDescription is wider than any box the picker draws, so a one-line row +// has to cut it. Its last word is the part a cut loses. +const longDescription = "Keep the current schema and add a nullable column, " + + "backfill it in batches overnight, then flip the default once every row " + + "has a value and the old readers are gone. ENDMARK" + +func wrapPicker(cursor, w, h int) *picker { + items := []pickerItem{ + {id: "a", title: "Migrate in place", subtitle: longDescription}, + {id: "b", title: "New table", subtitle: longDescription}, + {id: "c", title: "Do nothing", subtitle: "short"}, + } + p := newPicker("question", "Which migration?", items, w, h) + p.wrap = true + p.cursor = cursor + return p +} + +// A wrapped description is shown whole, for the selected row and the others. +func TestWrapPickerShowsTheWholeDescription(t *testing.T) { + for cursor := 0; cursor < 3; cursor++ { + body := stripANSI(wrapPicker(cursor, 64, 40).View()) + if got := strings.Count(body, "ENDMARK"); got != 2 { + t.Errorf("cursor %d: the description end is shown %d times, want 2:\n%s", + cursor, got, body) + } + } +} + +// No line may wrap a second time inside the box. The box pads every line to +// one width, so only a line count shows the break (see the two-line test). +func TestWrapPickerRowsDoNotWrap(t *testing.T) { + p := wrapPicker(1, 64, 40) + inner := 64 - 4 - 2 - 1 - approveBar.GetHorizontalFrameSize() + want := 2 + 2 + 1 // border, title and filter input, footer + for _, idx := range p.filtered { + want += len(p.wrapItemLines(idx, inner)) + } + if got := len(strings.Split(stripANSI(p.View()), "\n")); got != want { + t.Errorf("rendered %d lines, want %d — a row wrapped:\n%s", + got, want, stripANSI(p.View())) + } +} + +// On a short terminal the window keeps the cursor item in view and draws no +// more rows than fit. +func TestWrapPickerWindowFitsTheTerminal(t *testing.T) { + const h = 16 + for cursor := 0; cursor < 3; cursor++ { + p := wrapPicker(cursor, 64, h) + body := stripANSI(p.View()) + if got := len(strings.Split(body, "\n")); got > h { + t.Errorf("cursor %d: %d lines on a %d-row terminal:\n%s", cursor, got, h, body) + } + title := p.items[p.filtered[cursor]].title + if !strings.Contains(body, title) { + t.Errorf("cursor %d: the selected item %q is not drawn:\n%s", cursor, title, body) + } + } +} + +func TestWrapWindow(t *testing.T) { + cases := []struct { + heights []int + cursor, max int + wantStart, wantEnd int + }{ + {[]int{3, 3, 3}, 0, 10, 0, 3}, // everything fits + {[]int{3, 3, 3}, 0, 6, 0, 2}, // fills forward from the top + {[]int{3, 3, 3}, 2, 6, 1, 3}, // walks back from the cursor + {[]int{9, 1}, 0, 4, 0, 1}, // one tall item still shows + } + for _, c := range cases { + s, e := wrapWindow(c.heights, c.cursor, c.max) + if s != c.wantStart || e != c.wantEnd { + t.Errorf("wrapWindow(%v, %d, %d) = %d,%d, want %d,%d", + c.heights, c.cursor, c.max, s, e, c.wantStart, c.wantEnd) + } + } +} diff --git a/question.go b/question.go index 0340939..1ab96f8 100644 --- a/question.go +++ b/question.go @@ -92,6 +92,8 @@ func (q *pendingQuestion) picker(w, h int) *picker { title = fmt.Sprintf("(%d/%d) %s", q.idx+1, n, cur.Question) } p := newPicker("question", title, items, w, h) + // A description explains the choice, so it is shown whole, not cut to a row. + p.wrap = true // Claude asked for several answers, so mark rows instead of picking one. if cur.MultiSelect { p.setMulti()