mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
readline: add getPrompt to get the current prompt
Since there is a setPrompt() there should be a getPrompt(). There are use-cases where it is needed to know what the current prompt is. Adding a getPrompt() negates the need to store the set prompt externally or read the internal _prompt which would be bad practice. Co-authored-by: Colin Ihrig <cjihrig@gmail.com> PR-URL: https://github.com/nodejs/node/pull/33675 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
parent
3c38445cc8
commit
60a97c0b42
@ -283,6 +283,15 @@ added: v0.1.98
|
||||
The `rl.setPrompt()` method sets the prompt that will be written to `output`
|
||||
whenever `rl.prompt()` is called.
|
||||
|
||||
### `rl.getPrompt()`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Returns: {string} the current prompt string
|
||||
|
||||
The `rl.getPrompt()` method returns the current prompt used by `rl.prompt()`.
|
||||
|
||||
### `rl.write(data[, key])`
|
||||
<!-- YAML
|
||||
added: v0.1.98
|
||||
|
||||
@ -144,7 +144,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
|
||||
let escaped = null;
|
||||
|
||||
function getPreviewPos() {
|
||||
const displayPos = repl._getDisplayPos(`${repl._prompt}${repl.line}`);
|
||||
const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
|
||||
const cursorPos = repl.line.length !== repl.cursor ?
|
||||
repl.getCursorPos() :
|
||||
displayPos;
|
||||
@ -177,7 +177,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
|
||||
rows = pos.displayPos.rows - pos.cursorPos.rows;
|
||||
moveCursor(repl.output, 0, rows);
|
||||
}
|
||||
const totalLine = `${repl._prompt}${repl.line}${completionPreview}`;
|
||||
const totalLine = `${repl.getPrompt()}${repl.line}${completionPreview}`;
|
||||
const newPos = repl._getDisplayPos(totalLine);
|
||||
// Minimize work for the terminal. It is enough to clear the right part of
|
||||
// the current line in case the preview is visible on a single line.
|
||||
@ -263,7 +263,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
|
||||
}
|
||||
repl.output.write(result);
|
||||
cursorTo(repl.output, cursorPos.cols);
|
||||
const totalLine = `${repl._prompt}${repl.line}${suffix}`;
|
||||
const totalLine = `${repl.getPrompt()}${repl.line}${suffix}`;
|
||||
const newPos = repl._getDisplayPos(totalLine);
|
||||
const rows = newPos.rows - cursorPos.rows - (newPos.cols === 0 ? 1 : 0);
|
||||
moveCursor(repl.output, 0, -rows);
|
||||
@ -611,7 +611,7 @@ function setupReverseSearch(repl) {
|
||||
let rows = 0;
|
||||
if (lastMatch !== -1) {
|
||||
const line = repl.history[lastMatch].slice(0, lastCursor);
|
||||
rows = repl._getDisplayPos(`${repl._prompt}${line}`).rows;
|
||||
rows = repl._getDisplayPos(`${repl.getPrompt()}${line}`).rows;
|
||||
cursorTo(repl.output, promptPos.cols);
|
||||
} else if (isInReverseSearch && repl.line !== '') {
|
||||
rows = repl.getCursorPos().rows;
|
||||
@ -631,7 +631,7 @@ function setupReverseSearch(repl) {
|
||||
|
||||
// To know exactly how many rows we have to move the cursor back we need the
|
||||
// cursor rows, the output rows and the input rows.
|
||||
const prompt = repl._prompt;
|
||||
const prompt = repl.getPrompt();
|
||||
const cursorLine = `${prompt}${outputLine.slice(0, cursor)}`;
|
||||
const cursorPos = repl._getDisplayPos(cursorLine);
|
||||
const outputPos = repl._getDisplayPos(`${prompt}${outputLine}`);
|
||||
@ -682,7 +682,7 @@ function setupReverseSearch(repl) {
|
||||
if (!isInReverseSearch) {
|
||||
if (key.ctrl && checkAndSetDirectionKey(key.name)) {
|
||||
historyIndex = repl.historyIndex;
|
||||
promptPos = repl._getDisplayPos(`${repl._prompt}`);
|
||||
promptPos = repl._getDisplayPos(`${repl.getPrompt()}`);
|
||||
print(repl.line, `${labels[dir]}_`);
|
||||
isInReverseSearch = true;
|
||||
}
|
||||
|
||||
@ -291,6 +291,11 @@ Interface.prototype.setPrompt = function(prompt) {
|
||||
};
|
||||
|
||||
|
||||
Interface.prototype.getPrompt = function() {
|
||||
return this._prompt;
|
||||
};
|
||||
|
||||
|
||||
Interface.prototype._setRawMode = function(mode) {
|
||||
const wasInRawMode = this.input.isRaw;
|
||||
|
||||
|
||||
@ -898,6 +898,16 @@ for (let i = 0; i < 12; i++) {
|
||||
});
|
||||
}
|
||||
|
||||
// Calling the getPrompt method
|
||||
{
|
||||
const expectedPrompts = ['$ ', '> '];
|
||||
const [rli] = getInterface({ terminal });
|
||||
for (const prompt of expectedPrompts) {
|
||||
rli.setPrompt(prompt);
|
||||
assert.strictEqual(rli.getPrompt(), prompt);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const expected = terminal ?
|
||||
['\u001b[1G', '\u001b[0J', '$ ', '\u001b[3G'] :
|
||||
@ -920,7 +930,7 @@ for (let i = 0; i < 12; i++) {
|
||||
|
||||
rl.prompt();
|
||||
|
||||
assert.strictEqual(rl._prompt, '$ ');
|
||||
assert.strictEqual(rl.getPrompt(), '$ ');
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
@ -104,7 +104,7 @@ assert.throws(r3, {
|
||||
// 4, Verify that defaults are used when no arguments are provided
|
||||
const r4 = repl.start();
|
||||
|
||||
assert.strictEqual(r4._prompt, '> ');
|
||||
assert.strictEqual(r4.getPrompt(), '> ');
|
||||
assert.strictEqual(r4.input, process.stdin);
|
||||
assert.strictEqual(r4.output, process.stdout);
|
||||
assert.strictEqual(r4.terminal, !!r4.output.isTTY);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user