VOOZH about

URL: https://deepwiki.com/hypervel/prompts/9.1-scrolling-lists

⇱ Scrolling Lists | hypervel/prompts | DeepWiki


Loading...
Last indexed: 7 February 2026 (2e2181)
Menu

Scrolling Lists

Purpose and Scope

The Scrolling trait provides viewport management for prompts that display long lists of options. When the number of options exceeds the visible viewport size, this trait enables keyboard navigation through the list while maintaining a sliding window of visible items.

This trait is used by three interactive prompt types:

  • SelectPrompt - single-selection from a list (see 5.2)
  • MultiSelectPrompt - multi-selection from a list (see 5.2)
  • SearchPrompt - dynamic search with result navigation (see 5.3)

For information about the keyboard input handling that drives navigation, see 10.3. For the visual rendering of scrollbars, see the DrawsScrollbars trait used by renderers (see 8.2).


Core Scrolling Mechanism

The scrolling system maintains two critical indices that work together to provide a viewport into a potentially large list:

PropertyTypePurpose
$highlightedint|nullThe index of the currently focused item in the full list
$firstVisibleintThe index of the first item shown in the current viewport
$scrollintThe maximum number of items visible at once (viewport size)

Viewport Concept

The viewport is a sliding window that shows a subset of the total options. As the user navigates, the viewport automatically adjusts to keep the highlighted item visible.


Example: Viewport Calculation

For a list with 20 options and $scroll = 5:

  • User highlights index 8
  • Viewport shows indices 6-10 (with index 8 highlighted)
  • $firstVisible = 6
  • $highlighted = 8

Sources: src/MultiSelectPrompt.php100 src/SelectPrompt.php98 src/SearchPrompt.php109


Scrolling Trait Interface

The Scrolling trait (located at src/Concerns/Scrolling.php) provides the following interface:

Initialization


Initializes the scrolling state with an optional initial highlighted index. Called in the constructor of prompts using scrolling.

Usage Examples:

Prompt TypeInitializationPurpose
SelectPromptinitializeScrolling(0) or with default matchStart at first item or default value
MultiSelectPromptinitializeScrolling(0)Always start at first item
SearchPromptinitializeScrolling(null)No initial highlight (user is typing)

Sources: src/SelectPrompt.php45-52 src/MultiSelectPrompt.php55 src/SearchPrompt.php44

Navigation Methods


highlightPrevious(int $count, bool $allowWrap = false)

Moves the highlight up by one item. If $allowWrap is false and already at the first item, no change occurs. The viewport automatically scrolls if necessary.

highlightNext(int $count, bool $allowWrap = false)

Moves the highlight down by one item. If $allowWrap is false and already at the last item, no change occurs. The viewport automatically scrolls if necessary.

highlight(int $index)

Directly sets the highlighted index to a specific position. Used for HOME/END key handling to jump to start/end of list.

scrollToHighlighted(int $count)

Adjusts $firstVisible to ensure the currently highlighted item is within the viewport. Used when setting a default value that may be outside the initial viewport.

Sources: src/SelectPrompt.php56-59 src/MultiSelectPrompt.php58-61 src/SearchPrompt.php47-50


Integration with Prompt Types

SelectPrompt - Single Selection Lists


Key Features:

  • Default value handling: If a default is provided, the prompt finds its index, initializes scrolling at that position, and scrolls the viewport to ensure it's visible src/SelectPrompt.php43-50
  • Rich key bindings: Supports arrow keys, VI keys (h/j/k/l), Tab/Shift+Tab, and Ctrl combinations src/SelectPrompt.php56-59
  • Visible options: The visible() method returns the current viewport slice for rendering src/SelectPrompt.php96-99

Sources: src/SelectPrompt.php1-109

MultiSelectPrompt - Multi-Selection Lists


Key Features:

Sources: src/MultiSelectPrompt.php1-153

SearchPrompt - Dynamic Search Results


Key Features:

  • Null initialization: Starts with highlighted = null since the user begins by typing src/SearchPrompt.php44
  • Mode switching: Transitions between typing mode (no highlight) and navigation mode (with highlight) src/SearchPrompt.php52
  • Search resets viewport: When search() is called, both matches and firstVisible are reset src/SearchPrompt.php60-68
  • Wrap navigation: Passes true to highlightPrevious() and highlightNext() to allow wrapping from bottom to top src/SearchPrompt.php47-48
  • Context-aware ENTER: Submits if an item is highlighted, otherwise triggers a search src/SearchPrompt.php51
  • Exit navigation: LEFT/RIGHT arrow keys set highlighted = null, returning to typing mode src/SearchPrompt.php52

Sources: src/SearchPrompt.php1-142


Keyboard Navigation Patterns

All scrolling-enabled prompts share a common set of keyboard navigation bindings:

Standard Navigation Keys

KeyActionUsed By
UP_ARROWMove highlight upAll three prompts
DOWN_ARROWMove highlight downAll three prompts
LEFT_ARROWMove highlight up (SelectPrompt/MultiSelectPrompt) or exit navigation (SearchPrompt)Context-dependent
RIGHT_ARROWMove highlight down (SelectPrompt/MultiSelectPrompt) or exit navigation (SearchPrompt)Context-dependent
TabMove highlight downAll three prompts
Shift+TabMove highlight upAll three prompts
HOMEJump to first itemAll three prompts
ENDJump to last itemAll three prompts

Control Key Shortcuts

KeyActionVim Equivalent
Ctrl+PPrevious itemk (up)
Ctrl+NNext itemj (down)
Ctrl+BPrevious itemh (left)
Ctrl+FNext iteml (right)
Ctrl+AJump to first (Select) / Toggle all (MultiSelect)HOME equivalent
Ctrl+EJump to lastEND equivalent

Vim-Style Navigation (SelectPrompt/MultiSelectPrompt only)

KeyAction
kMove up
jMove down
hMove up
lMove down

Implementation Note: The key handlers use Key::oneOf() to match multiple keys to the same action src/SelectPrompt.php58-59 allowing flexible binding patterns without code duplication.

Sources: src/SelectPrompt.php55-62 src/MultiSelectPrompt.php57-66 src/SearchPrompt.php46-54


Viewport Calculation and Visible Items

Each scrolling prompt provides a visible() method that calculates the current viewport slice:


Viewport Behavior


Key Points:

  • Preserve keys: The preserve_keys: true parameter maintains the original array keys, which is essential for identifying options by their keys rather than viewport-relative positions src/SelectPrompt.php98
  • Consistent interface: All three prompts implement the same visible() signature, allowing renderers to use a uniform approach src/MultiSelectPrompt.php98-101 src/SearchPrompt.php107-110
  • Dynamic for SearchPrompt: In SearchPrompt, visible() slices from matches() rather than options, reflecting the filtered results src/SearchPrompt.php107-110

Sources: src/SelectPrompt.php96-99 src/MultiSelectPrompt.php98-101 src/SearchPrompt.php107-110


Automatic Viewport Adjustment

The scrolling trait automatically adjusts $firstVisible when the highlighted item would move outside the viewport:

Scrolling Scenarios

User ActionCurrent StateResult
Highlight moves downhighlighted >= firstVisible + scrollfirstVisible increments by 1
Highlight moves uphighlighted < firstVisiblefirstVisible decrements by 1
Jump to HOMEhighlighted = 0firstVisible = 0
Jump to ENDhighlighted = count - 1firstVisible = max(0, count - scroll)
Set default (SelectPrompt)Default outside initial viewportscrollToHighlighted() adjusts viewport

Example Flow:

Options: 15 items (indices 0-14)
Scroll: 5 items per viewport
Initial: highlighted=0, firstVisible=0

User presses DOWN 5 times:
 highlighted=1, firstVisible=0 (still in viewport)
 highlighted=2, firstVisible=0
 highlighted=3, firstVisible=0
 highlighted=4, firstVisible=0
 highlighted=5, firstVisible=1 (viewport shifts!)
 
Viewport now shows indices 1-5, with 5 highlighted

This automatic adjustment ensures the user never scrolls the highlight out of view, maintaining a smooth navigation experience.

Sources: Inferred from usage in src/SelectPrompt.php50 src/MultiSelectPrompt.php58-61 src/SearchPrompt.php47-50