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.Interface
class are constructed using thereadline.createInterface()
method.
Jump to headingInterface(options: ReadLineOptions)
NOTE: According to the documentation:
Instances of the
readline.Interface
class are constructed using thereadline.createInterface()
method.
Properties Jump to heading
Jump to headingcursor: number
The 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: string
The 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: boolean
Methods Jump to heading
Jump to heading[Symbol.asyncIterator](): AsyncIterableIterator<string>
Jump to headingaddListener(event: string,listener: (...args: any[]) => void,): this
events.EventEmitter
- close
- line
- pause
- resume
- SIGCONT
- SIGINT
- SIGTSTP
- history
Jump to headingaddListener(event: "close",listener: () => void,): this
Jump to headingaddListener(event: "line",listener: (input: string) => void,): this
Jump to headingaddListener(event: "pause",listener: () => void,): this
Jump to headingaddListener(event: "resume",listener: () => void,): this
Jump to headingaddListener(event: "SIGCONT",listener: () => void,): this
Jump to headingaddListener(event: "SIGINT",listener: () => void,): this
Jump to headingaddListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingaddListener(event: "history",listener: (history: string[]) => void,): this
Jump to headingclose(): void
The 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[],): boolean
Jump to headingemit(event: "close"): boolean
Jump to headingemit(event: "line",input: string,): boolean
Jump to headingemit(event: "pause"): boolean
Jump to headingemit(event: "resume"): boolean
Jump to headingemit(event: "SIGCONT"): boolean
Jump to headingemit(event: "SIGINT"): boolean
Jump to headingemit(event: "SIGTSTP"): boolean
Jump to headingemit(event: "history",history: string[],): boolean
Returns 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(): string
The rl.getPrompt()
method returns the current prompt used by rl.prompt()
.
Jump to headingon(event: string,listener: (...args: any[]) => void,): this
Jump to headingon(event: "close",listener: () => void,): this
Jump to headingon(event: "line",listener: (input: string) => void,): this
Jump to headingon(event: "pause",listener: () => void,): this
Jump to headingon(event: "resume",listener: () => void,): this
Jump to headingon(event: "SIGCONT",listener: () => void,): this
Jump to headingon(event: "SIGINT",listener: () => void,): this
Jump to headingon(event: "SIGTSTP",listener: () => void,): this
Jump to headingon(event: "history",listener: (history: string[]) => void,): this
Jump to headingonce(event: string,listener: (...args: any[]) => void,): this
Jump to headingonce(event: "close",listener: () => void,): this
Jump to headingonce(event: "line",listener: (input: string) => void,): this
Jump to headingonce(event: "pause",listener: () => void,): this
Jump to headingonce(event: "resume",listener: () => void,): this
Jump to headingonce(event: "SIGCONT",listener: () => void,): this
Jump to headingonce(event: "SIGINT",listener: () => void,): this
Jump to headingonce(event: "SIGTSTP",listener: () => void,): this
Jump to headingonce(event: "history",listener: (history: string[]) => void,): this
Jump to headingpause(): this
The 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,): this
Jump to headingprependListener(event: "close",listener: () => void,): this
Jump to headingprependListener(event: "line",listener: (input: string) => void,): this
Jump to headingprependListener(event: "pause",listener: () => void,): this
Jump to headingprependListener(event: "resume",listener: () => void,): this
Jump to headingprependListener(event: "SIGCONT",listener: () => void,): this
Jump to headingprependListener(event: "SIGINT",listener: () => void,): this
Jump to headingprependListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingprependListener(event: "history",listener: (history: string[]) => void,): this
Jump to headingprependOnceListener(event: string,listener: (...args: any[]) => void,): this
Jump to headingprependOnceListener(event: "close",listener: () => void,): this
Jump to headingprependOnceListener(event: "line",listener: (input: string) => void,): this
Jump to headingprependOnceListener(event: "pause",listener: () => void,): this
Jump to headingprependOnceListener(event: "resume",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGCONT",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGINT",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingprependOnceListener(event: "history",listener: (history: string[]) => void,): this
Jump to headingprompt(preserveCursor?: boolean): void
The 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,): void
The 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(): void
Jump to headingresume(): this
The rl.resume()
method resumes the input
stream if it has been paused.
Jump to headingsetPrompt(prompt: string): void
The 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,): void
The 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