readline
The readline module provides interactive line input for JSH applications.
It exposes a single ReadLine class backed by the native JSH readline implementation.
Typical usage looks like this.
const { ReadLine } = require('readline');ReadLine
Creates an interactive line reader.
Syntax
new ReadLine([options])Options
| Option | Type | Default | Description |
|---|---|---|---|
| history | String | readline | History file name stored under the JSH config directory. |
| prompt | Function | built-in prompt | Callback that returns the prompt string for each line. |
| submitOnEnterWhen | Function | always submit | Callback that decides whether Enter submits the current input. |
| autoInput | String[] | Input sequence used mainly for automated testing. |
If prompt is omitted, the first line uses > and continuation lines use . -style indentation.
If history is omitted, the module uses readline as the history file name.
The history option is treated as a file name, not a path, and the actual history file is stored under $HOME/.config/.jsh/.
Use only a base file name for history; do not include directory separators.
Usage example
| |
readLine()
Reads one logical input value.
- If the input is single-line, the result is a single string.
- If multi-line input is accepted, the result joins lines with
\n. - When the native reader ends with an error, JSH returns an
Errorobject.
Syntax
reader.readLine([options])readLine() accepts the same option shape as the constructor.
Per-call options override constructor options.
Usage example
| |
addHistory()
Adds a line to readline history.
Syntax
reader.addHistory(line)If the same line already exists, the previous entry is removed and the new one is appended at the end.
Usage example
| |
close()
Closes the current readline session.
Syntax
reader.close()If close() is called while readLine() is waiting for input, the pending call ends with EOF.
Usage example
| |
prompt option
prompt generates the prompt string for each line.
Signature
prompt(lineno) => stringlinenois zero-based.- Return the exact prompt text to render for that line.
Usage example
| |
submitOnEnterWhen option
submitOnEnterWhen controls whether pressing Enter submits the current input or continues multi-line editing.
Signature
submitOnEnterWhen(lines, idx) => booleanlinesis the current line array.idxis the current line index.- Return
trueto submit, orfalseto continue editing.
Usage example
| |
autoInput option
autoInput feeds predefined input into the reader.
This is mainly useful for tests and non-interactive scripts.
Usage example
| |
Multi-line input example
submitOnEnterWhen is commonly used to keep editing until the current line satisfies an application rule.
| |
Static key constants
ReadLine exposes many key constants for simulated input and key handling.
Representative constants include:
- Control keys:
CtrlA…CtrlZ,CtrlLeft,CtrlRight,CtrlUp,CtrlDown - Navigation keys:
Up,Down,Left,Right,Home,End,PageUp,PageDown - Editing keys:
Backspace,Delete,Enter,ShiftTab,Escape - Alt keys:
AltA…AltZ,ALTBackspace - Function keys:
F1…F24
For complete names, refer to the exported static properties on ReadLine.
Usage example
| |
Behavior notes
- The module uses callback-free synchronous reads at the JavaScript level.
readLine()returns the completed value directly. - The implementation supports interactive editing, cursor movement, history, and multi-line input through the native backend.
readLine()can return anErrorobject, so callers should checkline instanceof Errorwhen they want to handle failures explicitly.close()is primarily useful for canceling a pending read from another timer or event.