Skip to content

Fix up-arrow command history requiring 2 keystrokes per entry#206

Merged
PureWeen merged 3 commits intomainfrom
fix/when-using-up-arrow-to-list-preview-comm-20260224-2002
Feb 26, 2026
Merged

Fix up-arrow command history requiring 2 keystrokes per entry#206
PureWeen merged 3 commits intomainfrom
fix/when-using-up-arrow-to-list-preview-comm-20260224-2002

Conversation

@StephaneDelcroix
Copy link
Collaborator

Problem

When using up-arrow to navigate command history, it takes 2 keystrokes per command instead of 1.

Root Cause

After JsNavigateHistory sets the input text, the cursor was always placed at the end of the input (el.value.length). However, the ArrowUp handler only fires when the cursor is at position 0 (atStart). So the sequence was:

  1. 1st ArrowUp: cursor at 0 → handler fires → text set, cursor moves to end
  2. 2nd ArrowUp: cursor at end → atStart is false → handler doesn't fire → default textarea behavior moves cursor to start (wasted keystroke)
  3. 3rd ArrowUp: cursor at 0 → handler fires → next history entry

Fix

Place cursor at position 0 when navigating up (so next ArrowUp fires immediately), and at end when navigating down (so next ArrowDown fires immediately).

Also extracted CommandHistory model class from inline Dashboard.razor logic for testability.

Tests

Added CommandHistoryTests.cs with 10 tests covering:

  • Empty history returns null
  • Up navigation returns CursorAtStart = true
  • Down navigation returns CursorAtStart = false
  • Sequential up-presses cycle through all entries
  • Up-then-down navigation
  • Add resets index
  • Duplicate deduplication
  • Empty/null ignored
  • Max entries enforcement
  • Past-end returns empty string

StephaneDelcroix and others added 2 commits February 25, 2026 17:51
After navigating history with ArrowUp, the cursor was placed at the end
of the input. But the ArrowUp handler only fires when the cursor is at
position 0 (start). So the second ArrowUp just moved the cursor from
end to start (default textarea behavior), wasting a keystroke.

Fix: place cursor at start when navigating up, and at end when
navigating down, so the next same-direction press fires immediately.

Extract CommandHistory model class for testability.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When navigating up through history the Blazor handler sets cursor to
position 0. The JS atEnd guard then blocked ArrowDown (cursor not at
end), requiring 2 keystrokes per down-step — symmetric to the original
bug.

Fix:
- Add CommandHistory.IsNavigating (true while _index < entries.Count)
- JsNavigateHistory now returns bool indicating whether navigation is
  still active
- JS keydown handler tracks per-session nav state in window.__histNavActive
  via .then() on the async invoke; ArrowDown fires when atEnd OR histNavActive

Also adds IsNavigating unit test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen PureWeen force-pushed the fix/when-using-up-arrow-to-list-preview-comm-20260224-2002 branch from 6f6eed2 to 7f0cf84 Compare February 25, 2026 23:56
- Add delegated 'input' event listener inside __dashboardKeydownRegistered
  guard that sets window.__histNavActive[session] = false when user types,
  preventing ArrowDown from overwriting in-progress edits after ArrowUp
- Add .catch() to JsNavigateHistory Promise so a failed interop round-trip
  resets the flag instead of leaving it stuck true

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen PureWeen merged commit c3c1fad into main Feb 26, 2026
@PureWeen PureWeen deleted the fix/when-using-up-arrow-to-list-preview-comm-20260224-2002 branch February 26, 2026 00:51
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.

2 participants