Skip to content

feat: Arrow-Key Menu Navigation - #10

Merged
sahil-noon merged 3 commits into
mainfrom
260516-dkg7-arrow-key-menu-navigation
May 17, 2026
Merged

sahil-noon merged 3 commits into
mainfrom
260516-dkg7-arrow-key-menu-navigation

Conversation

@sahil-noon

Copy link
Copy Markdown
Collaborator

Meta

ID Type Confidence Plan Review
dkg7 feat 3.2/5.0 18/18 tasks, 39/39 acceptance ✓ ✓ 1 cycle

Pipeline: intake ✓ → spec ✓ → apply ✓ → review ✓ → hydrate ✓ → ship → review-pr

Impact: +1223/−6 code (excluding fab/, docs/) · +2089/−7 total

Summary

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 to ShowMenu while keeping the numbered prompt as the non-TTY fallback, so scripts, CI, and --non-interactive callers see byte-identical behavior.

Changes

  • ShowMenu gets a TTY-aware interactive path (↑/↓/k/j to navigate, Enter to confirm, Esc/Ctrl-C/q to cancel, 0–9 for direct numeric selection)
  • Interactive rendering uses ANSI in-place redraw with reverse-video highlight and a › gutter marker; the existing (default) green marker is preserved
  • Scope is ShowMenu only — ConfirmYesNo and PromptWithDefault are unchanged; public signature ShowMenu(prompt, options, defaultIdx) is preserved so all ~11 call sites pick up the new behavior with no edits
  • Library choice: hand-rolled on golang.org/x/term (single new dep, no transitive bloat) per Constitution Principle I
  • Tests cover the pure state machine (nextMenuState) and escape parser (parseKey) exhaustively, plus a panic-restore test that verifies term.Restore runs before unwind

sahil87 added 2 commits May 17, 2026 01:39
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: ColorReset clears reverse video before Cancel is 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

  • finalizeMenu also writes while the terminal is still in raw mode, so the plain \n in 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.

Comment on lines +374 to +392
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
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines +683 to +688
if isHighlighted {
fmt.Fprintf(w, "%s %s%s%d)%s %s%s%s\n",
gutter,
ansiReverseVideo, ColorBold, num, ColorReset,
label, ansiResetSGR,
defaultMarker)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — corrected the documented signature to initialHighlight(defaultIdx, numOptions) to match the implementation. (c176838)

Comment on lines +629 to +638
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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants