import { Interface } from "https://deno.land/std@0.170.0/node/_readline.d.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
NOTE: According to the documentation:
Instances of the
readline.Interface
class are constructed using thereadline.createInterface()
method.
Properties
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.
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();
});
Methods
events.EventEmitter
- close
- line
- pause
- resume
- SIGCONT
- SIGINT
- SIGTSTP
- history
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.
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.
The rl.getPrompt()
method returns the current prompt used by rl.prompt()
.
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.
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.
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 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();
The rl.resume()
method resumes the input
stream if it has been paused.
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 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_.