Clear terminal window in Node.js readline shell

后端 未结 7 869
长情又很酷
长情又很酷 2021-01-31 05:41

I have a simple readline shell written in Coffeescript:

rl = require \'readline\'
cli = rl.createInterface process.std         


        
相关标签:
7条回答
  • 2021-01-31 06:29

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

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

    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.

    0 讨论(0)
提交回复
热议问题