I am not sure if this is a bug or a feature.
Consider this simple echo interface:
'use strict';
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
console.log(line);
});
It does not use rl.prompt() anywhere and with a common input there will be no default prompt:
GIF 1:

However, if a user press DELETE, BACKSPACE or UP button, default prompt immediately and somehow unexpectedly appears shifting the whole line:
GIF 2:

This can be prevented by setting the prompt option into the empty line. However, I am not sure if this opt-out rule is an intended behavior.
It seems this line in the Interface.prototype._refreshLine() (which is used in many cases) is the cause.
This seems like a bug to me, thanks for catching it. :)
@addaleax This function was created 5 years ago and is used heavily inside. I am afraid to mess with it. So maybe this better be addressed by anybody who knows this lib well enough)
@vsemozhetbyt Yeah, I can see that – this might be tricky to fix and with readline.js line coverage at 78 % I am scared to touch that, too … :smile:
is there a way to override the method for this specific use case? that way we avoid modifying the function itself.
@vsemozhetbyt I guess the problem here is bit tricky. readline is currently tightly coupled with repl.js and as you had mentioned refreshLine is actually adding up the prompt. In fact, readline has a code https://github.com/nodejs/node/blob/master/lib/readline.js#L154, which by default adds up the prompt (default value is >) and on _refreshLine it brings back to the light. I will try to see if there is a fix for this.
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: ''
});
https://nodejs.org/api/readline.html#readline_readline_createinterface_options
@vsemozhetbyt using prompt: '' seems correct.
I do see a bug though: each line should visualize the default prompt at all times. I guess it's a historical issue because the repl and readline is deeply coupled and that part was just originally handled in the repl. Thus, it was never implemented in readline correct.
Fixing readline to always print the prompt seems more difficult than I anticipated. Readline works synchronous but we'll have to defer printing the very first prompt in case there's no data listener attached so far. My first approach ended in some kind of Zalgo.
Most helpful comment
@vsemozhetbyt Yeah, I can see that – this might be tricky to fix and with readline.js line coverage at 78 % I am scared to touch that, too … :smile: