Skip to content
Open
Show file tree
Hide file tree
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
86 changes: 49 additions & 37 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,48 +549,60 @@ class Interface extends InterfaceConstructor {
// cursor position
const cursorPos = this.getCursorPos();

// First move to the bottom of the current line, based on cursor pos
const prevRows = this.prevRows || 0;
if (prevRows > 0) {
moveCursor(this.output, 0, -prevRows);
}

// Cursor to left edge.
cursorTo(this.output, 0);
// erase data
clearScreenDown(this.output);

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

// For continuation lines, add the "|" prefix
for (let i = 1; i < lines.length; i++) {
this[kWriteToOutput](`\n${kMultilinePrompt.description}` +
(this[kColorize]?.(lines[i]) ?? lines[i]));
}
} else {
// Write the prompt and the current buffer content.
this[kWriteToOutput](this[kPrompt] +
(this[kColorize]?.(this.line) ?? this.line));
const output = this.output;
const corked = typeof output?.cork === 'function' &&
typeof output?.uncork === 'function';
if (corked) {
output.cork();
}
try {
// First move to the bottom of the current line, based on cursor pos
const prevRows = this.prevRows || 0;
if (prevRows > 0) {
moveCursor(output, 0, -prevRows);
}

// Force terminal to allocate a new line
if (lineCols === 0) {
this[kWriteToOutput](' ');
}
// Cursor to left edge.
cursorTo(output, 0);
// erase data
clearScreenDown(output);

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

// For continuation lines, add the "|" prefix
for (let i = 1; i < lines.length; i++) {
this[kWriteToOutput](`\n${kMultilinePrompt.description}` +
(this[kColorize]?.(lines[i]) ?? lines[i]));
}
} else {
// Write the prompt and the current buffer content.
this[kWriteToOutput](this[kPrompt] +
(this[kColorize]?.(this.line) ?? this.line));
}

// Move cursor to original position.
cursorTo(this.output, cursorPos.cols);
// Force terminal to allocate a new line
if (lineCols === 0) {
this[kWriteToOutput](' ');
}

const diff = lineRows - cursorPos.rows;
if (diff > 0) {
moveCursor(this.output, 0, -diff);
}
// Move cursor to original position.
cursorTo(output, cursorPos.cols);

const diff = lineRows - cursorPos.rows;
if (diff > 0) {
moveCursor(output, 0, -diff);
}

this.prevRows = cursorPos.rows;
this.prevRows = cursorPos.rows;
} finally {
if (corked) {
output.uncork();
}
}
}

/**
Expand Down
153 changes: 153 additions & 0 deletions test/parallel/test-readline-refresh-cork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
'use strict';

const common = require('../common');

if (process.env.TERM === 'dumb') {
common.skip('skipping - dumb terminal');
}

const assert = require('assert');
const readline = require('readline');
const { Readable, Writable } = require('stream');
const ArrayStream = require('../common/arraystream');

// Verify [kRefreshLine] batches all terminal writes using cork()/uncork()
// so Writable streams with _writev flush each redraw atomically.
{
const writevBatches = [];
let individualWriteCalls = 0;

const output = new Writable({
write(chunk, encoding, callback) {
individualWriteCalls++;
callback();
},
writev(chunks, callback) {
writevBatches.push(chunks.map((c) => c.chunk.toString()));
callback();
},
});

const rl = readline.createInterface({
input: new Readable({ read() {} }),
output,
prompt: '> ',
terminal: true,
colorize: (line) => `\x1b[36m${line}\x1b[39m`,
});

rl.prompt();
assert.strictEqual(individualWriteCalls, 0);
assert.strictEqual(writevBatches.length, 1);
assert.deepStrictEqual(writevBatches[0], [
'\x1b[1G',
'\x1b[0J',
'> \x1b[36m\x1b[39m',
'\x1b[3G',
]);

writevBatches.length = 0;
rl.write('a');
rl.write('b');
assert.strictEqual(individualWriteCalls, 0);
assert.strictEqual(writevBatches.length, 2);
assert.deepStrictEqual(writevBatches[0], [
'\x1b[1G',
'\x1b[0J',
'> \x1b[36ma\x1b[39m',
'\x1b[4G',
]);
assert.deepStrictEqual(writevBatches[1], [
'\x1b[1G',
'\x1b[0J',
'> \x1b[36mab\x1b[39m',
'\x1b[5G',
]);

rl.close();
}

// Verify REPL syntax-highlighted keystrokes batch each redraw into one writev call.
if (common.hasInspector) {
const { REPLServer } = require('repl');
const writevBatches = [];
let individualWriteCalls = 0;

const output = new Writable({
write(chunk, encoding, callback) {
individualWriteCalls++;
callback();
},
writev(chunks, callback) {
writevBatches.push(chunks.map((c) => c.chunk.toString()));
callback();
},
});

const repl = new REPLServer({
input: new Readable({ read() {} }),
output,
prompt: '> ',
terminal: true,
useColors: true,
});

writevBatches.length = 0;
repl.write('n');
repl.write('e');
repl.write('w');
assert.strictEqual(individualWriteCalls, 0);
assert.strictEqual(writevBatches.length, 3);
assert.deepStrictEqual(writevBatches[2].slice(0, 2), ['\x1b[1G', '\x1b[0J']);
assert.strictEqual(writevBatches[2][writevBatches[2].length - 1], '\x1b[6G');

repl.close();
}

// Verify output is still uncorked if colorize throws during a refresh.
{
const output = new Writable({
write(chunk, encoding, callback) {
callback();
},
writev(chunks, callback) {
callback();
},
});

const rl = readline.createInterface({
input: new Readable({ read() {} }),
output,
prompt: '> ',
terminal: true,
colorize: () => {
throw new Error('colorize failed');
},
});

assert.throws(() => rl.prompt(), { message: 'colorize failed' });
assert.strictEqual(output.writableCorked, 0);
rl.close();
}

// Verify custom output streams without cork/uncork continue to work.
{
const input = new ArrayStream();
const output = new ArrayStream();
let rendered = '';
output.write = (chunk) => {
rendered += chunk;
};

const rl = readline.createInterface({
input,
output,
prompt: '> ',
terminal: true,
colorize: (line) => `<${line}>`,
});

rl.write('hi');
assert.ok(rendered.includes('> <hi>'));
rl.close();
}