I have a simple readline shell written in Coffeescript:
rl = require 'readline'
cli = rl.createInterface process.stdin, process.stdout, null
cli.setPrompt "hello> "
cli.on 'line', (line) ->
console.log line
cli.prompt()
cli.prompt()
Running this displays a prompt:
$ coffee cli.coffee
hello>
I would like to be able to hit Ctrl-L
to clear the screen. Is this possible?
I have also noticed that I cannot hit Ctrl-L
in either the node or coffee REPLs either.
I am running on Ubuntu 11.04.
Try also:
Vorpal.js makes things like this really easy.
For an interactive CLI with a
clear
command as well as a REPL within the context of your application, do this:In the MAC terminal, to clear the console in NodeJS, you just hit
COMMAND+K
just like in Google Developer Tools Console so I'm guessing that on Windows it would beCTRL+K
.You can watch for the keypress yourself and clear the screen.
Clearing is done with ASCII control sequences like those written here: http://ascii-table.com/ansi-escape-sequences-vt-100.php
The first code
\u001B[2J
instructs the terminal to clear itself, and the second one\u001B[0;0f
forces the cursor back to position 0,0.Note
The
keypress
event is no longer part of the standard Node API in Node>= 0.10.x
but you can use the keypress module instead.On response to @loganfsmyth comment on his answer (thanks for the edit!).
I have been looking here and there and, besides of the wonderfull keypress module, there is a core module that makes possible to create a
cli
with all standard terminal behavior (all things we give for granted today such as history, options to provide an auto-complete function and input events such askeypress
are there).The module is
readline
(documentation). The good news is that all the standar behaviour is already done for us so there is no need to attach event handlers (i.e. history, clearing the screen on Ctrl+L, man if you provided the auto complete function it'll be on Tabpress).Just as an example
Really good discovery.
The node version I'm using is
v0.10.29
. I have been looking at the changelog and it was there since 2010 (commit 10d8ad).You can clear screen using
console.log()
and escape sequences.