Skip to content

Commit 0f0388c

Browse files
Js-Brechttargos
authored andcommitted
doc: include line/cursor in readline documentation
Documents the existence and purpose of the `line` and `cursor` properties. `line` can be used for reading the current input value during runtime, if reading from a TTY stream. Both properties are necessary when developing a custom CLI input process using `readline` as a backend. Refs: #30347 Refs: DefinitelyTyped/DefinitelyTyped#40513 PR-URL: #30667 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent bb77e6e commit 0f0388c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

doc/api/readline.md

+49
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,55 @@ async function processLineByLine() {
349349
}
350350
```
351351

352+
### rl.line
353+
<!-- YAML
354+
added: 0.1.98
355+
-->
356+
357+
* {string|undefined}
358+
359+
The current input data being processed by node.
360+
361+
This can be used when collecting input from a TTY stream to retrieve the
362+
current value that has been processed thus far, prior to the `line` event
363+
being emitted. Once the `line` event has been emitted, this property will
364+
be an empty string.
365+
366+
Be aware that modifying the value during the instance runtime may have
367+
unintended consequences if `rl.cursor` is not also controlled.
368+
369+
**If not using a TTY stream for input, use the [`'line'`][] event.**
370+
371+
One possible use case would be as follows:
372+
373+
```js
374+
const values = ['lorem ipsum', 'dolor sit amet'];
375+
const rl = readline.createInterface(process.stdin);
376+
const showResults = debounce(() => {
377+
console.log(
378+
'\n',
379+
values.filter((val) => val.startsWith(rl.line)).join(' ')
380+
);
381+
}, 300);
382+
process.stdin.on('keypress', (c, k) => {
383+
showResults();
384+
});
385+
```
386+
387+
### rl.cursor
388+
<!-- YAML
389+
added: 0.1.98
390+
-->
391+
392+
* {number|undefined}
393+
394+
The cursor position relative to `rl.line`.
395+
396+
This will track where the current cursor lands in the input string, when
397+
reading input from a TTY stream. The position of cursor determines the
398+
portion of the input string that will be modified as input is processed,
399+
as well as the column where the terminal caret will be rendered.
400+
352401
## `readline.clearLine(stream, dir[, callback])`
353402
<!-- YAML
354403
added: v0.7.7

0 commit comments

Comments
 (0)