Instances of the readline.Interface class are constructed using the readline.createInterface() method. Every instance is associated with a
single input Readable stream and a single output Writable stream.
The output stream is used to print prompts for user input that arrives on,
and is read from, the input stream.
Constructors Jump to heading
Jump to headingInterface(input: ReadableStream,output?: WritableStream,completer?: Completer | AsyncCompleter,terminal?: boolean,)NOTE: According to the documentation:
Instances of the
readline.Interfaceclass are constructed using thereadline.createInterface()method.
Jump to headingInterface(options: ReadLineOptions)NOTE: According to the documentation:
Instances of the
readline.Interfaceclass are constructed using thereadline.createInterface()method.
Properties Jump to heading
Jump to headingcursor: numberThe cursor position relative to rl.line.
This will track where the current cursor lands in the input string, when reading input from a TTY stream. The position of cursor determines the portion of the input string that will be modified as input is processed, as well as the column where the terminal caret will be rendered.
Jump to headingline: stringThe current input data being processed by node.
This can be used when collecting input from a TTY stream to retrieve the
current value that has been processed thus far, prior to the line event
being emitted. Once the line event has been emitted, this property will
be an empty string.
Be aware that modifying the value during the instance runtime may have
unintended consequences if rl.cursor is not also controlled.
If not using a TTY stream for input, use the 'line' event.
One possible use case would be as follows:
const values = ['lorem ipsum', 'dolor sit amet'];
const rl = readline.createInterface(process.stdin);
const showResults = debounce(() => {
console.log(
'\n',
values.filter((val) => val.startsWith(rl.line)).join(' '),
);
}, 300);
process.stdin.on('keypress', (c, k) => {
showResults();
});
Jump to headingterminal: booleanMethods Jump to heading
Jump to heading[Symbol.asyncIterator](): AsyncIterableIterator<string>Jump to headingaddListener(event: string,listener: (...args: any[]) => void,): thisevents.EventEmitter
- close
- line
- pause
- resume
- SIGCONT
- SIGINT
- SIGTSTP
- history
Jump to headingaddListener(event: "close",listener: () => void,): thisJump to headingaddListener(event: "line",listener: (input: string) => void,): thisJump to headingaddListener(event: "pause",listener: () => void,): thisJump to headingaddListener(event: "resume",listener: () => void,): thisJump to headingaddListener(event: "SIGCONT",listener: () => void,): thisJump to headingaddListener(event: "SIGINT",listener: () => void,): thisJump to headingaddListener(event: "SIGTSTP",listener: () => void,): thisJump to headingaddListener(event: "history",listener: (history: string[]) => void,): thisJump to headingclose(): voidThe rl.close() method closes the Interface instance and
relinquishes control over the input and output streams. When called,
the 'close' event will be emitted.
Calling rl.close() does not immediately stop other events (including 'line')
from being emitted by the Interface instance.
Jump to headingemit(event: string | symbol,...args: any[],): booleanJump to headingemit(event: "close"): booleanJump to headingemit(event: "line",input: string,): booleanJump to headingemit(event: "pause"): booleanJump to headingemit(event: "resume"): booleanJump to headingemit(event: "SIGCONT"): booleanJump to headingemit(event: "SIGINT"): booleanJump to headingemit(event: "SIGTSTP"): booleanJump to headingemit(event: "history",history: string[],): booleanReturns the real position of the cursor in relation to the input prompt + string. Long input (wrapping) strings, as well as multiple line prompts are included in the calculations.
Jump to headinggetPrompt(): stringThe rl.getPrompt() method returns the current prompt used by rl.prompt().
Jump to headingon(event: string,listener: (...args: any[]) => void,): thisJump to headingon(event: "close",listener: () => void,): thisJump to headingon(event: "line",listener: (input: string) => void,): thisJump to headingon(event: "pause",listener: () => void,): thisJump to headingon(event: "resume",listener: () => void,): thisJump to headingon(event: "SIGCONT",listener: () => void,): thisJump to headingon(event: "SIGINT",listener: () => void,): thisJump to headingon(event: "SIGTSTP",listener: () => void,): thisJump to headingon(event: "history",listener: (history: string[]) => void,): thisJump to headingonce(event: string,listener: (...args: any[]) => void,): thisJump to headingonce(event: "close",listener: () => void,): thisJump to headingonce(event: "line",listener: (input: string) => void,): thisJump to headingonce(event: "pause",listener: () => void,): thisJump to headingonce(event: "resume",listener: () => void,): thisJump to headingonce(event: "SIGCONT",listener: () => void,): thisJump to headingonce(event: "SIGINT",listener: () => void,): thisJump to headingonce(event: "SIGTSTP",listener: () => void,): thisJump to headingonce(event: "history",listener: (history: string[]) => void,): thisJump to headingpause(): thisThe rl.pause() method pauses the input stream, allowing it to be resumed
later if necessary.
Calling rl.pause() does not immediately pause other events (including 'line') from being emitted by the Interface instance.
Jump to headingprependListener(event: string,listener: (...args: any[]) => void,): thisJump to headingprependListener(event: "close",listener: () => void,): thisJump to headingprependListener(event: "line",listener: (input: string) => void,): thisJump to headingprependListener(event: "pause",listener: () => void,): thisJump to headingprependListener(event: "resume",listener: () => void,): thisJump to headingprependListener(event: "SIGCONT",listener: () => void,): thisJump to headingprependListener(event: "SIGINT",listener: () => void,): thisJump to headingprependListener(event: "SIGTSTP",listener: () => void,): thisJump to headingprependListener(event: "history",listener: (history: string[]) => void,): thisJump to headingprependOnceListener(event: string,listener: (...args: any[]) => void,): thisJump to headingprependOnceListener(event: "close",listener: () => void,): thisJump to headingprependOnceListener(event: "line",listener: (input: string) => void,): thisJump to headingprependOnceListener(event: "pause",listener: () => void,): thisJump to headingprependOnceListener(event: "resume",listener: () => void,): thisJump to headingprependOnceListener(event: "SIGCONT",listener: () => void,): thisJump to headingprependOnceListener(event: "SIGINT",listener: () => void,): thisJump to headingprependOnceListener(event: "SIGTSTP",listener: () => void,): thisJump to headingprependOnceListener(event: "history",listener: (history: string[]) => void,): thisJump to headingprompt(preserveCursor?: boolean): voidThe rl.prompt() method writes the Interface instances configuredprompt to a new line in output in order to provide a user with a new
location at which to provide input.
When called, rl.prompt() will resume the input stream if it has been
paused.
If the Interface was created with output set to null or undefined the prompt is not written.
Jump to headingquestion(query: string,callback: (answer: string) => void,): voidThe rl.question() method displays the query by writing it to the output,
waits for user input to be provided on input, then invokes the callback function passing the provided input as the first argument.
When called, rl.question() will resume the input stream if it has been
paused.
If the Interface was created with output set to null or undefined the query is not written.
The callback function passed to rl.question() does not follow the typical
pattern of accepting an Error object or null as the first argument.
The callback is called with the provided answer as the only argument.
An error will be thrown if calling rl.question() after rl.close().
Example usage:
rl.question('What is your favorite food? ', (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
Using an AbortController to cancel a question.
const ac = new AbortController();
const signal = ac.signal;
rl.question('What is your favorite food? ', { signal }, (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
signal.addEventListener('abort', () => {
console.log('The food question timed out');
}, { once: true });
setTimeout(() => ac.abort(), 10000);
Jump to headingquestion(): voidJump to headingresume(): thisThe rl.resume() method resumes the input stream if it has been paused.
Jump to headingsetPrompt(prompt: string): voidThe rl.setPrompt() method sets the prompt that will be written to output whenever rl.prompt() is called.
Jump to headingwrite(data: string | Buffer,key?: Key,): voidThe rl.write() method will write either data or a key sequence identified
by key to the output. The key argument is supported only if output is
a TTY text terminal. See TTY keybindings for a list of key
combinations.
If key is specified, data is ignored.
When called, rl.write() will resume the input stream if it has been
paused.
If the Interface was created with output set to null or undefined the data and key are not written.
rl.write('Delete this!');
// Simulate Ctrl+U to delete the line written previously
rl.write(null, { ctrl: true, name: 'u' });
The rl.write() method will write the data to the readline Interface's input as if it were provided by the user.
Jump to headingwrite(data: undefined
| null
| string
| Buffer,key: Key,): void