916 lines
57 KiB
HTML
916 lines
57 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width">
|
|
<meta name="nodejs.org:node-version" content="v12.22.12">
|
|
<title>Readline | Node.js v12.22.12 Documentation</title>
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
|
|
<link rel="stylesheet" href="assets/style.css">
|
|
<link rel="stylesheet" href="assets/hljs.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 href="documentation.html" class="nav-documentation">About this documentation</a></li>
|
|
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
|
|
</ul>
|
|
<hr class="line">
|
|
<ul>
|
|
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
|
|
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
|
|
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
|
|
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
|
|
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons with N-API</a></li>
|
|
<li><a href="embedding.html" class="nav-embedding">C++ Embedder API</a></li>
|
|
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
|
|
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
|
|
<li><a href="cli.html" class="nav-cli">Command line options</a></li>
|
|
<li><a href="console.html" class="nav-console">Console</a></li>
|
|
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
|
|
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
|
|
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
|
|
<li><a href="dns.html" class="nav-dns">DNS</a></li>
|
|
<li><a href="domain.html" class="nav-domain">Domain</a></li>
|
|
<li><a href="errors.html" class="nav-errors">Errors</a></li>
|
|
<li><a href="events.html" class="nav-events">Events</a></li>
|
|
<li><a href="fs.html" class="nav-fs">File system</a></li>
|
|
<li><a href="globals.html" class="nav-globals">Globals</a></li>
|
|
<li><a href="http.html" class="nav-http">HTTP</a></li>
|
|
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
|
|
<li><a href="https.html" class="nav-https">HTTPS</a></li>
|
|
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
|
|
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
|
|
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
|
|
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
|
|
<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
|
|
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
|
|
<li><a href="net.html" class="nav-net">Net</a></li>
|
|
<li><a href="os.html" class="nav-os">OS</a></li>
|
|
<li><a href="path.html" class="nav-path">Path</a></li>
|
|
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
|
|
<li><a href="policy.html" class="nav-policy">Policies</a></li>
|
|
<li><a href="process.html" class="nav-process">Process</a></li>
|
|
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
|
|
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
|
|
<li><a href="readline.html" class="nav-readline active">Readline</a></li>
|
|
<li><a href="repl.html" class="nav-repl">REPL</a></li>
|
|
<li><a href="report.html" class="nav-report">Report</a></li>
|
|
<li><a href="stream.html" class="nav-stream">Stream</a></li>
|
|
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
|
|
<li><a href="timers.html" class="nav-timers">Timers</a></li>
|
|
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
|
|
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
|
|
<li><a href="tty.html" class="nav-tty">TTY</a></li>
|
|
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
|
|
<li><a href="url.html" class="nav-url">URL</a></li>
|
|
<li><a href="util.html" class="nav-util">Utilities</a></li>
|
|
<li><a href="v8.html" class="nav-v8">V8</a></li>
|
|
<li><a href="vm.html" class="nav-vm">VM</a></li>
|
|
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
|
|
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
|
|
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
|
|
</ul>
|
|
<hr class="line">
|
|
<ul>
|
|
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div id="column1" data-id="readline" class="interior">
|
|
<header>
|
|
<h1>Node.js v12.22.12 Documentation</h1>
|
|
<div id="gtoc">
|
|
<ul>
|
|
<li>
|
|
<a href="index.html">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-v17.x/api/readline.html">17.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v16.x/api/readline.html">16.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v15.x/api/readline.html">15.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v14.x/api/readline.html">14.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v13.x/api/readline.html">13.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v12.x/api/readline.html">12.x <b>LTS</b></a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v11.x/api/readline.html">11.x</a></li>
|
|
<li><a href="https://nodejs.org/docs/latest-v10.x/api/readline.html">10.x</a></li>
|
|
<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</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</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</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>
|
|
|
|
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/readline.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></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><a href="#readline_class_interface">Class: <code>Interface</code></a>
|
|
<ul>
|
|
<li><a href="#readline_event_close">Event: <code>'close'</code></a></li>
|
|
<li><a href="#readline_event_line">Event: <code>'line'</code></a></li>
|
|
<li><a href="#readline_event_pause">Event: <code>'pause'</code></a></li>
|
|
<li><a href="#readline_event_resume">Event: <code>'resume'</code></a></li>
|
|
<li><a href="#readline_event_sigcont">Event: <code>'SIGCONT'</code></a></li>
|
|
<li><a href="#readline_event_sigint">Event: <code>'SIGINT'</code></a></li>
|
|
<li><a href="#readline_event_sigtstp">Event: <code>'SIGTSTP'</code></a></li>
|
|
<li><a href="#readline_rl_close"><code>rl.close()</code></a></li>
|
|
<li><a href="#readline_rl_pause"><code>rl.pause()</code></a></li>
|
|
<li><a href="#readline_rl_prompt_preservecursor"><code>rl.prompt([preserveCursor])</code></a></li>
|
|
<li><a href="#readline_rl_question_query_callback"><code>rl.question(query, callback)</code></a></li>
|
|
<li><a href="#readline_rl_resume"><code>rl.resume()</code></a></li>
|
|
<li><a href="#readline_rl_setprompt_prompt"><code>rl.setPrompt(prompt)</code></a></li>
|
|
<li><a href="#readline_rl_write_data_key"><code>rl.write(data[, key])</code></a></li>
|
|
<li><a href="#readline_rl_symbol_asynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li>
|
|
<li><a href="#readline_rl_line">rl.line</a></li>
|
|
<li><a href="#readline_rl_cursor">rl.cursor</a></li>
|
|
<li><a href="#readline_rl_getcursorpos"><code>rl.getCursorPos()</code></a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#readline_readline_clearline_stream_dir_callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li>
|
|
<li><a href="#readline_readline_clearscreendown_stream_callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li>
|
|
<li><a href="#readline_readline_createinterface_options"><code>readline.createInterface(options)</code></a>
|
|
<ul>
|
|
<li><a href="#readline_use_of_the_completer_function">Use of the <code>completer</code> function</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#readline_readline_cursorto_stream_x_y_callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li>
|
|
<li><a href="#readline_readline_emitkeypressevents_stream_interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li>
|
|
<li><a href="#readline_readline_movecursor_stream_dx_dy_callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li>
|
|
<li><a href="#readline_example_tiny_cli">Example: Tiny CLI</a></li>
|
|
<li><a href="#readline_example_read_file_stream_line_by_line">Example: Read file stream line-by-Line</a></li>
|
|
<li><a href="#readline_tty_keybindings">TTY keybindings</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div id="apicontent">
|
|
<h1>Readline<span><a class="mark" href="#readline_readline" id="readline_readline">#</a></span></h1>
|
|
|
|
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
|
|
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v12.22.12/lib/readline.js">lib/readline.js</a></p>
|
|
<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="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);</code></pre>
|
|
<p>The following simple example illustrates the basic use of the <code>readline</code> module.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-attr">input</span>: process.stdin,
|
|
<span class="hljs-attr">output</span>: process.stdout
|
|
});
|
|
|
|
rl.question(<span class="hljs-string">'What do you think of Node.js? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> {
|
|
<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database</span>
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>);
|
|
|
|
rl.close();
|
|
});</code></pre>
|
|
<p>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: <code>Interface</code><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>
|
|
<ul>
|
|
<li>Extends: <a href="events.html#events_class_eventemitter" class="type"><EventEmitter></a></li>
|
|
</ul>
|
|
<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: <code>'close'</code><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: <code>'line'</code><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>
|
|
<pre><code class="language-js">rl.on(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">input</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received: <span class="hljs-subst">${input}</span>`</span>);
|
|
});</code></pre>
|
|
<h3>Event: <code>'pause'</code><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>
|
|
<pre><code class="language-js">rl.on(<span class="hljs-string">'pause'</span>, <span class="hljs-function">() =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Readline paused.'</span>);
|
|
});</code></pre>
|
|
<h3>Event: <code>'resume'</code><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="language-js">rl.on(<span class="hljs-string">'resume'</span>, <span class="hljs-function">() =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Readline resumed.'</span>);
|
|
});</code></pre>
|
|
<h3>Event: <code>'SIGCONT'</code><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.1p.html"><code>fg(1p)</code></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>
|
|
<pre><code class="language-js">rl.on(<span class="hljs-string">'SIGCONT'</span>, <span class="hljs-function">() =></span> {
|
|
<span class="hljs-comment">// `prompt` will automatically resume the stream</span>
|
|
rl.prompt();
|
|
});</code></pre>
|
|
<p>The <code>'SIGCONT'</code> event is <em>not</em> supported on Windows.</p>
|
|
<h3>Event: <code>'SIGINT'</code><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>
|
|
<pre><code class="language-js">rl.on(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-function">() =></span> {
|
|
rl.question(<span class="hljs-string">'Are you sure you want to exit? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> {
|
|
<span class="hljs-keyword">if</span> (answer.match(<span class="hljs-regexp">/^y(es)?$/i</span>)) rl.pause();
|
|
});
|
|
});</code></pre>
|
|
<h3>Event: <code>'SIGTSTP'</code><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.1p.html"><code>fg(1p)</code></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>
|
|
<pre><code class="language-js">rl.on(<span class="hljs-string">'SIGTSTP'</span>, <span class="hljs-function">() =></span> {
|
|
<span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the</span>
|
|
<span class="hljs-comment">// background.</span>
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Caught SIGTSTP.'</span>);
|
|
});</code></pre>
|
|
<p>The <code>'SIGTSTP'</code> event is <em>not</em> supported on Windows.</p>
|
|
<h3><code>rl.close()</code><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>
|
|
<p>Calling <code>rl.close()</code> does not immediately stop other events (including <code>'line'</code>)
|
|
from being emitted by the <code>readline.Interface</code> instance.</p>
|
|
<h3><code>rl.pause()</code><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><code>rl.prompt([preserveCursor])</code><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><code>rl.question(query, callback)</code><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="language-js">rl.question(<span class="hljs-string">'What is your favorite food? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>);
|
|
});</code></pre>
|
|
<p>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><code>rl.resume()</code><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><code>rl.setPrompt(prompt)</code><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><code>rl.write(data[, key])</code><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. See <a href="#readline_tty_keybindings">TTY keybindings</a> for a list of key
|
|
combinations.</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>
|
|
<pre><code class="language-js">rl.write(<span class="hljs-string">'Delete this!'</span>);
|
|
<span class="hljs-comment">// Simulate Ctrl+u to delete the line written previously</span>
|
|
rl.write(<span class="hljs-literal">null</span>, { <span class="hljs-attr">ctrl</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">'u'</span> });</code></pre>
|
|
<p>The <code>rl.write()</code> method will write the data to the <code>readline</code> <code>Interface</code>'s
|
|
<code>input</code> <em>as if it were provided by the user</em>.</p>
|
|
<h3><code>rl[Symbol.asyncIterator]()</code><span><a class="mark" href="#readline_rl_symbol_asynciterator" id="readline_rl_symbol_asynciterator">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v11.14.0</td>
|
|
<td><p>Symbol.asyncIterator support is no longer experimental.</p></td></tr>
|
|
<tr><td>v11.4.0</td>
|
|
<td><p><span>Added in: v11.4.0</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li>
|
|
</ul>
|
|
<p>Create an <code>AsyncIterator</code> object that iterates through each line in the input
|
|
stream as a string. This method allows asynchronous iteration of
|
|
<code>readline.Interface</code> objects through <code>for await...of</code> loops.</p>
|
|
<p>Errors in the input stream are not forwarded.</p>
|
|
<p>If the loop is terminated with <code>break</code>, <code>throw</code>, or <code>return</code>,
|
|
<a href="#readline_rl_close"><code>rl.close()</code></a> will be called. In other words, iterating over a
|
|
<code>readline.Interface</code> will always consume the input stream fully.</p>
|
|
<p>Performance is not on par with the traditional <code>'line'</code> event API. Use <code>'line'</code>
|
|
instead for performance-sensitive applications.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processLineByLine</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-comment">// ...</span>
|
|
});
|
|
|
|
<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) {
|
|
<span class="hljs-comment">// Each line in the readline input will be successively available here as</span>
|
|
<span class="hljs-comment">// `line`.</span>
|
|
}
|
|
}</code></pre>
|
|
<p><code>readline.createInterface()</code> will start to consume the input stream once
|
|
invoked. Having asynchronous operations between interface creation and
|
|
asynchronous iteration may result in missed lines.</p>
|
|
<h3>rl.line<span><a class="mark" href="#readline_rl_line" id="readline_rl_line">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.1.98</span>
|
|
</div>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
|
|
</ul>
|
|
<p>The current input data being processed by node.</p>
|
|
<p>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 <code>line</code> event
|
|
being emitted. Once the <code>line</code> event has been emitted, this property will
|
|
be an empty string.</p>
|
|
<p>Be aware that modifying the value during the instance runtime may have
|
|
unintended consequences if <code>rl.cursor</code> is not also controlled.</p>
|
|
<p><strong>If not using a TTY stream for input, use the <a href="#readline_event_line"><code>'line'</code></a> event.</strong></p>
|
|
<p>One possible use case would be as follows:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'lorem ipsum'</span>, <span class="hljs-string">'dolor sit amet'</span>];
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface(process.stdin);
|
|
<span class="hljs-keyword">const</span> showResults = debounce(<span class="hljs-function">() =></span> {
|
|
<span class="hljs-built_in">console</span>.log(
|
|
<span class="hljs-string">'\n'</span>,
|
|
values.filter(<span class="hljs-function">(<span class="hljs-params">val</span>) =></span> val.startsWith(rl.line)).join(<span class="hljs-string">' '</span>)
|
|
);
|
|
}, <span class="hljs-number">300</span>);
|
|
process.stdin.on(<span class="hljs-string">'keypress'</span>, <span class="hljs-function">(<span class="hljs-params">c, k</span>) =></span> {
|
|
showResults();
|
|
});</code></pre>
|
|
<h3>rl.cursor<span><a class="mark" href="#readline_rl_cursor" id="readline_rl_cursor">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v0.1.98</span>
|
|
</div>
|
|
<ul>
|
|
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
|
|
</ul>
|
|
<p>The cursor position relative to <code>rl.line</code>.</p>
|
|
<p>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.</p>
|
|
<h3><code>rl.getCursorPos()</code><span><a class="mark" href="#readline_rl_getcursorpos" id="readline_rl_getcursorpos">#</a></span></h3>
|
|
<div class="api_metadata">
|
|
<span>Added in: v12.16.0</span>
|
|
</div>
|
|
<ul>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
|
|
<ul>
|
|
<li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the row of the prompt the cursor currently lands on</li>
|
|
<li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the screen column the cursor currently lands on</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<p>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.</p>
|
|
<h2><code>readline.clearLine(stream, dir[, callback])</code><span><a class="mark" href="#readline_readline_clearline_stream_dir_callback" id="readline_readline_clearline_stream_dir_callback">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.7.0</td>
|
|
<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
|
|
<tr><td>v0.7.7</td>
|
|
<td><p><span>Added in: v0.7.7</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</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>
|
|
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
|
|
the <code>'drain'</code> event to be emitted before continuing to write additional data;
|
|
otherwise <code>true</code>.</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><code>readline.clearScreenDown(stream[, callback])</code><span><a class="mark" href="#readline_readline_clearscreendown_stream_callback" id="readline_readline_clearscreendown_stream_callback">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.7.0</td>
|
|
<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
|
|
<tr><td>v0.7.7</td>
|
|
<td><p><span>Added in: v0.7.7</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</div>
|
|
<ul>
|
|
<li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li>
|
|
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
|
|
the <code>'drain'</code> event to be emitted before continuing to write additional data;
|
|
otherwise <code>true</code>.</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><code>readline.createInterface(options)</code><span><a class="mark" href="#readline_readline_createinterface_options" id="readline_readline_createinterface_options">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v8.3.0, 6.11.4</td>
|
|
<td><p>Remove max limit of <code>crlfDelay</code> option.</p></td></tr>
|
|
<tr><td>v6.6.0</td>
|
|
<td><p>The <code>crlfDelay</code> option is supported now.</p></td></tr>
|
|
<tr><td>v6.3.0</td>
|
|
<td><p>The <code>prompt</code> option is supported now.</p></td></tr>
|
|
<tr><td>v6.0.0</td>
|
|
<td><p>The <code>historySize</code> option can be <code>0</code> now.</p></td></tr>
|
|
<tr><td>v0.1.98</td>
|
|
<td><p><span>Added in: v0.1.98</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</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.
|
|
<strong>Default:</strong> 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>. 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.
|
|
<strong>Default:</strong> <code>30</code>.</li>
|
|
<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The prompt string to use. <strong>Default:</strong> <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. <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 (which may be reasonable for
|
|
<a href="#readline_example_read_file_stream_line_by_line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</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. <strong>Default:</strong> <code>false</code>.</li>
|
|
<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration <code>readline</code> will wait for a
|
|
character (when reading an ambiguous key sequence in milliseconds one that
|
|
can both form a complete key sequence using the input read so far and can
|
|
take additional input to complete a longer key sequence).
|
|
<strong>Default:</strong> <code>500</code>.</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<p>The <code>readline.createInterface()</code> method creates a new <code>readline.Interface</code>
|
|
instance.</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-attr">input</span>: process.stdin,
|
|
<span class="hljs-attr">output</span>: 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="language-js">rl.on(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received: <span class="hljs-subst">${line}</span>`</span>);
|
|
});</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 <code>Array</code> with 2 entries:</p>
|
|
<ul>
|
|
<li>An <code>Array</code> 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="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">completer</span>(<span class="hljs-params">line</span>) </span>{
|
|
<span class="hljs-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span>.split(<span class="hljs-string">' '</span>);
|
|
<span class="hljs-keyword">const</span> hits = completions.filter(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.startsWith(line));
|
|
<span class="hljs-comment">// Show all completions if none found</span>
|
|
<span class="hljs-keyword">return</span> [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="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">completer</span>(<span class="hljs-params">linePartial, callback</span>) </span>{
|
|
callback(<span class="hljs-literal">null</span>, [[<span class="hljs-string">'123'</span>], linePartial]);
|
|
}</code></pre>
|
|
<h2><code>readline.cursorTo(stream, x[, y][, callback])</code><span><a class="mark" href="#readline_readline_cursorto_stream_x_y_callback" id="readline_readline_cursorto_stream_x_y_callback">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.7.0</td>
|
|
<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
|
|
<tr><td>v0.7.7</td>
|
|
<td><p><span>Added in: v0.7.7</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</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>
|
|
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
|
|
the <code>'drain'</code> event to be emitted before continuing to write additional data;
|
|
otherwise <code>true</code>.</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><code>readline.emitKeypressEvents(stream[, interface])</code><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>
|
|
stream 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>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="language-js">readline.emitKeypressEvents(process.stdin);
|
|
<span class="hljs-keyword">if</span> (process.stdin.isTTY)
|
|
process.stdin.setRawMode(<span class="hljs-literal">true</span>);</code></pre>
|
|
<h2><code>readline.moveCursor(stream, dx, dy[, callback])</code><span><a class="mark" href="#readline_readline_movecursor_stream_dx_dy_callback" id="readline_readline_movecursor_stream_dx_dy_callback">#</a></span></h2>
|
|
<div class="api_metadata">
|
|
<details class="changelog"><summary>History</summary>
|
|
<table>
|
|
<tbody><tr><th>Version</th><th>Changes</th></tr>
|
|
<tr><td>v12.7.0</td>
|
|
<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
|
|
<tr><td>v0.7.7</td>
|
|
<td><p><span>Added in: v0.7.7</span></p></td></tr>
|
|
</tbody></table>
|
|
</details>
|
|
</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>
|
|
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li>
|
|
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
|
|
the <code>'drain'</code> event to be emitted before continuing to write additional data;
|
|
otherwise <code>true</code>.</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="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-attr">input</span>: process.stdin,
|
|
<span class="hljs-attr">output</span>: process.stdout,
|
|
<span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>
|
|
});
|
|
|
|
rl.prompt();
|
|
|
|
rl.on(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
|
|
<span class="hljs-keyword">switch</span> (line.trim()) {
|
|
<span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>:
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'world!'</span>);
|
|
<span class="hljs-keyword">break</span>;
|
|
<span class="hljs-keyword">default</span>:
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Say what? I might have heard '<span class="hljs-subst">${line.trim()}</span>'`</span>);
|
|
<span class="hljs-keyword">break</span>;
|
|
}
|
|
rl.prompt();
|
|
}).on(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Have a great day!'</span>);
|
|
process.exit(<span class="hljs-number">0</span>);
|
|
});</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 an input file one line at a
|
|
time. The easiest way to do so is leveraging the <a href="fs.html#fs_class_fs_readstream"><code>fs.ReadStream</code></a> API as
|
|
well as a <code>for await...of</code> loop:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
|
|
<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
|
|
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processLineByLine</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">const</span> fileStream = fs.createReadStream(<span class="hljs-string">'input.txt'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-attr">input</span>: fileStream,
|
|
<span class="hljs-attr">crlfDelay</span>: <span class="hljs-literal">Infinity</span>
|
|
});
|
|
<span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR LF</span>
|
|
<span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span>
|
|
|
|
<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) {
|
|
<span class="hljs-comment">// Each line in input.txt will be successively available here as `line`.</span>
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>);
|
|
}
|
|
}
|
|
|
|
processLineByLine();</code></pre>
|
|
<p>Alternatively, one could use the <a href="#readline_event_line"><code>'line'</code></a> event:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
|
|
<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
|
|
<span class="hljs-keyword">const</span> rl = readline.createInterface({
|
|
<span class="hljs-attr">input</span>: fs.createReadStream(<span class="hljs-string">'sample.txt'</span>),
|
|
<span class="hljs-attr">crlfDelay</span>: <span class="hljs-literal">Infinity</span>
|
|
});
|
|
|
|
rl.on(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>);
|
|
});</code></pre>
|
|
<p>Currently, <code>for await...of</code> loop can be a bit slower. If <code>async</code> / <code>await</code>
|
|
flow and speed are both essential, a mixed approach can be applied:</p>
|
|
<pre><code class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
|
|
<span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
|
|
<span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
|
|
|
|
(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processLineByLine</span>(<span class="hljs-params"></span>) </span>{
|
|
<span class="hljs-keyword">try</span> {
|
|
<span class="hljs-keyword">const</span> rl = createInterface({
|
|
<span class="hljs-attr">input</span>: createReadStream(<span class="hljs-string">'big-file.txt'</span>),
|
|
<span class="hljs-attr">crlfDelay</span>: <span class="hljs-literal">Infinity</span>
|
|
});
|
|
|
|
rl.on(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
|
|
<span class="hljs-comment">// Process the line.</span>
|
|
});
|
|
|
|
<span class="hljs-keyword">await</span> once(rl, <span class="hljs-string">'close'</span>);
|
|
|
|
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'File processed.'</span>);
|
|
} <span class="hljs-keyword">catch</span> (err) {
|
|
<span class="hljs-built_in">console</span>.error(err);
|
|
}
|
|
})();</code></pre>
|
|
<h2>TTY keybindings<span><a class="mark" href="#readline_tty_keybindings" id="readline_tty_keybindings">#</a></span></h2>
|
|
<table>
|
|
<tbody><tr>
|
|
<th>Keybindings</th>
|
|
<th>Description</th>
|
|
<th>Notes</th>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>shift</code> + <code>backspace</code></td>
|
|
<td>Delete line left</td>
|
|
<td>Doesn't work on Linux, Mac and Windows</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>shift</code> + <code>delete</code></td>
|
|
<td>Delete line right</td>
|
|
<td>Doesn't work on Mac</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>c</code></td>
|
|
<td>Emit <code>SIGINT</code> or close the readline instance</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>h</code></td>
|
|
<td>Delete left</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>d</code></td>
|
|
<td>Delete right or close the readline instance in case the current line is empty / EOF</td>
|
|
<td>Doesn't work on Windows</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>u</code></td>
|
|
<td>Delete from the current position to the line start</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>k</code></td>
|
|
<td>Delete from the current position to the end of line</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>a</code></td>
|
|
<td>Go to start of line</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>e</code></td>
|
|
<td>Go to to end of line</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>b</code></td>
|
|
<td>Back one character</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>f</code></td>
|
|
<td>Forward one character</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>l</code></td>
|
|
<td>Clear screen</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>n</code></td>
|
|
<td>Next history item</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>p</code></td>
|
|
<td>Previous history item</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>z</code></td>
|
|
<td>Moves running process into background. Type
|
|
<code>fg</code> and press <code>enter</code>
|
|
to return.</td>
|
|
<td>Doesn't work on Windows</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>w</code> or <code>ctrl</code>
|
|
+ <code>backspace</code></td>
|
|
<td>Delete backward to a word boundary</td>
|
|
<td><code>ctrl</code> + <code>backspace</code> Doesn't
|
|
work on Linux, Mac and Windows</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>delete</code></td>
|
|
<td>Delete forward to a word boundary</td>
|
|
<td>Doesn't work on Mac</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>left</code> or
|
|
<code>meta</code> + <code>b</code></td>
|
|
<td>Word left</td>
|
|
<td><code>ctrl</code> + <code>left</code> Doesn't work
|
|
on Mac</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>ctrl</code> + <code>right</code> or
|
|
<code>meta</code> + <code>f</code></td>
|
|
<td>Word right</td>
|
|
<td><code>ctrl</code> + <code>right</code> Doesn't work
|
|
on Mac</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>meta</code> + <code>d</code> or <code>meta</code>
|
|
+ <code>delete</code></td>
|
|
<td>Delete word right</td>
|
|
<td><code>meta</code> + <code>delete</code> Doesn't work
|
|
on windows</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>meta</code> + <code>backspace</code></td>
|
|
<td>Delete word left</td>
|
|
<td>Doesn't work on Mac</td>
|
|
</tr>
|
|
</tbody></table>
|
|
<!-- API END -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|