Skip to content

Conversation

Han5991
Copy link
Contributor

@Han5991 Han5991 commented Oct 9, 2025

Description

Fixes #59938

When multiline REPL input exceeds the terminal height, navigating the cursor with arrow
keys would move the cursor position correctly but fail to refresh the screen properly,
leaving stale content visible on the screen.

Problem

In the REPL, when a multiline input has more lines than the terminal height:

  • Pressing UP arrow to navigate to lines beyond the viewport boundary
  • The cursor position moves correctly
  • But the screen doesn't refresh - old content remains visible
  • This makes it appear as if the cursor "stops" at the top visible line

Solution

This PR implements viewport-based rendering in kRefreshLine() that displays only the
visible portion of input centered around the cursor position when content exceeds
terminal height:

  • Near top: Show from beginning
  • Near bottom: Show last N lines
  • In middle: Center viewport around cursor

Changes

  • Added rows getter to Interface class for terminal height detection
  • Imported MathMin primitive for viewport calculations
  • Modified kRefreshLine() to calculate visible line range based on:
    • Terminal height (this.rows)
    • Current cursor position
    • Total number of lines
  • Adjusted prevRows calculation to account for viewport position

Demo

Before (Bug):

[Your video showing the bug - cursor moves but screen doesn't refresh]

After (Fixed):

[Your video showing the fix - screen properly refreshes during navigation]

Testing

2025-10-09.10.55.16.mov

Added regression test in test/parallel/test-repl-multiline-navigation.js that:

  • Creates a 27-line input on a simulated 20-row terminal
  • Verifies cursor navigation works correctly across viewport boundaries
  • Ensures cursor position consistency when navigating up/down

Run tests:

./node test/parallel/test-repl-multiline-navigation.js
make test-repl

@Han5991 Han5991 marked this pull request as ready for review October 9, 2025 02:02
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. labels Oct 9, 2025
@Han5991 Han5991 force-pushed the fix-repl-cursor-multiline-overflow branch from 38d53d6 to 2508b06 Compare October 9, 2025 02:12
Copy link

codecov bot commented Oct 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.56%. Comparing base (bfc81ca) to head (315a793).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #60171      +/-   ##
==========================================
+ Coverage   88.55%   88.56%   +0.01%     
==========================================
  Files         704      704              
  Lines      208087   208120      +33     
  Branches    40019    40022       +3     
==========================================
+ Hits       184266   184318      +52     
+ Misses      15818    15808      -10     
+ Partials     8003     7994       -9     
Files with missing lines Coverage Δ
lib/internal/readline/interface.js 97.38% <100.00%> (+0.05%) ⬆️

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add test for cursor position consistency when REPL multiline input
exceeds terminal height. Tests navigation through a 27-line input on
a 20-row terminal to ensure cursor moves correctly across viewport
boundaries
When multiline REPL input exceeds the terminal height, navigating
the cursor would move the cursor position correctly but fail to
refresh the screen properly, leaving stale content visible.

This implements viewport-based rendering in kRefreshLine() that
displays only the visible portion of input centered around the
cursor position when content exceeds terminal height.

Fixes: nodejs#59938
@Han5991 Han5991 force-pushed the fix-repl-cursor-multiline-overflow branch from 2508b06 to 315a793 Compare October 9, 2025 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REPL cursor in big input 'stops' at top visible line

2 participants