Skip to content

Commit 1d743e5

Browse files
committed
repl: add cls command
repl: add cls command `.cls` will clear the current repl screen
1 parent b9289a6 commit 1d743e5

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

doc/api/repl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The following special commands are supported by all REPL instances:
3939
further input or processing of that expression.
4040
* `.clear`: Resets the REPL `context` to an empty object and clears any
4141
multi-line expression being input.
42+
* `.cls`: Clear the screen (or press <kbd>Ctrl</kbd>+<kbd>L</kbd>).
4243
* `.exit`: Close the I/O stream, causing the REPL to exit.
4344
* `.help`: Show this list of special commands.
4445
* `.save`: Save the current REPL session to a file:

lib/repl.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ const {
191191
const {
192192
makeContextifyScript,
193193
} = require('internal/vm');
194+
const {
195+
clearScreenDown,
196+
cursorTo,
197+
} = require('internal/readline/callbacks');
198+
194199
let nextREPLResourceNumber = 1;
195200
// This prevents v8 code cache from getting confused and using a different
196201
// cache from a resource of the same name
@@ -1836,6 +1841,16 @@ function defineDefaultCommands(repl) {
18361841
this.displayPrompt();
18371842
},
18381843
});
1844+
1845+
repl.defineCommand('cls', {
1846+
help: 'Clear the screen',
1847+
action: function() {
1848+
cursorTo(this.output, 0, 0);
1849+
clearScreenDown(this.output);
1850+
this.displayPrompt();
1851+
},
1852+
});
1853+
18391854
if (repl.terminal) {
18401855
repl.defineCommand('editor', {
18411856
help: 'Enter editor mode',
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const repl = require('repl');
6+
const ArrayStream = require('../common/arraystream');
7+
8+
// eslint-disable-next-line no-control-regex
9+
const clearChar = /\[1;1H\u001b\[0J>/;
10+
let accum = '';
11+
const output = new ArrayStream();
12+
output.write = (data) => (accum += data.replace('\r', ''));
13+
14+
const r = repl.start({
15+
input: new ArrayStream(),
16+
output,
17+
});
18+
['new Error', 'Promise'].forEach((cmd) => r.write(`${cmd}\n`));
19+
assert.strictEqual(accum.match(clearChar), null);
20+
r.write('.cls\n');
21+
assert.strictEqual(accum.match(clearChar).length > 0, true);
22+
r.write('.exit\n');

test/parallel/test-repl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ const errorTests = [
458458
expect: [
459459
/\.break/,
460460
/\.clear/,
461+
/\.cls/,
461462
/\.exit/,
462463
/\.help/,
463464
/\.load/,

0 commit comments

Comments
 (0)