diff options
author | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-08-05 21:01:33 +0300 |
---|---|---|
committer | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-09-17 00:16:46 +0300 |
commit | 341312d78a8b5b4d5ef03429161242dd9d9b9206 (patch) | |
tree | 652758a4f6076d03e03e1ebe33cd50ea17a6597b | |
parent | 707dd77d8636399aefb1cad14a56369a77d4db13 (diff) |
readline: add `autoCommit` option
PR-URL: https://github.com/nodejs/node/pull/37947
Fixes: https://github.com/nodejs/node/issues/37287
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
-rw-r--r-- | doc/api/readline.md | 16 | ||||
-rw-r--r-- | lib/internal/readline/promises.js | 35 |
2 files changed, 34 insertions, 17 deletions
diff --git a/doc/api/readline.md b/doc/api/readline.md index b886c5b2bb6..cf2cf8a1537 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -599,12 +599,14 @@ setTimeout(() => ac.abort(), 10000); added: REPLACEME --> -#### `new readlinePromises.Readline(stream)` +#### `new readlinePromises.Readline(stream[, options])` <!-- YAML added: REPLACEME --> * `stream` {stream.Writable} A [TTY][] stream. +* `options` {Object} + * `autoCommit` {boolean} If `true`, no need to call `rl.commit()`. #### `rl.clearLine(dir)` <!-- YAML @@ -620,7 +622,8 @@ added: REPLACEME The `rl.clearLine()` method adds to the internal list of pending action an action that clears current line of the associated `stream` in a specified direction identified by `dir`. -You need to call `rl.commit()` to see the effect of this method. +Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` +was passed to the constructor. #### `rl.clearScreenDown()` <!-- YAML @@ -632,7 +635,8 @@ added: REPLACEME The `rl.clearScreenDown()` method adds to the internal list of pending action an action that clears the associated stream from the current position of the cursor down. -You need to call `rl.commit()` to see the effect of this method. +Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` +was passed to the constructor. #### `rl.commit()` <!-- YAML @@ -655,7 +659,8 @@ added: REPLACEME The `rl.cursorTo()` method adds to the internal list of pending action an action that moves cursor to the specified position in the associated `stream`. -You need to call `rl.commit()` to see the effect of this method. +Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` +was passed to the constructor. #### `rl.moveCursor(dx, dy)` <!-- YAML @@ -669,7 +674,8 @@ added: REPLACEME The `rl.moveCursor()` method adds to the internal list of pending action an action that moves the cursor *relative* to its current position in the associated `stream`. -You need to call `rl.commit()` to see the effect of this method. +Call `rl.commit()` to see the effect of this method, unless `autoCommit: true` +was passed to the constructor. #### `rl.rollback()` <!-- YAML diff --git a/lib/internal/readline/promises.js b/lib/internal/readline/promises.js index 3e20085b281..1a0c7b4c809 100644 --- a/lib/internal/readline/promises.js +++ b/lib/internal/readline/promises.js @@ -7,7 +7,7 @@ const { } = primordials; const { CSI } = require('internal/readline/utils'); -const { validateInteger } = require('internal/validators'); +const { validateBoolean, validateInteger } = require('internal/validators'); const { isWritable } = require('internal/streams/utils'); const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors'); @@ -19,13 +19,18 @@ const { } = CSI; class Readline { + #autoCommit = false; #stream; #todo = []; - constructor(stream) { + constructor(stream, options = undefined) { if (!isWritable(stream)) throw new ERR_INVALID_ARG_TYPE('stream', 'Writable', stream); this.#stream = stream; + if (options?.autoCommit != null) { + validateBoolean(options.autoCommit, 'options.autoCommit'); + this.#autoCommit = options.autoCommit; + } } /** @@ -38,10 +43,9 @@ class Readline { validateInteger(x, 'x'); if (y != null) validateInteger(y, 'y'); - ArrayPrototypePush( - this.#todo, - y == null ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H` - ); + const data = y == null ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`; + if (this.#autoCommit) process.nextTick(() => this.#stream.write(data)); + else ArrayPrototypePush(this.#todo, data); return this; } @@ -70,7 +74,8 @@ class Readline { } else if (dy > 0) { data += CSI`${dy}B`; } - ArrayPrototypePush(this.#todo, data); + if (this.#autoCommit) process.nextTick(() => this.#stream.write(data)); + else ArrayPrototypePush(this.#todo, data); } return this; } @@ -86,10 +91,12 @@ class Readline { clearLine(dir) { validateInteger(dir, 'dir', -1, 1); - ArrayPrototypePush( - this.#todo, - dir < 0 ? kClearToLineBeginning : dir > 0 ? kClearToLineEnd : kClearLine - ); + const data = + dir < 0 ? kClearToLineBeginning : + dir > 0 ? kClearToLineEnd : + kClearLine; + if (this.#autoCommit) process.nextTick(() => this.#stream.write(data)); + else ArrayPrototypePush(this.#todo, data); return this; } @@ -98,7 +105,11 @@ class Readline { * @returns {Readline} this */ clearScreenDown() { - ArrayPrototypePush(this.#todo, kClearScreenDown); + if (this.#autoCommit) { + process.nextTick(() => this.#stream.write(kClearScreenDown)); + } else { + ArrayPrototypePush(this.#todo, kClearScreenDown); + } return this; } |