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

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

问题描述

我有一个用 Coffeescript 编写的简单 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
  cli.prompt()

cli.prompt()

运行这个会显示一个提示:

Running this displays a prompt:

$ coffee cli.coffee 
hello> 

我希望能够按 Ctrl-L 来清除屏幕.这可能吗?

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

我还注意到我无法在 nodecoffee REPL 中按 Ctrl-L.

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

我在 Ubuntu 11.04 上运行.

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[2Ju001B[0;0f'

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

Clearing is done with ASCII control sequences like those written here: http://ascii-table.com/ansi-escape-sequences-vt-100.php

第一个代码 u001B[2J 指示终端清除自身,第二个代码 u001B[0;0f 强制光标回到位置 0,0.

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.

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

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天全站免登陆