mkopala mkopala - 6 months ago 59
CoffeeScript Question

Clear terminal window in Node.js readline shell

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


Running this displays a prompt:

$ coffee

I would like to be able to hit
to clear the screen. Is this possible?

I have also noticed that I cannot hit
in either the node or coffee REPLs either.

I am running on Ubuntu 11.04.

Answer Source

You can watch for the keypress yourself and clear the screen.

process.stdin.on 'keypress', (s, key) ->
  if key.ctrl && == 'l'
    process.stdout.write '\u001B[2J\u001B[0;0f'

Clearing is done with ASCII control sequences like those written here:

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.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download