Repository navigation
feat: Arrow-Key Menu Navigation - #10
Conversation
ShowMenu now detects when stdin and stdout are both TTYs and renders an interactive selector supporting Up/Down (and j/k), Enter, Esc/Ctrl-C/q to cancel, and direct number keys (0-9). Non-TTY input (piped stdin, CI, --non-interactive) falls back to the existing numbered prompt with byte-identical output to preserve scripted callers and integration tests. Implementation is hand-rolled on golang.org/x/term (no charm/survey dependencies) per Constitution Principle I. Pure state machine (nextMenuState) and escape parser (parseKey) are separated from the raw-mode I/O shell so the bulk of the logic is unit-tested without a pty. term.Restore is deferred to guarantee cooked-mode restore on submit, cancel, or panic. Public signature of ShowMenu is unchanged; all ~11 call sites in wt open / wt delete / wt create pick up the new behavior automatically.
There was a problem hiding this comment.
Pull request overview
Adds TTY-aware arrow-key navigation to the shared ShowMenu workflow while preserving the numbered prompt fallback for non-TTY/scripted usage.
Changes:
- Adds an interactive raw-terminal menu path with key parsing, state-machine navigation, in-place redraw, and cancel handling.
- Adds unit coverage for state transitions, key parsing, fallback behavior, restore-on-panic, and render consistency.
- Documents the new menu-navigation contract and adds
golang.org/x/term.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/internal/worktree/menu.go |
Implements TTY detection, interactive menu rendering, key parsing, and fallback delegation. |
src/internal/worktree/menu_test.go |
Adds tests for menu state, key parsing, fallback output, panic restore, and render sharing. |
src/go.mod |
Adds golang.org/x/term and updates the Go directive/dependency blocks. |
src/go.sum |
Adds checksums for golang.org/x/term and golang.org/x/sys. |
fab/changes/260516-dkg7-arrow-key-menu-navigation/spec.md |
Adds the feature specification. |
fab/changes/260516-dkg7-arrow-key-menu-navigation/plan.md |
Adds the implementation plan and acceptance checklist. |
fab/changes/260516-dkg7-arrow-key-menu-navigation/intake.md |
Adds intake rationale and scope. |
fab/changes/260516-dkg7-arrow-key-menu-navigation/.status.yaml |
Adds change-stage metadata. |
fab/changes/260516-dkg7-arrow-key-menu-navigation/.history.jsonl |
Adds change history events. |
docs/memory/wt-cli/menu-navigation-contract.md |
Documents the post-implementation behavior contract. |
docs/memory/index.md |
Links the new menu-navigation memory document. |
Comments suppressed due to low confidence (2)
src/internal/worktree/menu.go:711
- The highlighted Cancel row has the same reset-before-label issue:
ColorResetclears reverse video beforeCancelis written, leaving only the number highlighted. Keep the reverse-video styling active across the label so the selected row is visually clear.
if isHighlighted {
fmt.Fprintf(w, "%s %s%s0)%s Cancel%s%s\n",
gutter,
ansiReverseVideo, ColorBold, ColorReset,
ansiResetSGR,
defaultMarker)
src/internal/worktree/menu.go:665
finalizeMenualso writes while the terminal is still in raw mode, so the plain\nin the clear loop and summary line may not return the cursor to column 0. Use raw-mode-safe line endings (\r\n) or restore cooked mode before emitting the final line to avoid leaving the shell prompt/output misaligned after a selection or cancel.
for i := 0; i < rowsRendered; i++ {
fmt.Fprint(w, ansiClearLine)
if i < rowsRendered-1 {
fmt.Fprintln(w)
}
}
// After the loop the cursor is on the last cleared line; move back to
// the top and emit the single summary line.
fmt.Fprint(w, ansiCarriageRet)
fmt.Fprintf(w, ansiCursorUpFmt, rowsRendered-1)
fmt.Fprint(w, ansiClearLine)
if choice == 0 {
fmt.Fprintf(w, "%s (cancelled)\n", prompt)
return
}
// choice in 1..len(options)
fmt.Fprintf(w, "%s %s\n", prompt, options[choice-1])
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func (b *blockingByteReader) readByteWithin(timeout time.Duration) (byte, bool) { | ||
| type readResult struct { | ||
| bt byte | ||
| err error | ||
| } | ||
| ch := make(chan readResult, 1) | ||
| go func() { | ||
| bt, err := b.src.ReadByte() | ||
| ch <- readResult{bt: bt, err: err} | ||
| }() | ||
| select { | ||
| case r := <-ch: | ||
| if r.err != nil { | ||
| return 0, false | ||
| } | ||
| return r.bt, true | ||
| case <-time.After(timeout): | ||
| return 0, false | ||
| } |
There was a problem hiding this comment.
Fixed — replaced per-call goroutine with a single byte-pump goroutine that forwards bytes via a buffered channel. Both readByteBlocking and readByteWithin now consume from the same channel, so there is never more than one outstanding read on the underlying bufio.Reader. Pending bytes after a bare-Esc timeout are delivered to the next call rather than racing a new reader. (c176838)
| if isHighlighted { | ||
| fmt.Fprintf(w, "%s %s%s%d)%s %s%s%s\n", | ||
| gutter, | ||
| ansiReverseVideo, ColorBold, num, ColorReset, | ||
| label, ansiResetSGR, | ||
| defaultMarker) |
There was a problem hiding this comment.
Fixed — kept reverse video active across the label on the highlighted row. The number now ends with \x1b[22m (bold off) instead of \x1b[0m so reverse video persists; the full SGR reset happens once after the label. Applied to both the option row (line 688) and the Cancel row (line 706). (c176838)
| - `defaultIdx == 0` → the renderer pre-highlights the `Cancel` row. | ||
| - `defaultIdx == -1` (no default) → the renderer pre-highlights row 1. | ||
| - The existing green `(default)` marker is still rendered on the `defaultIdx` row, visually distinct from the moving reverse-video highlight. A row may carry both at once (default row when highlighted = green marker + reverse video + `›` gutter). | ||
| - The seeding rule lives in `initialHighlight(numOptions, defaultIdx)` and is exercised by `nextMenuState` seeding tests — it is verified without opening a real terminal. |
There was a problem hiding this comment.
Fixed — corrected the documented signature to initialHighlight(defaultIdx, numOptions) to match the implementation. (c176838)
| func renderRows(w io.Writer, prompt string, options []string, st menuState, linePrefix string) { | ||
| fmt.Fprint(w, linePrefix) | ||
| fmt.Fprintln(w, prompt) | ||
| for i, opt := range options { | ||
| row := i + 1 | ||
| fmt.Fprint(w, linePrefix) | ||
| writeOptionRow(w, row, opt, row == st.defaultIdx, row == st.highlight) | ||
| } | ||
| fmt.Fprint(w, linePrefix) | ||
| writeCancelRow(w, st.defaultIdx == 0, st.highlight == 0) |
There was a problem hiding this comment.
Fixed — switched all interactive-mode line terminators (renderRows, writeOptionRow, writeCancelRow, finalizeMenu) from \n to \r\n so rows align at column 0 while the terminal is in raw mode (ONLCR disabled). The fallback path is untouched — it still runs in cooked mode and uses plain \n. (c176838)
Meta
Pipeline: intake ✓ → spec ✓ → apply ✓ → review ✓ → hydrate ✓ → ship → review-pr
Impact: +1223/−6 code (excluding
fab/,docs/) · +2089/−7 totalSummary
Worktree-selection menus (
wt open,wt delete) currently force the user to scan a numbered list and type a digit — clumsy for the 5–20 row lists this CLI routinely produces and out of step with modern peers (gh, gum, fzf). This change adds arrow-key navigation toShowMenuwhile keeping the numbered prompt as the non-TTY fallback, so scripts, CI, and--non-interactivecallers see byte-identical behavior.Changes
ShowMenugets a TTY-aware interactive path (↑/↓/k/j to navigate, Enter to confirm, Esc/Ctrl-C/q to cancel, 0–9 for direct numeric selection)›gutter marker; the existing(default)green marker is preservedShowMenuonly —ConfirmYesNoandPromptWithDefaultare unchanged; public signatureShowMenu(prompt, options, defaultIdx)is preserved so all ~11 call sites pick up the new behavior with no editsgolang.org/x/term(single new dep, no transitive bloat) per Constitution Principle InextMenuState) and escape parser (parseKey) exhaustively, plus a panic-restore test that verifiesterm.Restoreruns before unwind