Skip to content

Add cls command to repl #52206

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following special commands are supported by all REPL instances:
further input or processing of that expression.
* `.clear`: Resets the REPL `context` to an empty object and clears any
multi-line expression being input.
* `.cls`: Clear the screen (or press <kbd>Ctrl</kbd>+<kbd>L</kbd>).
* `.exit`: Close the I/O stream, causing the REPL to exit.
* `.help`: Show this list of special commands.
* `.save`: Save the current REPL session to a file:
Expand Down
15 changes: 15 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ const {
const {
makeContextifyScript,
} = require('internal/vm');
const {
clearScreenDown,
cursorTo,
} = require('internal/readline/callbacks');

let nextREPLResourceNumber = 1;
// This prevents v8 code cache from getting confused and using a different
// cache from a resource of the same name
Expand Down Expand Up @@ -1836,6 +1841,16 @@ function defineDefaultCommands(repl) {
this.displayPrompt();
},
});

repl.defineCommand('cls', {
help: 'Clear the screen',
action: function() {
cursorTo(this.output, 0, 0);
clearScreenDown(this.output);
this.displayPrompt();
},
});

if (repl.terminal) {
repl.defineCommand('editor', {
help: 'Enter editor mode',
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-repl-cls-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

require('../common');
const assert = require('assert');
const repl = require('repl');
const ArrayStream = require('../common/arraystream');

// eslint-disable-next-line no-control-regex
const clearChar = /\[1;1H\u001b\[0J>/;
let accum = '';
const output = new ArrayStream();
output.write = (data) => (accum += data.replace('\r', ''));

const r = repl.start({
input: new ArrayStream(),
output,
});
['new Error', 'Promise'].forEach((cmd) => r.write(`${cmd}\n`));
assert.strictEqual(accum.match(clearChar), null);
r.write('.cls\n');
assert.strictEqual(accum.match(clearChar).length > 0, true);
r.write('.exit\n');
1 change: 1 addition & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ const errorTests = [
expect: [
/\.break/,
/\.clear/,
/\.cls/,
/\.exit/,
/\.help/,
/\.load/,
Expand Down
Loading