File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed
Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ The following special commands are supported by all REPL instances:
4343 further input or processing of that expression.
4444* ` .clear ` : Resets the REPL ` context ` to an empty object and clears any
4545 multi-line expression being input.
46+ * ` .cls ` : Clear the screen (or press <kbd >Ctrl</kbd >+<kbd >L</kbd >).
4647* ` .exit ` : Close the I/O stream, causing the REPL to exit.
4748* ` .help ` : Show this list of special commands.
4849* ` .save ` : Save the current REPL session to a file:
Original file line number Diff line number Diff line change @@ -199,6 +199,12 @@ const {
199199 kAddNewLineOnTTY,
200200 kLastCommandErrored,
201201} = require ( 'internal/readline/interface' ) ;
202+
203+ const {
204+ clearScreenDown,
205+ cursorTo,
206+ } = require ( 'internal/readline/callbacks' ) ;
207+
202208let nextREPLResourceNumber = 1 ;
203209// This prevents v8 code cache from getting confused and using a different
204210// cache from a resource of the same name
@@ -2144,6 +2150,16 @@ function defineDefaultCommands(repl) {
21442150 this . displayPrompt ( ) ;
21452151 } ,
21462152 } ) ;
2153+
2154+ repl . defineCommand ( 'cls' , {
2155+ help : 'Clear the screen' ,
2156+ action : function ( ) {
2157+ cursorTo ( this . output , 0 , 0 ) ;
2158+ clearScreenDown ( this . output ) ;
2159+ this . displayPrompt ( ) ;
2160+ } ,
2161+ } ) ;
2162+
21472163 if ( repl . terminal ) {
21482164 repl . defineCommand ( 'editor' , {
21492165 help : 'Enter editor mode' ,
Original file line number Diff line number Diff line change 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 ; 1 H \u001b \[ 0 J > / ;
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' ) ;
Original file line number Diff line number Diff line change @@ -493,6 +493,7 @@ const errorTests = [
493493 expect : [
494494 / \. b r e a k / ,
495495 / \. c l e a r / ,
496+ / \. c l s / ,
496497 / \. e x i t / ,
497498 / \. h e l p / ,
498499 / \. l o a d / ,
You can’t perform that action at this time.
0 commit comments