Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: bind keystroke ctrl+6 to redo #41662

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,18 @@ const { createInterface } = require('readline');
<tr>
<td><kbd>Ctrl</kbd>+<kbd>-</kbd></td>
<td>Undo previous change</td>
<td>Any keystroke emits key code <code>0x1F</code> would do this action.</td>
<td>Any keystroke that emits key code <code>0x1F</code> will do this action.
In many terminals, for example <code>xterm</code>,
this is bound to <kbd>Ctrl</kbd>+<kbd>-</kbd>.</td>
<td></td>
</tr>
<tr>
<td><kbd>Ctrl</kbd>+<kbd>6</kbd></td>
<td>Redo previous change</td>
<td>Many terminals don't have a default redo keystroke.
We choose key code <code>0x1E</code> to perform redo.
In <code>xterm</code>, it is bound to <kbd>Ctrl</kbd>+<kbd>6</kbd>
by default.</td>
<td></td>
</tr>
<tr>
Expand Down
31 changes: 22 additions & 9 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,24 +889,30 @@ class Interface extends InterfaceConstructor {
[kUndo]() {
if (this[kUndoStack].length <= 0) return;

const entry = this[kUndoStack].pop();
ArrayPrototypePush(
this[kRedoStack],
{ text: this.line, cursor: this.cursor },
);

const entry = ArrayPrototypePop(this[kUndoStack]);
this.line = entry.text;
this.cursor = entry.cursor;

ArrayPrototypePush(this[kRedoStack], entry);
this[kRefreshLine]();
}

[kRedo]() {
if (this[kRedoStack].length <= 0) return;

const entry = this[kRedoStack].pop();
ArrayPrototypePush(
this[kUndoStack],
{ text: this.line, cursor: this.cursor },
);

const entry = ArrayPrototypePop(this[kRedoStack]);
this.line = entry.text;
this.cursor = entry.cursor;

ArrayPrototypePush(this[kUndoStack], entry);
this[kRefreshLine]();
}

Expand Down Expand Up @@ -1071,11 +1077,18 @@ class Interface extends InterfaceConstructor {
}
}

// Undo
if (typeof key.sequence === 'string' &&
StringPrototypeCodePointAt(key.sequence, 0) === 0x1f) {
this[kUndo]();
return;
// Undo & Redo
if (typeof key.sequence === 'string') {
switch (StringPrototypeCodePointAt(key.sequence, 0)) {
case 0x1f:
this[kUndo]();
return;
case 0x1e:
this[kRedo]();
return;
default:
break;
}
}

// Ignore escape key, fixes
Expand Down
20 changes: 16 additions & 4 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,24 +792,36 @@ function assertCursorRowsAndCols(rli, rows, cols) {
rli.close();
}

// Undo
// Undo & Redo
{
const [rli, fi] = getInterface({ terminal: true, prompt: '' });
fi.emit('data', 'the quick brown fox');
assertCursorRowsAndCols(rli, 0, 19);

// Delete right line from the 5th char
// Delete the last eight chars
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', ',', { ctrl: true, shift: false, name: 'k' });
fi.emit('keypress', ',', { ctrl: true, shift: false, name: 'u' });
assertCursorRowsAndCols(rli, 0, 0);

fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', '.', { ctrl: true, shift: false, name: 'b' });
fi.emit('keypress', ',', { ctrl: true, shift: false, name: 'k' });

assertCursorRowsAndCols(rli, 0, 11);
// Perform undo twice
fi.emit('keypress', ',', { sequence: '\x1F' });
assert.strictEqual(rli.line, 'the quick brown');
fi.emit('keypress', ',', { sequence: '\x1F' });
assert.strictEqual(rli.line, 'the quick brown fox');
// Perform redo twice
fi.emit('keypress', ',', { sequence: '\x1E' });
assert.strictEqual(rli.line, 'the quick brown');
fi.emit('keypress', ',', { sequence: '\x1E' });
assert.strictEqual(rli.line, 'the quick b');
fi.emit('data', '\n');
rli.close();
}
Expand Down