<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Readline | Node.js v6.17.1 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="https://nodejs.org/api/readline.html"> </head> <body class="alt apidoc" id="api-section-readline"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a class="nav-documentation" href="documentation.html">About these Docs</a></li> <li><a class="nav-synopsis" href="synopsis.html">Usage & Example</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-assert" href="assert.html">Assertion Testing</a></li> <li><a class="nav-buffer" href="buffer.html">Buffer</a></li> <li><a class="nav-addons" href="addons.html">C++ Addons</a></li> <li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li> <li><a class="nav-child_process" href="child_process.html">Child Processes</a></li> <li><a class="nav-cluster" href="cluster.html">Cluster</a></li> <li><a class="nav-cli" href="cli.html">Command Line Options</a></li> <li><a class="nav-console" href="console.html">Console</a></li> <li><a class="nav-crypto" href="crypto.html">Crypto</a></li> <li><a class="nav-debugger" href="debugger.html">Debugger</a></li> <li><a class="nav-dns" href="dns.html">DNS</a></li> <li><a class="nav-domain" href="domain.html">Domain</a></li> <li><a class="nav-errors" href="errors.html">Errors</a></li> <li><a class="nav-events" href="events.html">Events</a></li> <li><a class="nav-fs" href="fs.html">File System</a></li> <li><a class="nav-globals" href="globals.html">Globals</a></li> <li><a class="nav-http" href="http.html">HTTP</a></li> <li><a class="nav-https" href="https.html">HTTPS</a></li> <li><a class="nav-intl" href="intl.html">Internationalization</a></li> <li><a class="nav-modules" href="modules.html">Modules</a></li> <li><a class="nav-net" href="net.html">Net</a></li> <li><a class="nav-os" href="os.html">OS</a></li> <li><a class="nav-path" href="path.html">Path</a></li> <li><a class="nav-process" href="process.html">Process</a></li> <li><a class="nav-punycode" href="punycode.html">Punycode</a></li> <li><a class="nav-querystring" href="querystring.html">Query Strings</a></li> <li><a class="nav-readline active" href="readline.html">Readline</a></li> <li><a class="nav-repl" href="repl.html">REPL</a></li> <li><a class="nav-stream" href="stream.html">Stream</a></li> <li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li> <li><a class="nav-timers" href="timers.html">Timers</a></li> <li><a class="nav-tls" href="tls.html">TLS/SSL</a></li> <li><a class="nav-tty" href="tty.html">TTY</a></li> <li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li> <li><a class="nav-url" href="url.html">URL</a></li> <li><a class="nav-util" href="util.html">Utilities</a></li> <li><a class="nav-v8" href="v8.html">V8</a></li> <li><a class="nav-vm" href="vm.html">VM</a></li> <li><a class="nav-zlib" href="zlib.html">ZLIB</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo & Issue Tracker</a></li> <li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li> </ul> </div> <div id="column1" data-id="readline" class="interior"> <header> <h1>Node.js v6.17.1 Documentation</h1> <div id="gtoc"> <ul> <li> <a href="index.html" name="toc">Index</a> </li> <li> <a href="all.html">View on single page</a> </li> <li> <a href="readline.json">View as JSON</a> </li> <li class="version-picker"> <a href="#">View another version <span>▼</span></a> <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/readline.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/readline.html">8.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/readline.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/readline.html">6.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/readline.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/readline.html">4.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/readline.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/readline.html">0.10.x</a></li></ol> </li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li><span class="stability_2"><a href="#readline_readline">Readline</a></span><ul> <li><span class="stability_undefined"><a href="#readline_class_interface">Class: Interface</a></span><ul> <li><span class="stability_undefined"><a href="#readline_event_close">Event: 'close'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_line">Event: 'line'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_pause">Event: 'pause'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_resume">Event: 'resume'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_sigcont">Event: 'SIGCONT'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_sigint">Event: 'SIGINT'</a></span></li> <li><span class="stability_undefined"><a href="#readline_event_sigtstp">Event: 'SIGTSTP'</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_close">rl.close()</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_pause">rl.pause()</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_prompt_preservecursor">rl.prompt([preserveCursor])</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_question_query_callback">rl.question(query, callback)</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_resume">rl.resume()</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_setprompt_prompt">rl.setPrompt(prompt)</a></span></li> <li><span class="stability_undefined"><a href="#readline_rl_write_data_key">rl.write(data[, key])</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#readline_readline_clearline_stream_dir">readline.clearLine(stream, dir)</a></span></li> <li><span class="stability_undefined"><a href="#readline_readline_clearscreendown_stream">readline.clearScreenDown(stream)</a></span></li> <li><span class="stability_undefined"><a href="#readline_readline_createinterface_options">readline.createInterface(options)</a></span><ul> <li><span class="stability_undefined"><a href="#readline_use_of_the_completer_function">Use of the <code>completer</code> Function</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#readline_readline_cursorto_stream_x_y">readline.cursorTo(stream, x, y)</a></span></li> <li><span class="stability_undefined"><a href="#readline_readline_emitkeypressevents_stream_interface">readline.emitKeypressEvents(stream[, interface])</a></span></li> <li><span class="stability_undefined"><a href="#readline_readline_movecursor_stream_dx_dy">readline.moveCursor(stream, dx, dy)</a></span></li> <li><span class="stability_undefined"><a href="#readline_example_tiny_cli">Example: Tiny CLI</a></span></li> <li><span class="stability_undefined"><a href="#readline_example_read_file_stream_line_by_line">Example: Read File Stream Line-by-Line</a></span></li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>Readline<span><a class="mark" href="#readline_readline"id="readline_readline">#</a></span></h1> <!--introduced_in=v0.10.0--> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>The <code>readline</code> module provides an interface for reading data from a <a href="stream.html#stream_readable_streams">Readable</a> stream (such as <a href="process.html#process_process_stdin"><code>process.stdin</code></a>) one line at a time. It can be accessed using:</p> <pre><code class="lang-js">const readline = require('readline'); </code></pre> <p>The following simple example illustrates the basic use of the <code>readline</code> module.</p> <pre><code class="lang-js">const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('What do you think of Node.js? ', (answer) => { // TODO: Log the answer in a database console.log(`Thank you for your valuable feedback: ${answer}`); rl.close(); }); </code></pre> <p><em>Note</em> Once this code is invoked, the Node.js application will not terminate until the <code>readline.Interface</code> is closed because the interface waits for data to be received on the <code>input</code> stream.</p> <h2>Class: Interface<span><a class="mark" href="#readline_class_interface"id="readline_class_interface">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.104</span> </div><p>Instances of the <code>readline.Interface</code> class are constructed using the <code>readline.createInterface()</code> method. Every instance is associated with a single <code>input</code> <a href="stream.html#stream_readable_streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#stream_writable_streams">Writable</a> stream. The <code>output</code> stream is used to print prompts for user input that arrives on, and is read from, the <code>input</code> stream.</p> <h3>Event: 'close'<span><a class="mark" href="#readline_event_close"id="readline_event_close">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><p>The <code>'close'</code> event is emitted when one of the following occur:</p> <ul> <li>The <code>rl.close()</code> method is called and the <code>readline.Interface</code> instance has relinquished control over the <code>input</code> and <code>output</code> streams;</li> <li>The <code>input</code> stream receives its <code>'end'</code> event;</li> <li>The <code>input</code> stream receives <code><ctrl>-D</code> to signal end-of-transmission (EOT);</li> <li>The <code>input</code> stream receives <code><ctrl>-C</code> to signal <code>SIGINT</code> and there is no <code>SIGINT</code> event listener registered on the <code>readline.Interface</code> instance.</li> </ul> <p>The listener function is called without passing any arguments.</p> <p>The <code>readline.Interface</code> instance is finished once the <code>'close'</code> event is emitted.</p> <h3>Event: 'line'<span><a class="mark" href="#readline_event_line"id="readline_event_line">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><p>The <code>'line'</code> event is emitted whenever the <code>input</code> stream receives an end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs when the user presses the <code><Enter></code>, or <code><Return></code> keys.</p> <p>The listener function is called with a string containing the single line of received input.</p> <p>For example:</p> <pre><code class="lang-js">rl.on('line', (input) => { console.log(`Received: ${input}`); }); </code></pre> <h3>Event: 'pause'<span><a class="mark" href="#readline_event_pause"id="readline_event_pause">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.7.5</span> </div><p>The <code>'pause'</code> event is emitted when one of the following occur:</p> <ul> <li>The <code>input</code> stream is paused.</li> <li>The <code>input</code> stream is not paused and receives the <code>SIGCONT</code> event. (See events <a href="readline.html#readline_event_sigtstp"><code>SIGTSTP</code></a> and <a href="readline.html#readline_event_sigcont"><code>SIGCONT</code></a>)</li> </ul> <p>The listener function is called without passing any arguments.</p> <p>For example:</p> <pre><code class="lang-js">rl.on('pause', () => { console.log('Readline paused.'); }); </code></pre> <h3>Event: 'resume'<span><a class="mark" href="#readline_event_resume"id="readline_event_resume">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.7.5</span> </div><p>The <code>'resume'</code> event is emitted whenever the <code>input</code> stream is resumed.</p> <p>The listener function is called without passing any arguments.</p> <pre><code class="lang-js">rl.on('resume', () => { console.log('Readline resumed.'); }); </code></pre> <h3>Event: 'SIGCONT'<span><a class="mark" href="#readline_event_sigcont"id="readline_event_sigcont">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.7.5</span> </div><p>The <code>'SIGCONT'</code> event is emitted when a Node.js process previously moved into the background using <code><ctrl>-Z</code> (i.e. <code>SIGTSTP</code>) is then brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1.html">fg(1)</a>.</p> <p>If the <code>input</code> stream was paused <em>before</em> the <code>SIGTSTP</code> request, this event will not be emitted.</p> <p>The listener function is invoked without passing any arguments.</p> <p>For example:</p> <pre><code class="lang-js">rl.on('SIGCONT', () => { // `prompt` will automatically resume the stream rl.prompt(); }); </code></pre> <p><em>Note</em>: The <code>'SIGCONT'</code> event is <em>not</em> supported on Windows.</p> <h3>Event: 'SIGINT'<span><a class="mark" href="#readline_event_sigint"id="readline_event_sigint">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.3.0</span> </div><p>The <code>'SIGINT'</code> event is emitted whenever the <code>input</code> stream receives a <code><ctrl>-C</code> input, known typically as <code>SIGINT</code>. If there are no <code>'SIGINT'</code> event listeners registered when the <code>input</code> stream receives a <code>SIGINT</code>, the <code>'pause'</code> event will be emitted.</p> <p>The listener function is invoked without passing any arguments.</p> <p>For example:</p> <pre><code class="lang-js">rl.on('SIGINT', () => { rl.question('Are you sure you want to exit? ', (answer) => { if (answer.match(/^y(es)?$/i)) rl.pause(); }); }); </code></pre> <h3>Event: 'SIGTSTP'<span><a class="mark" href="#readline_event_sigtstp"id="readline_event_sigtstp">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.7.5</span> </div><p>The <code>'SIGTSTP'</code> event is emitted when the <code>input</code> stream receives a <code><ctrl>-Z</code> input, typically known as <code>SIGTSTP</code>. If there are no <code>SIGTSTP</code> event listeners registered when the <code>input</code> stream receives a <code>SIGTSTP</code>, the Node.js process will be sent to the background.</p> <p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1.html">fg(1)</a>, the <code>'pause'</code> and <code>SIGCONT</code> events will be emitted. These can be used to resume the <code>input</code> stream.</p> <p>The <code>'pause'</code> and <code>'SIGCONT'</code> events will not be emitted if the <code>input</code> was paused before the process was sent to the background.</p> <p>The listener function is invoked without passing any arguments.</p> <p>For example:</p> <pre><code class="lang-js">rl.on('SIGTSTP', () => { // This will override SIGTSTP and prevent the program from going to the // background. console.log('Caught SIGTSTP.'); }); </code></pre> <p><em>Note</em>: The <code>'SIGTSTP'</code> event is <em>not</em> supported on Windows.</p> <h3>rl.close()<span><a class="mark" href="#readline_rl_close"id="readline_rl_close">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><p>The <code>rl.close()</code> method closes the <code>readline.Interface</code> instance and relinquishes control over the <code>input</code> and <code>output</code> streams. When called, the <code>'close'</code> event will be emitted.</p> <h3>rl.pause()<span><a class="mark" href="#readline_rl_pause"id="readline_rl_pause">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.3.4</span> </div><p>The <code>rl.pause()</code> method pauses the <code>input</code> stream, allowing it to be resumed later if necessary.</p> <p>Calling <code>rl.pause()</code> does not immediately pause other events (including <code>'line'</code>) from being emitted by the <code>readline.Interface</code> instance.</p> <h3>rl.prompt([preserveCursor])<span><a class="mark" href="#readline_rl_prompt_preservecursor"id="readline_rl_prompt_preservecursor">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><ul> <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, prevents the cursor placement from being reset to <code>0</code>.</li> </ul> <p>The <code>rl.prompt()</code> method writes the <code>readline.Interface</code> instances configured <code>prompt</code> to a new line in <code>output</code> in order to provide a user with a new location at which to provide input.</p> <p>When called, <code>rl.prompt()</code> will resume the <code>input</code> stream if it has been paused.</p> <p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or <code>undefined</code> the prompt is not written.</p> <h3>rl.question(query, callback)<span><a class="mark" href="#readline_rl_question_query_callback"id="readline_rl_question_query_callback">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.3.3</span> </div><ul> <li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the prompt.</li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback function that is invoked with the user's input in response to the <code>query</code>.</li> </ul> <p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, waits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> function passing the provided input as the first argument.</p> <p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been paused.</p> <p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or <code>undefined</code> the <code>query</code> is not written.</p> <p>Example usage:</p> <pre><code class="lang-js">rl.question('What is your favorite food? ', (answer) => { console.log(`Oh, so your favorite food is ${answer}`); }); </code></pre> <p><em>Note</em>: The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical pattern of accepting an <code>Error</code> object or <code>null</code> as the first argument. The <code>callback</code> is called with the provided answer as the only argument.</p> <h3>rl.resume()<span><a class="mark" href="#readline_rl_resume"id="readline_rl_resume">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.3.4</span> </div><p>The <code>rl.resume()</code> method resumes the <code>input</code> stream if it has been paused.</p> <h3>rl.setPrompt(prompt)<span><a class="mark" href="#readline_rl_setprompt_prompt"id="readline_rl_setprompt_prompt">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><ul> <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The <code>rl.setPrompt()</code> method sets the prompt that will be written to <code>output</code> whenever <code>rl.prompt()</code> is called.</p> <h3>rl.write(data[, key])<span><a class="mark" href="#readline_rl_write_data_key"id="readline_rl_write_data_key">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><ul> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <code><ctrl></code> key.</li> <li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <code><Meta></code> key.</li> <li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <code><Shift></code> key.</li> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the a key.</li> </ul> </li> </ul> <p>The <code>rl.write()</code> method will write either <code>data</code> or a key sequence identified by <code>key</code> to the <code>output</code>. The <code>key</code> argument is supported only if <code>output</code> is a <a href="tty.html">TTY</a> text terminal.</p> <p>If <code>key</code> is specified, <code>data</code> is ignored.</p> <p>When called, <code>rl.write()</code> will resume the <code>input</code> stream if it has been paused.</p> <p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or <code>undefined</code> the <code>data</code> and <code>key</code> are not written.</p> <p>For example:</p> <pre><code class="lang-js">rl.write('Delete this!'); // Simulate Ctrl+u to delete the line written previously rl.write(null, {ctrl: true, name: 'u'}); </code></pre> <p><em>Note</em>: The <code>rl.write()</code> method will write the data to the <code>readline</code> Interface's <code>input</code> <em>as if it were provided by the user</em>.</p> <h2>readline.clearLine(stream, dir)<span><a class="mark" href="#readline_readline_clearline_stream_dir"id="readline_readline_clearline_stream_dir">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div><ul> <li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a><ul> <li><code>-1</code> - to the left from cursor</li> <li><code>1</code> - to the right from cursor</li> <li><code>0</code> - the entire line</li> </ul> </li> </ul> <p>The <code>readline.clearLine()</code> method clears current line of given <a href="tty.html">TTY</a> stream in a specified direction identified by <code>dir</code>.</p> <h2>readline.clearScreenDown(stream)<span><a class="mark" href="#readline_readline_clearscreendown_stream"id="readline_readline_clearscreendown_stream">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div><ul> <li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> </ul> <p>The <code>readline.clearScreenDown()</code> method clears the given <a href="tty.html">TTY</a> stream from the current position of the cursor down.</p> <h2>readline.createInterface(options)<span><a class="mark" href="#readline_readline_createinterface_options"id="readline_readline_createinterface_options">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.98</span> </div><ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>input</code> <a href="stream.html#stream_class_stream_readable" class="type"><stream.Readable></a> The <a href="stream.html#stream_readable_streams">Readable</a> stream to listen to. This option is <em>required</em>.</li> <li><code>output</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a> The <a href="stream.html#stream_writable_streams">Writable</a> stream to write readline data to.</li> <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function used for Tab autocompletion.</li> <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Defaults to checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li> <li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> maximum number of history lines retained. To disable the history set this value to <code>0</code>. Defaults to <code>30</code>. This option makes sense only if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check, otherwise the history caching mechanism is not initialized at all.</li> <li><code>prompt</code> - the prompt string to use. Default: <code>'> '</code></li> <li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds <code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate end-of-line input. Default to <code>100</code> milliseconds. <code>crlfDelay</code> will be coerced to a number no less than <code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code> will always be considered a single newline.</li> <li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, when a new input line added to the history list duplicates an older one, this removes the older line from the list. Defaults to <code>false</code>.</li> </ul> </li> </ul> <p>The <code>readline.createInterface()</code> method creates a new <code>readline.Interface</code> instance.</p> <p>For example:</p> <pre><code class="lang-js">const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); </code></pre> <p>Once the <code>readline.Interface</code> instance is created, the most common case is to listen for the <code>'line'</code> event:</p> <pre><code class="lang-js">rl.on('line', (line) => { console.log(`Received: ${line}`); }); </code></pre> <p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get the best compatibility if it defines an <code>output.columns</code> property and emits a <code>'resize'</code> event on the <code>output</code> if or when the columns ever change (<a href="process.html#process_process_stdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p> <h3>Use of the <code>completer</code> Function<span><a class="mark" href="#readline_use_of_the_completer_function"id="readline_use_of_the_completer_function">#</a></span></h3> <p>The <code>completer</code> function takes the current line entered by the user as an argument, and returns an Array with 2 entries:</p> <ul> <li>An Array with matching entries for the completion.</li> <li>The substring that was used for the matching.</li> </ul> <p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p> <pre><code class="lang-js">function completer(line) { const completions = '.help .error .exit .quit .q'.split(' '); const hits = completions.filter((c) => c.startsWith(line)); // show all completions if none found return [hits.length ? hits : completions, line]; } </code></pre> <p>The <code>completer</code> function can be called asynchronously if it accepts two arguments:</p> <pre><code class="lang-js">function completer(linePartial, callback) { callback(null, [['123'], linePartial]); } </code></pre> <h2>readline.cursorTo(stream, x, y)<span><a class="mark" href="#readline_readline_cursorto_stream_x_y"id="readline_readline_cursorto_stream_x_y">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div><ul> <li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>The <code>readline.cursorTo()</code> method moves cursor to the specified position in a given <a href="tty.html">TTY</a> <code>stream</code>.</p> <h2>readline.emitKeypressEvents(stream[, interface])<span><a class="mark" href="#readline_readline_emitkeypressevents_stream_interface"id="readline_readline_emitkeypressevents_stream_interface">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div><ul> <li><code>stream</code> <a href="stream.html#stream_class_stream_readable" class="type"><stream.Readable></a></li> <li><code>interface</code> <a href="readline.html#readline_class_interface" class="type"><readline.Interface></a></li> </ul> <p>The <code>readline.emitKeypressEvents()</code> method causes the given <a href="stream.html#stream_readable_streams">Readable</a> <code>stream</code> to begin emitting <code>'keypress'</code> events corresponding to received input.</p> <p>Optionally, <code>interface</code> specifies a <code>readline.Interface</code> instance for which autocompletion is disabled when copy-pasted input is detected.</p> <p>If the <code>stream</code> is a <a href="tty.html">TTY</a>, then it must be in raw mode.</p> <p><em>Note</em>: This is automatically called by any readline instance on its <code>input</code> if the <code>input</code> is a terminal. Closing the <code>readline</code> instance does not stop the <code>input</code> from emitting <code>'keypress'</code> events.</p> <pre><code class="lang-js">readline.emitKeypressEvents(process.stdin); if (process.stdin.isTTY) process.stdin.setRawMode(true); </code></pre> <h2>readline.moveCursor(stream, dx, dy)<span><a class="mark" href="#readline_readline_movecursor_stream_dx_dy"id="readline_readline_movecursor_stream_dx_dy">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div><ul> <li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>The <code>readline.moveCursor()</code> method moves the cursor <em>relative</em> to its current position in a given <a href="tty.html">TTY</a> <code>stream</code>.</p> <h2>Example: Tiny CLI<span><a class="mark" href="#readline_example_tiny_cli"id="readline_example_tiny_cli">#</a></span></h2> <p>The following example illustrates the use of <code>readline.Interface</code> class to implement a small command-line interface:</p> <pre><code class="lang-js">const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: 'OHAI> ' }); rl.prompt(); rl.on('line', (line) => { switch (line.trim()) { case 'hello': console.log('world!'); break; default: console.log(`Say what? I might have heard '${line.trim()}'`); break; } rl.prompt(); }).on('close', () => { console.log('Have a great day!'); process.exit(0); }); </code></pre> <h2>Example: Read File Stream Line-by-Line<span><a class="mark" href="#readline_example_read_file_stream_line_by_line"id="readline_example_read_file_stream_line_by_line">#</a></span></h2> <p>A common use case for <code>readline</code> is to consume input from a filesystem <a href="stream.html#stream_readable_streams">Readable</a> stream one line at a time, as illustrated in the following example:</p> <pre><code class="lang-js">const readline = require('readline'); const fs = require('fs'); const rl = readline.createInterface({ input: fs.createReadStream('sample.txt') }); rl.on('line', (line) => { console.log(`Line from file: ${line}`); }); </code></pre> </div> </div> </div> <script src="assets/sh_main.js"></script> <script src="assets/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> <!-- __TRACKING__ --> </body> </html>