Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 341312d

Browse filesBrowse files
committed
readline: add autoCommit option
PR-URL: #37947 Fixes: #37287 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 707dd77 commit 341312d
Copy full SHA for 341312d

File tree

Expand file treeCollapse file tree

2 files changed

+34
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-17
lines changed
Open diff view settings
Collapse file

‎doc/api/readline.md‎

Copy file name to clipboardExpand all lines: doc/api/readline.md
+11-5Lines changed: 11 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,14 @@ setTimeout(() => ac.abort(), 10000);
599599
added: REPLACEME
600600
-->
601601

602-
#### `new readlinePromises.Readline(stream)`
602+
#### `new readlinePromises.Readline(stream[, options])`
603603
<!-- YAML
604604
added: REPLACEME
605605
-->
606606

607607
* `stream` {stream.Writable} A [TTY][] stream.
608+
* `options` {Object}
609+
* `autoCommit` {boolean} If `true`, no need to call `rl.commit()`.
608610

609611
#### `rl.clearLine(dir)`
610612
<!-- YAML
@@ -620,7 +622,8 @@ added: REPLACEME
620622
The `rl.clearLine()` method adds to the internal list of pending action an
621623
action that clears current line of the associated `stream` in a specified
622624
direction identified by `dir`.
623-
You need to call `rl.commit()` to see the effect of this method.
625+
Call `rl.commit()` to see the effect of this method, unless `autoCommit: true`
626+
was passed to the constructor.
624627

625628
#### `rl.clearScreenDown()`
626629
<!-- YAML
@@ -632,7 +635,8 @@ added: REPLACEME
632635
The `rl.clearScreenDown()` method adds to the internal list of pending action an
633636
action that clears the associated stream from the current position of the
634637
cursor down.
635-
You need to call `rl.commit()` to see the effect of this method.
638+
Call `rl.commit()` to see the effect of this method, unless `autoCommit: true`
639+
was passed to the constructor.
636640

637641
#### `rl.commit()`
638642
<!-- YAML
@@ -655,7 +659,8 @@ added: REPLACEME
655659

656660
The `rl.cursorTo()` method adds to the internal list of pending action an action
657661
that moves cursor to the specified position in the associated `stream`.
658-
You need to call `rl.commit()` to see the effect of this method.
662+
Call `rl.commit()` to see the effect of this method, unless `autoCommit: true`
663+
was passed to the constructor.
659664

660665
#### `rl.moveCursor(dx, dy)`
661666
<!-- YAML
@@ -669,7 +674,8 @@ added: REPLACEME
669674
The `rl.moveCursor()` method adds to the internal list of pending action an
670675
action that moves the cursor *relative* to its current position in the
671676
associated `stream`.
672-
You need to call `rl.commit()` to see the effect of this method.
677+
Call `rl.commit()` to see the effect of this method, unless `autoCommit: true`
678+
was passed to the constructor.
673679

674680
#### `rl.rollback()`
675681
<!-- YAML
Collapse file

‎lib/internal/readline/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/readline/promises.js
+23-12Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
} = primordials;
88

99
const { CSI } = require('internal/readline/utils');
10-
const { validateInteger } = require('internal/validators');
10+
const { validateBoolean, validateInteger } = require('internal/validators');
1111
const { isWritable } = require('internal/streams/utils');
1212
const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors');
1313

@@ -19,13 +19,18 @@ const {
1919
} = CSI;
2020

2121
class Readline {
22+
#autoCommit = false;
2223
#stream;
2324
#todo = [];
2425

25-
constructor(stream) {
26+
constructor(stream, options = undefined) {
2627
if (!isWritable(stream))
2728
throw new ERR_INVALID_ARG_TYPE('stream', 'Writable', stream);
2829
this.#stream = stream;
30+
if (options?.autoCommit != null) {
31+
validateBoolean(options.autoCommit, 'options.autoCommit');
32+
this.#autoCommit = options.autoCommit;
33+
}
2934
}
3035

3136
/**
@@ -38,10 +43,9 @@ class Readline {
3843
validateInteger(x, 'x');
3944
if (y != null) validateInteger(y, 'y');
4045

41-
ArrayPrototypePush(
42-
this.#todo,
43-
y == null ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`
44-
);
46+
const data = y == null ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`;
47+
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
48+
else ArrayPrototypePush(this.#todo, data);
4549

4650
return this;
4751
}
@@ -70,7 +74,8 @@ class Readline {
7074
} else if (dy > 0) {
7175
data += CSI`${dy}B`;
7276
}
73-
ArrayPrototypePush(this.#todo, data);
77+
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
78+
else ArrayPrototypePush(this.#todo, data);
7479
}
7580
return this;
7681
}
@@ -86,10 +91,12 @@ class Readline {
8691
clearLine(dir) {
8792
validateInteger(dir, 'dir', -1, 1);
8893

89-
ArrayPrototypePush(
90-
this.#todo,
91-
dir < 0 ? kClearToLineBeginning : dir > 0 ? kClearToLineEnd : kClearLine
92-
);
94+
const data =
95+
dir < 0 ? kClearToLineBeginning :
96+
dir > 0 ? kClearToLineEnd :
97+
kClearLine;
98+
if (this.#autoCommit) process.nextTick(() => this.#stream.write(data));
99+
else ArrayPrototypePush(this.#todo, data);
93100
return this;
94101
}
95102

@@ -98,7 +105,11 @@ class Readline {
98105
* @returns {Readline} this
99106
*/
100107
clearScreenDown() {
101-
ArrayPrototypePush(this.#todo, kClearScreenDown);
108+
if (this.#autoCommit) {
109+
process.nextTick(() => this.#stream.write(kClearScreenDown));
110+
} else {
111+
ArrayPrototypePush(this.#todo, kClearScreenDown);
112+
}
102113
return this;
103114
}
104115

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.