Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ class Interface extends InterfaceConstructor {
return Infinity;
}

get rows() {
if (this.output?.rows) return this.output.rows;
return Infinity;
}

/**
* Sets the prompt written to the output.
* @param {string} prompt
Expand Down Expand Up @@ -496,6 +501,7 @@ class Interface extends InterfaceConstructor {
const dispPos = this[kGetDisplayPos](line);
const lineCols = dispPos.cols;
const lineRows = dispPos.rows;
const terminalRows = this.rows;

// cursor position
const cursorPos = this.getCursorPos();
Expand All @@ -513,11 +519,32 @@ class Interface extends InterfaceConstructor {

if (this[kIsMultiline]) {
const lines = StringPrototypeSplit(this.line, '\n');
// Write first line with normal prompt
this[kWriteToOutput](this[kPrompt] + lines[0]);

// Normal case - display line from the first row to the last
let startLine = 0;
let endLine = lineRows;

const exceedsTerminal = lineRows >= terminalRows && terminalRows !== Infinity;
if (exceedsTerminal) {
const topOfVisibleData = lineRows - terminalRows;
// The cursor starts from the last row. If it is within the
// visible data segment, show the last part of the multiline data
if (cursorPos.rows > topOfVisibleData) {
startLine = topOfVisibleData;
} else {
// Cursor has traveled through the visible rows
// Start displaying the data that the cursor is on
// up until the terminal size allows
startLine = cursorPos.rows;
endLine = cursorPos.rows + terminalRows - 1;
}
}
// If the first row is visible, show the normal prompt
if (startLine === 0) {
this[kWriteToOutput](this[kPrompt] + lines[0]);
startLine++;
}
// For continuation lines, add the "|" prefix
for (let i = 1; i < lines.length; i++) {
for (let i = startLine; i <= endLine; i++) {
this[kWriteToOutput](`\n${kMultilinePrompt.description}` + lines[i]);
}
} else {
Expand Down
Loading