【发布时间】:2020-07-23 09:21:13
【问题描述】:
这在 Linux 终端中是可能的,因为有像 fish 这样的 shell 对输入文本使用不同的突出显示。是否有可能在 Node.js 中有这样的东西。还是我需要用这个功能重新实现 readLine 库。
有谁知道如何在 Node.js 中做到这一点?我正在检查fish on GitHub 的代码,似乎该项目使用了NCurses。我可以在 Node.js 中做同样的事情来让 REPL 输入文本是彩色的吗?
编辑:
我已经通过@MehdiBelbal 解决方案测试了这段代码:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("lips> ", function(code) {
console.log('\ncode is ' + code);
rl.close();
});
rl._writeToOutput = function _writeToOutput(stringToWrite) {
rl.output.write(stringToWrite.replace(/define/g, '\u001b[1;34mdefine\x1b[0m'));
};
但是在你输入之后它并没有高亮define这个词,你需要输入空格(或任何字符)并用退格键删除它。
【问题讨论】:
-
那做不到。因为:您的程序无法控制终端或用户输入的内容。您可以设置终端的样式以使用不同的颜色,仅此而已。
标签: javascript node.js syntax-highlighting read-eval-print-loop ansi-escape