{ "source": "doc/api/readline.markdown", "modules": [ { "textRaw": "Readline", "name": "readline", "stability": 2, "stabilityText": "Unstable", "desc": "

To use this module, do require('readline'). Readline allows reading of a\nstream (such as process.stdin) on a line-by-line basis.\n\n

\n

Note that once you've invoked this module, your node program will not\nterminate until you've closed the interface. Here's how to allow your\nprogram to gracefully exit:\n\n

\n
var readline = require('readline');\n\nvar rl = readline.createInterface({\n  input: process.stdin,\n  output: process.stdout\n});\n\nrl.question("What do you think of node.js? ", function(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", "methods": [ { "textRaw": "readline.createInterface(options)", "type": "method", "name": "createInterface", "desc": "

Creates a readline Interface instance. Accepts an "options" Object that takes\nthe following values:\n\n

\n\n

The completer function is given the current line entered by the user, and\nis supposed to return an Array with 2 entries:\n\n

\n
    \n
  1. An Array with matching entries for the completion.

    \n
  2. \n
  3. The substring that was used for the matching.

    \n
  4. \n
\n

Which ends up looking something like:\n[[substr1, substr2, ...], originalsubstring].\n\n

\n

Example:\n\n

\n
function completer(line) {\n  var completions = '.help .error .exit .quit .q'.split(' ')\n  var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })\n  // show all completions if none found\n  return [hits.length ? hits : completions, line]\n}
\n

Also completer can be run in async mode if it accepts two arguments:\n\n

\n
function completer(linePartial, callback) {\n  callback(null, [['123'], linePartial]);\n}
\n

createInterface is commonly used with process.stdin and\nprocess.stdout in order to accept user input:\n\n

\n
var readline = require('readline');\nvar rl = readline.createInterface({\n  input: process.stdin,\n  output: process.stdout\n});
\n

Once you have a readline instance, you most commonly listen for the\n"line" event.\n\n

\n

If terminal is true for this instance then the output stream will get\nthe best compatibility if it defines an output.columns property, and fires\na "resize" event on the output if/when the columns ever change\n(process.stdout does this automatically when it is a TTY).\n\n

\n", "signatures": [ { "params": [ { "name": "options" } ] } ] }, { "textRaw": "readline.cursorTo(stream, x, y)", "type": "method", "name": "cursorTo", "desc": "

Move cursor to the specified position in a given TTY stream.\n\n

\n", "signatures": [ { "params": [ { "name": "stream" }, { "name": "x" }, { "name": "y" } ] } ] }, { "textRaw": "readline.moveCursor(stream, dx, dy)", "type": "method", "name": "moveCursor", "desc": "

Move cursor relative to it's current position in a given TTY stream.\n\n

\n", "signatures": [ { "params": [ { "name": "stream" }, { "name": "dx" }, { "name": "dy" } ] } ] }, { "textRaw": "readline.clearLine(stream, dir)", "type": "method", "name": "clearLine", "desc": "

Clears current line of given TTY stream in a specified direction.\ndir should have one of following values:\n\n

\n\n", "signatures": [ { "params": [ { "name": "stream" }, { "name": "dir" } ] } ] }, { "textRaw": "readline.clearScreenDown(stream)", "type": "method", "name": "clearScreenDown", "desc": "

Clears the screen from the current position of the cursor down.\n

\n", "signatures": [ { "params": [ { "name": "stream" } ] } ] } ], "classes": [ { "textRaw": "Class: Interface", "type": "class", "name": "Interface", "desc": "

The class that represents a readline interface with an input and output\nstream.\n\n

\n", "methods": [ { "textRaw": "rl.setPrompt(prompt, length)", "type": "method", "name": "setPrompt", "desc": "

Sets the prompt, for example when you run node on the command line, you see\n> , which is node's prompt.\n\n

\n", "signatures": [ { "params": [ { "name": "prompt" }, { "name": "length" } ] } ] }, { "textRaw": "rl.prompt([preserveCursor])", "type": "method", "name": "prompt", "desc": "

Readies readline for input from the user, putting the current setPrompt\noptions on a new line, giving the user a new spot to write. Set preserveCursor\nto true to prevent the cursor placement being reset to 0.\n\n

\n

This will also resume the input stream used with createInterface if it has\nbeen paused.\n\n

\n", "signatures": [ { "params": [ { "name": "preserveCursor", "optional": true } ] } ] }, { "textRaw": "rl.question(query, callback)", "type": "method", "name": "question", "desc": "

Prepends the prompt with query and invokes callback with the user's\nresponse. Displays the query to the user, and then invokes callback\nwith the user's response after it has been typed.\n\n

\n

This will also resume the input stream used with createInterface if\nit has been paused.\n\n

\n

Example usage:\n\n

\n
interface.question('What is your favorite food?', function(answer) {\n  console.log('Oh, so your favorite food is ' + answer);\n});
\n", "signatures": [ { "params": [ { "name": "query" }, { "name": "callback" } ] } ] }, { "textRaw": "rl.pause()", "type": "method", "name": "pause", "desc": "

Pauses the readline input stream, allowing it to be resumed later if needed.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "rl.resume()", "type": "method", "name": "resume", "desc": "

Resumes the readline input stream.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "rl.close()", "type": "method", "name": "close", "desc": "

Closes the Interface instance, relinquishing control on the input and\noutput streams. The "close" event will also be emitted.\n\n

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "rl.write(data, [key])", "type": "method", "name": "write", "desc": "

Writes data to output stream. key is an object literal to represent a key\nsequence; available if the terminal is a TTY.\n\n

\n

This will also resume the input stream if it has been paused.\n\n

\n

Example:\n\n

\n
rl.write('Delete me!');\n// Simulate ctrl+u to delete the line written previously\nrl.write(null, {ctrl: true, name: 'u'});
\n", "signatures": [ { "params": [ { "name": "data" }, { "name": "key", "optional": true } ] } ] } ] } ], "modules": [ { "textRaw": "Events", "name": "events", "events": [ { "textRaw": "Event: 'line'", "type": "event", "name": "line", "desc": "

function (line) {}\n\n

\n

Emitted whenever the input stream receives a \\n, usually received when the\nuser hits enter, or return. This is a good hook to listen for user input.\n\n

\n

Example of listening for line:\n\n

\n
rl.on('line', function (cmd) {\n  console.log('You just typed: '+cmd);\n});
\n", "params": [] }, { "textRaw": "Event: 'pause'", "type": "event", "name": "pause", "desc": "

function () {}\n\n

\n

Emitted whenever the input stream is paused.\n\n

\n

Also emitted whenever the input stream is not paused and receives the\nSIGCONT event. (See events SIGTSTP and SIGCONT)\n\n

\n

Example of listening for pause:\n\n

\n
rl.on('pause', function() {\n  console.log('Readline paused.');\n});
\n", "params": [] }, { "textRaw": "Event: 'resume'", "type": "event", "name": "resume", "desc": "

function () {}\n\n

\n

Emitted whenever the input stream is resumed.\n\n

\n

Example of listening for resume:\n\n

\n
rl.on('resume', function() {\n  console.log('Readline resumed.');\n});
\n", "params": [] }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "desc": "

function () {}\n\n

\n

Emitted when close() is called.\n\n

\n

Also emitted when the input stream receives its "end" event. The Interface\ninstance should be considered "finished" once this is emitted. For example, when\nthe input stream receives ^D, respectively known as EOT.\n\n

\n

This event is also called if there is no SIGINT event listener present when\nthe input stream receives a ^C, respectively known as SIGINT.\n\n

\n", "params": [] }, { "textRaw": "Event: 'SIGINT'", "type": "event", "name": "SIGINT", "desc": "

function () {}\n\n

\n

Emitted whenever the input stream receives a ^C, respectively known as\nSIGINT. If there is no SIGINT event listener present when the input\nstream receives a SIGINT, pause will be triggered.\n\n

\n

Example of listening for SIGINT:\n\n

\n
rl.on('SIGINT', function() {\n  rl.question('Are you sure you want to exit?', function(answer) {\n    if (answer.match(/^y(es)?$/i)) rl.pause();\n  });\n});
\n", "params": [] }, { "textRaw": "Event: 'SIGTSTP'", "type": "event", "name": "SIGTSTP", "desc": "

function () {}\n\n

\n

This does not work on Windows.\n\n

\n

Emitted whenever the input stream receives a ^Z, respectively known as\nSIGTSTP. If there is no SIGTSTP event listener present when the input\nstream receives a SIGTSTP, the program will be sent to the background.\n\n

\n

When the program is resumed with fg, the pause and SIGCONT events will be\nemitted. You can use either to resume the stream.\n\n

\n

The pause and SIGCONT events will not be triggered if the stream was paused\nbefore the program was sent to the background.\n\n

\n

Example of listening for SIGTSTP:\n\n

\n
rl.on('SIGTSTP', function() {\n  // This will override SIGTSTP and prevent the program from going to the\n  // background.\n  console.log('Caught SIGTSTP.');\n});
\n", "params": [] }, { "textRaw": "Event: 'SIGCONT'", "type": "event", "name": "SIGCONT", "desc": "

function () {}\n\n

\n

This does not work on Windows.\n\n

\n

Emitted whenever the input stream is sent to the background with ^Z,\nrespectively known as SIGTSTP, and then continued with fg(1). This event\nonly emits if the stream was not paused before sending the program to the\nbackground.\n\n

\n

Example of listening for SIGCONT:\n\n

\n
rl.on('SIGCONT', function() {\n  // `prompt` will automatically resume the stream\n  rl.prompt();\n});
\n

Example: Tiny CLI

\n

Here's an example of how to use all these together to craft a tiny command\nline interface:\n\n

\n
var readline = require('readline'),\n    rl = readline.createInterface(process.stdin, process.stdout);\n\nrl.setPrompt('OHAI> ');\nrl.prompt();\n\nrl.on('line', function(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', function() {\n  console.log('Have a great day!');\n  process.exit(0);\n});
\n", "params": [] } ], "type": "module", "displayName": "Events" } ], "type": "module", "displayName": "Readline" } ] }