如何在 nodejs 中清除终端并向后滚动?

问题描述 投票:0回答:2

我正在使用 nodemon 并希望在检测到代码更改时删除回滚。

我试过

console.clear()
但这并不能清除历史记录。

我想做的是发送终端转义序列 ESC [ 3J - 清除屏幕 + 历史记录。 https://apple.stackexchange.com/questions/31872/how-do-i-reset-the-scrollback-in-the-terminal-via-a-shell-command

index.js:

...
var cmd = '3J';
const seq = `\x1B\x5B${cmd}`;
console.dir(seq,{showHidden:true});
debug("Cleared?");
...

但输出是:

[nodemon] starting `node index.js`
  index:index.js Starts +0ms
'\u001b[3J'
  index:index.js Cleared? +5ms
node.js console.log nodemon
2个回答
1
投票

不确定您是否还需要这个答案。我改编了来自用户“jonathan-annett”的代码here。我基本上不知道代码在做什么,哈哈,但经过一些操作后,我找到了一种让它工作的方法……我想。

如果有人可以解释 process.stdout.write() 方法中的代码在做什么,请告诉我。我会尝试自己弄清楚。但与此同时,这对我的目的有用。

function clearConsoleAndScrollBuffer () {
    process.stdout.write('\u001b[3J\u001b[1J');
    console.clear();
}

0
投票

我发现.clear可以解决..或者打开~/.node_repl_history可以查看历史

© www.soinside.com 2019 - 2024. All rights reserved.