{ "source": "doc/api/readline.md", "modules": [ { "textRaw": "Readline", "name": "readline", "stability": 2, "stabilityText": "Stable", "desc": "
The readline
module provides an interface for reading data from a Readable\nstream (such as process.stdin
) one line at a time. It can be accessed using:
const readline = require('readline');\n
\nThe following simple example illustrates the basic use of the readline
module.
const readline = require('readline');\n\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n});\n\nrl.question('What do you think of Node.js? ', (answer) => {\n // TODO: Log the answer in a database\n console.log(`Thank you for your valuable feedback: ${answer}`);\n\n rl.close();\n});\n
\nNote Once this code is invoked, the Node.js application will not\nterminate until the readline.Interface
is closed because the interface\nwaits for data to be received on the input
stream.
Instances of the readline.Interface
class are constructed using the\nreadline.createInterface()
method. Every instance is associated with a\nsingle input
Readable stream and a single output
Writable stream.\nThe output
stream is used to print prompts for user input that arrives on,\nand is read from, the input
stream.
The 'close'
event is emitted when one of the following occur:
rl.close()
method is called and the readline.Interface
instance has\nrelinquished control over the input
and output
streams;input
stream receives its 'end'
event;input
stream receives <ctrl>-D
to signal end-of-transmission (EOT);input
stream receives <ctrl>-C
to signal SIGINT
and there is no\nSIGINT
event listener registered on the readline.Interface
instance.The listener function is called without passing any arguments.
\nThe readline.Interface
instance should be considered to be "finished" once\nthe 'close'
event is emitted.
The 'line'
event is emitted whenever the input
stream receives an\nend-of-line input (\\n
, \\r
, or \\r\\n
). This usually occurs when the user\npresses the <Enter>
, or <Return>
keys.
The listener function is called with a string containing the single line of\nreceived input.
\nFor example:
\nrl.on('line', (input) => {\n console.log(`Received: ${input}`);\n});\n
\n",
"params": []
},
{
"textRaw": "Event: 'pause'",
"type": "event",
"name": "pause",
"meta": {
"added": [
"v0.7.5"
]
},
"desc": "The 'pause'
event is emitted when one of the following occur:
input
stream is paused.input
stream is not paused and receives the SIGCONT
event. (See\nevents SIGTSTP
and SIGCONT
)The listener function is called without passing any arguments.
\nFor example:
\nrl.on('pause', () => {\n console.log('Readline paused.');\n});\n
\n",
"params": []
},
{
"textRaw": "Event: 'resume'",
"type": "event",
"name": "resume",
"meta": {
"added": [
"v0.7.5"
]
},
"desc": "The 'resume'
event is emitted whenever the input
stream is resumed.
The listener function is called without passing any arguments.
\nrl.on('resume', () => {\n console.log('Readline resumed.');\n});\n
\n",
"params": []
},
{
"textRaw": "Event: 'SIGCONT'",
"type": "event",
"name": "SIGCONT",
"meta": {
"added": [
"v0.7.5"
]
},
"desc": "The 'SIGCONT'
event is emitted when a Node.js process previously moved into\nthe background using <ctrl>-Z
(i.e. SIGTSTP
) is then brought back to the\nforeground using fg(1).
If the input
stream was paused before the SIGTSTP
request, this event will\nnot be emitted.
The listener function is invoked without passing any arguments.
\nFor example:
\nrl.on('SIGCONT', () => {\n // `prompt` will automatically resume the stream\n rl.prompt();\n});\n
\nNote: The 'SIGCONT'
event is not supported on Windows.
The 'SIGINT'
event is emitted whenever the input
stream receives a\n<ctrl>-C
input, known typically as SIGINT
. If there are no 'SIGINT'
event\nlisteners registered when the input
stream receives a SIGINT
, the 'pause'
\nevent will be emitted.
The listener function is invoked without passing any arguments.
\nFor example:
\nrl.on('SIGINT', () => {\n rl.question('Are you sure you want to exit?', (answer) => {\n if (answer.match(/^y(es)?$/i)) rl.pause();\n });\n});\n
\n",
"params": []
},
{
"textRaw": "Event: 'SIGTSTP'",
"type": "event",
"name": "SIGTSTP",
"meta": {
"added": [
"v0.7.5"
]
},
"desc": "The 'SIGTSTP'
event is emitted when the input
stream receives a <ctrl>-Z
\ninput, typically known as SIGTSTP
. If there are no SIGTSTP
event listeners\nregistered when the input
stream receives a SIGTSTP
, the Node.js process\nwill be sent to the background.
When the program is resumed using fg(1), the 'pause'
and SIGCONT
events\nwill be emitted. These can be used to resume the input
stream.
The 'pause'
and 'SIGCONT'
events will not be emitted if the input
was\npaused before the process was sent to the background.
The listener function is invoked without passing any arguments.
\nFor example:
\nrl.on('SIGTSTP', () => {\n // This will override SIGTSTP and prevent the program from going to the\n // background.\n console.log('Caught SIGTSTP.');\n});\n
\nNote: The 'SIGTSTP'
event is not supported on Windows.
The rl.close()
method closes the readline.Interface
instance and\nrelinquishes control over the input
and output
streams. When called,\nthe 'close'
event will be emitted.
The rl.pause()
method pauses the input
stream, allowing it to be resumed\nlater if necessary.
Calling rl.pause()
does not immediately pause other events (including\n'line'
) from being emitted by the readline.Interface
instance.
The rl.prompt()
method writes the readline.Interface
instances configured\nprompt
to a new line in output
in order to provide a user with a new\nlocation at which to provide input.
When called, rl.prompt()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the prompt is not written.
The rl.question()
method displays the query
by writing it to the output
,\nwaits for user input to be provided on input
, then invokes the callback
\nfunction passing the provided input as the first argument.
When called, rl.question()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the query
is not written.
Example usage:
\nrl.question('What is your favorite food?', (answer) => {\n console.log(`Oh, so your favorite food is ${answer}`);\n});\n
\nNote: The callback
function passed to rl.question()
does not follow the\ntypical pattern of accepting an Error
object or null
as the first argument.\nThe callback
is called with the provided answer as the only argument.
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
\nwhenever rl.prompt()
is called.
The rl.write()
method will write either data
or a key sequence identified\nby key
to the output
. The key
argument is supported only if output
is\na TTY text terminal.
If key
is specified, data
is ignored.
When called, rl.write()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the data
and key
are not written.
For example:
\nrl.write('Delete this!');\n// Simulate Ctrl+u to delete the line written previously\nrl.write(null, {ctrl: true, name: 'u'});\n
\nNote: The rl.write()
method will write the data to the readline
\nInterface's input
as if it were provided by the user.
The readline.clearLine()
method clears current line of given TTY stream\nin a specified direction identified by dir
.
The readline.clearScreenDown()
method clears the given TTY stream from\nthe current position of the cursor down.
The readline.createInterface()
method creates a new readline.Interface
\ninstance.
For example:
\nconst readline = require('readline');\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n});\n
\nOnce the readline.Interface
instance is created, the most common case is to\nlisten for the 'line'
event:
rl.on('line', (line) => {\n console.log(`Received: ${line}`);\n});\n
\nIf terminal
is true
for this instance then the output
stream will get\nthe best compatibility if it defines an output.columns
property and emits\na 'resize'
event on the output
if or when the columns ever change\n(process.stdout
does this automatically when it is a TTY).
When called, the completer
function is provided the current line entered by\nthe user, and is expected to return an Array with 2 entries:
For instance: [[substr1, substr2, ...], originalsubstring]
.
function completer(line) {\n const completions = '.help .error .exit .quit .q'.split(' ');\n const hits = completions.filter((c) => { return c.indexOf(line) === 0 });\n // show all completions if none found\n return [hits.length ? hits : completions, line];\n}\n
\nThe completer
function can be called asynchronously if it accepts two\narguments:
function completer(linePartial, callback) {\n callback(null, [['123'], linePartial]);\n}\n
\n",
"type": "module",
"displayName": "Use of the `completer` Function"
}
]
},
{
"textRaw": "readline.cursorTo(stream, x, y)",
"type": "method",
"name": "cursorTo",
"meta": {
"added": [
"v0.7.7"
]
},
"signatures": [
{
"params": [
{
"textRaw": "`stream` {Writable} ",
"name": "stream",
"type": "Writable"
},
{
"textRaw": "`x` {number} ",
"name": "x",
"type": "number"
},
{
"textRaw": "`y` {number} ",
"name": "y",
"type": "number"
}
]
},
{
"params": [
{
"name": "stream"
},
{
"name": "x"
},
{
"name": "y"
}
]
}
],
"desc": "The readline.cursorTo()
method moves cursor to the specified position in a\ngiven TTY stream
.
The readline.emitKeypressEvents()
method causes the given Writable\nstream
to begin emitting 'keypress'
events corresponding to received input.
Optionally, interface
specifies a readline.Interface
instance for which\nautocompletion is disabled when copy-pasted input is detected.
If the stream
is a TTY, then it must be in raw mode.
readline.emitKeypressEvents(process.stdin);\nif (process.stdin.isTTY)\n process.stdin.setRawMode(true);\n
\n"
},
{
"textRaw": "readline.moveCursor(stream, dx, dy)",
"type": "method",
"name": "moveCursor",
"meta": {
"added": [
"v0.7.7"
]
},
"signatures": [
{
"params": [
{
"textRaw": "`stream` {Writable} ",
"name": "stream",
"type": "Writable"
},
{
"textRaw": "`dx` {number} ",
"name": "dx",
"type": "number"
},
{
"textRaw": "`dy` {Number} ",
"name": "dy",
"type": "Number"
}
]
},
{
"params": [
{
"name": "stream"
},
{
"name": "dx"
},
{
"name": "dy"
}
]
}
],
"desc": "The readline.moveCursor()
method moves the cursor relative to its current\nposition in a given TTY stream
.
The following example illustrates the use of readline.Interface
class to\nimplement a small command-line interface:
const readline = require('readline');\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n prompt: 'OHAI> '\n});\n\nrl.prompt();\n\nrl.on('line', (line) => {\n switch(line.trim()) {\n case 'hello':\n console.log('world!');\n break;\n default:\n console.log(`Say what? I might have heard '${line.trim()}'`);\n break;\n }\n rl.prompt();\n}).on('close', () => {\n console.log('Have a great day!');\n process.exit(0);\n});\n
\nA common use case for readline
is to consume input from a filesystem\nReadable stream one line at a time, as illustrated in the following\nexample:
const readline = require('readline');\nconst fs = require('fs');\n\nconst rl = readline.createInterface({\n input: fs.createReadStream('sample.txt')\n});\n\nrl.on('line', (line) => {\n console.log(`Line from file: ${line}`);\n});\n
\n"
}
],
"type": "module",
"displayName": "Readline"
}
]
}