From 43e1003131c0bb79ca8d80953b4a79956d7e8776 Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Sun, 19 Jun 2022 16:32:26 +0530 Subject: [PATCH] repl: add cls command repl: add cls command `.cls` will clear the current repl screen --- doc/api/repl.md | 1 + lib/repl.js | 16 ++++++++++++++++ test/parallel/test-repl-cls-command.js | 22 ++++++++++++++++++++++ test/parallel/test-repl.js | 1 + 4 files changed, 40 insertions(+) create mode 100644 test/parallel/test-repl-cls-command.js diff --git a/doc/api/repl.md b/doc/api/repl.md index c120bb25efff29..b15fd7ea166b92 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -43,6 +43,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 Ctrl+L). * `.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: diff --git a/lib/repl.js b/lib/repl.js index fbe85c7b89c0a6..610718a8b48633 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -199,6 +199,12 @@ const { kAddNewLineOnTTY, kLastCommandErrored, } = require('internal/readline/interface'); + +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 @@ -2144,6 +2150,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', diff --git a/test/parallel/test-repl-cls-command.js b/test/parallel/test-repl-cls-command.js new file mode 100644 index 00000000000000..1b88ccee02f350 --- /dev/null +++ b/test/parallel/test-repl-cls-command.js @@ -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'); diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 5e4fac12723b73..b1e2b9c3689c0f 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -493,6 +493,7 @@ const errorTests = [ expect: [ /\.break/, /\.clear/, + /\.cls/, /\.exit/, /\.help/, /\.load/,