node.js - 如何删除控制台中打印的字符

标签 node.js

我一直在寻找如何在其他语言中做到这一点,我发现我必须使用特殊字符\b 来删除最后一个字符。 (how-do-i-erase-printed-characters-in-a-console-applicationlinux)

这对 node.js 在多次调用 console.log() 时不起作用;

如果我写一个日志:

console.log ("abc\bd");

我得到结果:abd

但如果我写:

console.log ("abc");
console.log ("\bd");

我得到了结果:

abc
d

我的目标是打印一条等待消息,例如:

等待
等待。
等待..
等待中...

又一次:

等待
等待。
等等

都在同一行。

最佳答案

process.stdout 有可用的函数:

var i = 0;  // dots counter
setInterval(function() {
  process.stdout.clearLine();  // clear current text
  process.stdout.cursorTo(0);  // move cursor to beginning of line
  i = (i + 1) % 4;
  var dots = new Array(i + 1).join(".");
  process.stdout.write("Waiting" + dots);  // write text
}, 300);

可以为 clearLine(direction, callback)

提供参数
/**
 * -1 - to the left from cursor
 *  0 - the entire line // default
 *  1 - to the right from cursor
 */

更新 2015 年 12 月 13 日:虽然上述代码有效,但它不再作为 process.stdin 的一部分记录在案。已移至 readline

关于node.js - 如何删除控制台中打印的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600890/

相关文章:

node.js - 如何将 mongo 字符串解析为 nodejs 中的对象?

javascript - 数组包含其他数组中的所有元素

node.js - 使用 NPM 安装包时,你能告诉它使用其依赖项之一的不同版本吗?

javascript - 无法读取路由参数中未定义的属性 'name'

Javascript 将字符串转换为对象数组

javascript - 将正则表达式与括号匹配

node.js - express-handlebars 部分,我们如何将子文件夹放入部分?

node.js - 在 EC2 实例上使用 Cloud9 IDE

javascript 异步帮助 async.timesLimit

node.js - 如何在本地检查 socket.io 连接?