在Node.js的readline shell中清除终端窗口 [英] Clear terminal window in Node.js readline shell

查看:1053
本文介绍了在Node.js的readline shell中清除终端窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的readline shell写在Coffeescript:

  rl = require'readline' 
cli = rl.createInterface process.stdin,process.stdout,null
cli.setPrompthello>

cli.on'line',(line) - >
console.log line
cli.prompt()

cli.prompt()

运行此操作会显示提示符:

  $ coffee cli.coffee 
hello>

我想能够点击 Ctrl-L 清除屏幕。这是可能的吗?



我也注意到,我不能在节点中找到 Ctrl-L



我在Ubuntu 11.04上运行。

 过程中,您可以自行观看按键,并清除屏幕。 .stdin.on'keypress',(s,key) - > 
if key.ctrl&&& key.name =='l'
process.stdout.write'\\\ [2J\\\ [0; 0f'

清除是使用ASCII控制序列完成的,如下所示:
http://ascii-table.com/ansi-escape-sequences-vt-100.php



第一个代码 \\\ [2J 指示终端自行清除,第二个 \\\ [0; 0f 到位置0,0。





keypress event不再是Node > = 0.10.x 中标准Node API的一部分,但您可以使用 keypress 模块。


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.

解决方案

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.

这篇关于在Node.js的readline shell中清除终端窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆