问题:
当打印回所读的文本流时,您看不到颜色,因为您已设置
terminal
createInterface中的选项
true
. 这导致createInterface()不返回ANSI/VT100编码。
解决方案:
这个
终端
选项需要设置为
false
所以文本将用ANSI/VT100编码
从Nodejs文档:
terminal <boolean> true if the input and output streams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Default: checking isTTY on the output stream upon instantiation.
见
readline.createInterface(options) documentation
readline.createInterface({
input: myCommand.stdout,
terminal: true//<== NEEDS TO BE SET TO FALSE
}).on('line', (line) => handleOutput(myCommand.pid, line));
设置后
终端
到
假
,返回的文本将是ANSI/VT100编码的。然后需要提取与颜色相关的ANSI/VT100编码,并将其更改为所需的任何颜色格式。
请参阅下面的修改代码和输出,并添加正则表达式以提取ANSI颜色代码。您需要添加自己的逻辑来处理ANSI转义的颜色。
const spawn = require('child_process').spawn;
const readline = require('readline');
const chalk = require('chalk');
//Testing on windows 10
let myCommand = spawn(process.env.comspec, ['/c', 'echo ' + chalk.red('Hello world!'), ], {
// cwd: dirPath
});
readline.createInterface({
input: myCommand.stdout,
terminal: false
}).on('line', (line) => handleOutput(myCommand.pid, line));
readline.createInterface({
input: myCommand.stderr,
terminal: false
}).on('line', (line) => handleErrorOutput(myCommand.pid, line));
myCommand.on('exit', (code) => {
// Do more stuff ..
});
function handleErrorOutput(obj, obj2) {}
function handleOutput(obj, line) {
//PRINT TEXT WITH ANSI FORMATING
console.log(line);
//REGEX PATTERN TO EXTRACT COLOR
var options = Object.assign({
onlyFirst: false
});
const pattern = [
'[\\u001B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
].join('|');
var regex = new RegExp(pattern, options.onlyFirst ? undefined : 'g');
var ansiColor = (line.match(regex));
//PRINT EXTRACTED ANSI
console.log("ANSI COLOR CODE :");
console.log(ansiColor);
}
以上代码的输出:
编辑:
粉笔自动检测,如果你写的TTY或终端不支持颜色,并禁用颜色。可以通过设置环境变量强制执行
FORCE_COLOR=1
或者通过
--color
作为论据。
如果您将代码更改为下面代码片段中的行,那么粉笔应该正确地添加样式。
let myCommand = spawn('node', ['test.js', '--color', ], {
});
输出: