Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/node/readline.ts>Interface

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
class Interface
extends EventEmitter
Re-export
import { Interface } from "https://deno.land/std@0.142.0/node/readline.ts";

Instances of the readline.Interface class are constructed using thereadline.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

new
protected
Interface(
output?: WritableStream,
completer?: Completer | AsyncCompleter,
terminal?: boolean,
)

NOTE: According to the documentation:

Instances of the readline.Interface class are constructed using the readline.createInterface() method.

new
protected
Interface(options: ReadLineOptions)

NOTE: According to the documentation:

Instances of the readline.Interface class are constructed using the readline.createInterface() method.

Properties

readonly
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.

readonly
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();
});
readonly
terminal: boolean

Methods

addListener(event: string, listener: (...args: any[]) => void): this

events.EventEmitter

  1. close
  2. line
  3. pause
  4. resume
  5. SIGCONT
  6. SIGINT
  7. SIGTSTP
  8. 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 readline.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 readline.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
getCursorPos(): CursorPos

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 readline.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 readline.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 readline.Interface was created with output set to null orundefined 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 callbackfunction passing the provided input as the first argument.

When called, rl.question() will resume the input stream if it has been paused.

If the readline.Interface was created with output set to null orundefined 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.

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);

If this method is invoked as it's util.promisify()ed version, it returns a Promise that fulfills with the answer. If the question is canceled using an AbortController it will reject with an AbortError.

const util = require('util');
const question = util.promisify(rl.question).bind(rl);

async function questionExample() {
  try {
    const answer = await question('What is you favorite food? ');
    console.log(`Oh, so your favorite food is ${answer}`);
  } catch (err) {
    console.error('Question rejected', err);
  }
}
questionExample();
question(
query: string,
options: Abortable,
callback: (answer: string) => void,
): void
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 outputwhenever rl.prompt() is called.

write(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 readline.Interface was created with output set to null orundefined 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'sinput_as if it were provided by the user_.

write(data:
| undefined
| null
| string
| Buffer
, key: Key
): void
[Symbol.asyncIterator](): AsyncIterableIterator<string>