Readline
目录
自 v0.10.0 版本开始新增
源代码: lib/readline.js
The node:readline
module provides an interface for reading data from a
Readable stream (such as process.stdin
) one line at a time.
To use the promise-based APIs:
MJS
CJS
To use the callback and sync APIs:
MJS
CJS
The following simple example illustrates the basic use of the node:readline
module.
MJS
CJS
Once this code is invoked, the Node.js application will not terminate until the
readline.Interface
is closed because the interface waits for data to be
received on the input
stream.
C InterfaceConstructor
自 v0.1.104 版本开始新增
- Extends:
EventEmitter
Instances of the InterfaceConstructor
class are constructed using the
readlinePromises.createInterface()
or readline.createInterface()
method.
Every instance is associated with a single input
Readable stream and a
single output
Writable stream.
The output
stream is used to print prompts for user input that arrives on,
and is read from, the input
stream.
E 'close'
自 v0.1.98 版本开始新增
The 'close'
event is emitted when one of the following occur:
- The
rl.close()
method is called and theInterfaceConstructor
instance has relinquished control over theinput
andoutput
streams; - The
input
stream receives its'end'
event; - The
input
stream receives Ctrl+D to signal end-of-transmission (EOT); - The
input
stream receives Ctrl+C to signalSIGINT
and there is no'SIGINT'
event listener registered on theInterfaceConstructor
instance.
The listener function is called without passing any arguments.
The InterfaceConstructor
instance is finished once the 'close'
event is
emitted.
E 'line'
自 v0.1.98 版本开始新增
The 'line'
event is emitted whenever the input
stream receives an
end-of-line input (\n
, \r
, or \r\n
). This usually occurs when the user
presses Enter or Return.
The 'line'
event is also emitted if new data has been read from a stream and
that stream ends without a final end-of-line marker.
The listener function is called with a string containing the single line of received input.
JS
E 'history'
自 v15.8.0, v14.18.0 版本开始新增
The 'history'
event is emitted whenever the history array has changed.
The listener function is called with an array containing the history array.
It will reflect all changes, added lines and removed lines due to
historySize
and removeHistoryDuplicates
.
The primary purpose is to allow a listener to persist the history. It is also possible for the listener to change the history object. This could be useful to prevent certain lines to be added to the history, like a password.
JS
E 'pause'
自 v0.7.5 版本开始新增
The 'pause'
event is emitted when one of the following occur:
- The
input
stream is paused. - The
input
stream is not paused and receives the'SIGCONT'
event. (See events'SIGTSTP'
and'SIGCONT'
.)
The listener function is called without passing any arguments.
JS
E 'resume'
自 v0.7.5 版本开始新增
The 'resume'
event is emitted whenever the input
stream is resumed.
The listener function is called without passing any arguments.
JS
E 'SIGCONT'
自 v0.7.5 版本开始新增
The 'SIGCONT'
event is emitted when a Node.js process previously moved into
the background using Ctrl+Z (i.e. SIGTSTP
) is then
brought back to the foreground using fg(1p).
If the input
stream was paused before the SIGTSTP
request, this event will
not be emitted.
The listener function is invoked without passing any arguments.
JS
The 'SIGCONT'
event is not supported on Windows.
E 'SIGINT'
自 v0.3.0 版本开始新增
The 'SIGINT'
event is emitted whenever the input
stream receives
a Ctrl+C input, known typically as SIGINT
. If there are no
'SIGINT'
event listeners registered when the input
stream receives a
SIGINT
, the 'pause'
event will be emitted.
The listener function is invoked without passing any arguments.
JS
E 'SIGTSTP'
自 v0.7.5 版本开始新增
The 'SIGTSTP'
event is emitted when the input
stream receives
a Ctrl+Z input, typically known as SIGTSTP
. If there are
no 'SIGTSTP'
event listeners registered when the input
stream receives a
SIGTSTP
, the Node.js process will be sent to the background.
When the program is resumed using fg(1p), the 'pause'
and 'SIGCONT'
events
will be emitted. These can be used to resume the input
stream.
The 'pause'
and 'SIGCONT'
events will not be emitted if the input
was
paused before the process was sent to the background.
The listener function is invoked without passing any arguments.
JS
The 'SIGTSTP'
event is not supported on Windows.
M rl.close()
自 v0.1.98 版本开始新增
The rl.close()
method closes the InterfaceConstructor
instance and
relinquishes control over the input
and output
streams. When called,
the 'close'
event will be emitted.
Calling rl.close()
does not immediately stop other events (including 'line'
)
from being emitted by the InterfaceConstructor
instance.
M rl.pause()
自 v0.3.4 版本开始新增
The rl.pause()
method pauses the input
stream, allowing it to be resumed
later if necessary.
Calling rl.pause()
does not immediately pause other events (including
'line'
) from being emitted by the InterfaceConstructor
instance.
M rl.prompt([preserveCursor])
自 v0.1.98 版本开始新增
preserveCursor
boolean
Iftrue
, prevents the cursor placement from being reset to0
.
The rl.prompt()
method writes the InterfaceConstructor
instances configured
prompt
to a new line in output
in order to provide a user with a new
location at which to provide input.
When called, rl.prompt()
will resume the input
stream if it has been
paused.
If the InterfaceConstructor
was created with output
set to null
or
undefined
the prompt is not written.
M rl.resume()
自 v0.3.4 版本开始新增
The rl.resume()
method resumes the input
stream if it has been paused.
M rl.setPrompt(prompt)
自 v0.1.98 版本开始新增
prompt
string
The rl.setPrompt()
method sets the prompt that will be written to output
whenever rl.prompt()
is called.
M rl.getPrompt()
自 v15.3.0, v14.17.0 版本开始新增
- Returns:
string
the current prompt string
The rl.getPrompt()
method returns the current prompt used by rl.prompt()
.
M rl.write(data[, key])
自 v0.1.98 版本开始新增
The rl.write()
method will write either data
or a key sequence identified
by key
to the output
. The key
argument is supported only if output
is
a TTY text terminal. See TTY keybindings for a list of key
combinations.
If key
is specified, data
is ignored.
When called, rl.write()
will resume the input
stream if it has been
paused.
If the InterfaceConstructor
was created with output
set to null
or
undefined
the data
and key
are not written.
JS
The rl.write()
method will write the data to the readline
Interface
's
input
as if it were provided by the user.
M rl[Symbol.asyncIterator]()
历史
版本 | 历史变更 |
---|---|
v11.14.0, v10.17.0 | Symbol.asyncIterator support is no longer experimental. |
v11.4.0, v10.16.0 |