Usage in Deno
import { Interface } from "node:readline";
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.
Interface(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.
Interface(options: ReadLineOptions)
NOTE: According to the documentation:
Instances of the
readline.Interface
class are constructed using thereadline.createInterface()
method.
cursor: 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.
line: 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(); });
terminal: boolean
[Symbol.asyncIterator](): AsyncIterableIterator<string>
addListener(event: string,listener: (...args: any[]) => void,): this
events.EventEmitter
- close
- line
- pause
- resume
- SIGCONT
- SIGINT
- SIGTSTP
- history
addListener(event: "close",listener: () => void,): this
addListener(event: "line",listener: (input: string) => void,): this
addListener(event: "pause",listener: () => void,): this
addListener(event: "resume",listener: () => void,): this
addListener(event: "SIGCONT",listener: () => void,): this
addListener(event: "SIGINT",listener: () => void,): this
addListener(event: "SIGTSTP",listener: () => void,): this
addListener(event: "history",listener: (history: string[]) => void,): this
close(): 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.
emit(event: string | symbol,...args: any[],): boolean
emit(event: "close"): boolean
emit(event: "line",input: string,): boolean
emit(event: "pause"): boolean
emit(event: "resume"): boolean
emit(event: "SIGCONT"): boolean
emit(event: "SIGINT"): boolean
emit(event: "SIGTSTP"): boolean
emit(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.
getPrompt(): string
The rl.getPrompt()
method returns the current prompt used by rl.prompt()
.
on(event: string,listener: (...args: any[]) => void,): this
on(event: "close",listener: () => void,): this
on(event: "line",listener: (input: string) => void,): this
on(event: "pause",listener: () => void,): this
on(event: "resume",listener: () => void,): this
on(event: "SIGCONT",listener: () => void,): this
on(event: "SIGINT",listener: () => void,): this
on(event: "SIGTSTP",listener: () => void,): this
on(event: "history",listener: (history: string[]) => void,): this
once(event: string,listener: (...args: any[]) => void,): this
once(event: "close",listener: () => void,): this
once(event: "line",listener: (input: string) => void,): this
once(event: "pause",listener: () => void,): this
once(event: "resume",listener: () => void,): this
once(event: "SIGCONT",listener: () => void,): this
once(event: "SIGINT",listener: () => void,): this
once(event: "SIGTSTP",listener: () => void,): this
once(event: "history",listener: (history: string[]) => void,): this
pause(): 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.
prependListener(event: string,listener: (...args: any[]) => void,): this
prependListener(event: "close",listener: () => void,): this
prependListener(event: "line",listener: (input: string) => void,): this
prependListener(event: "pause",listener: () => void,): this
prependListener(event: "resume",listener: () => void,): this
prependListener(event: "SIGCONT",listener: () => void,): this
prependListener(event: "SIGINT",listener: () => void,): this
prependListener(event: "SIGTSTP",listener: () => void,): this
prependListener(event: "history",listener: (history: string[]) => void,): this
prependOnceListener(event: string,listener: (...args: any[]) => void,): this
prependOnceListener(event: "close",listener: () => void,): this
prependOnceListener(event: "line",listener: (input: string) => void,): this
prependOnceListener(event: "pause",listener: () => void,): this
prependOnceListener(event: "resume",listener: () => void,): this
prependOnceListener(event: "SIGCONT",listener: () => void,): this
prependOnceListener(event: "SIGINT",listener: () => void,): this
prependOnceListener(event: "SIGTSTP",listener: () => void,): this
prependOnceListener(event: "history",listener: (history: string[]) => void,): this
prompt(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.
question(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);
resume(): this
The rl.resume()
method resumes the input
stream if it has been paused.
setPrompt(prompt: string): void
The rl.setPrompt()
method sets the prompt that will be written to output
whenever rl.prompt()
is called.
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.