Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: don’t write to input stream in editor mode #9207

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ function REPLServer(prompt,
const matches = self._sawKeyPress ? cmd.match(/^\s+/) : null;
if (matches) {
const prefix = matches[0];
self.inputStream.write(prefix);
self.write(prefix);
self.line = prefix;
self.cursor = prefix.length;
}
Expand Down Expand Up @@ -601,6 +601,7 @@ function REPLServer(prompt,
// Wrap readline tty to enable editor mode
const ttyWrite = self._ttyWrite.bind(self);
self._ttyWrite = (d, key) => {
key = key || {};
if (!self.editorMode || !self.terminal) {
ttyWrite(d, key);
return;
Expand Down
26 changes: 21 additions & 5 deletions test/parallel/test-repl-.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const repl = require('repl');
// \u001b[0J - Clear screen
// \u001b[3G - Moves the cursor to 3rd column
const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');

function run({input, output, event}) {
function run({input, output, event, checkTerminalCodes = true}) {
const stream = new common.ArrayStream();
let found = '';

stream.write = (msg) => found += msg.replace('\r', '');

const expected = `${terminalCode}.editor\n` +
'// Entering editor mode (^D to finish, ^C to cancel)\n' +
`${input}${output}\n${terminalCode}`;
let expected = `${terminalCode}.editor\n` +
'// Entering editor mode (^D to finish, ^C to cancel)\n' +
`${input}${output}\n${terminalCode}`;

const replServer = repl.start({
prompt: '> ',
Expand All @@ -31,6 +32,12 @@ function run({input, output, event}) {
stream.emit('data', input);
replServer.write('', event);
replServer.close();

if (!checkTerminalCodes) {
found = found.replace(terminalCodeRegex, '').replace(/\n/g, '');
expected = expected.replace(terminalCodeRegex, '').replace(/\n/g, '');
}

assert.strictEqual(found, expected);
}

Expand All @@ -54,6 +61,12 @@ const tests = [
input: ' var i = 1;\ni + 3',
output: '\n4',
event: {ctrl: true, name: 'd'}
},
{
input: '',
output: '',
checkTerminalCodes: false,
event: null,
}
];

Expand All @@ -62,12 +75,15 @@ tests.forEach(run);
// Auto code alignment for .editor mode
function testCodeAligment({input, cursor = 0, line = ''}) {
const stream = new common.ArrayStream();
const outputStream = new common.ArrayStream();

stream.write = () => { throw new Error('Writing not allowed!'); };

const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: stream,
output: outputStream,
useColors: false
});

Expand Down