javascript - 标准输出中的NodeJS child_process ansi
问题描述
我想得到类似的东西
[06:32:35] [Server thread/INFO]: [0;36;1m | [0;36;22m|__) [0;32;22mLuckPerms [0;36;1mv4.3.73[m
[06:32:35] [Server thread/INFO]: [0;36;1m |___ [0;36;22m| [0;30;1mRunning on Bukkit - CraftBukkit[m
但我明白了
[06:05:02] [Server thread/INFO]: | |__) LuckPerms v4.3.73
[06:05:02] [Server thread/INFO]: |___ | Running on Bukkit - CraftBukkit
使用 child_process 运行 minecraft 服务器时
prcs.stdout.on("data", function(d) {
console.log(d.toString());
});
解决方案
在不确切知道如何d
形成的情况下,这里有一些符合您的示例的东西,可能不完全符合您的需要,但您可以随时尝试升级它(至少它不需要任何依赖项):
const versionRegExp = /v[0-9]+(\.[0-9]+)*$/;
d.toString().split("\n").forEach((line) => {
// no idea what the spaces are made of
const exploded = line.trim().split(/[ \t]+/);
// add paddings to the first two structures
const first = exploded.shift().padEnd(5, ' ');
const second = exploded.shift().padEnd(7, ' ');
// work out the content
// condition based on `second`, or should it be remainder.match(versionRegExp) ?
const remainder = 0 === second.indexOf('|__)')
? `[0;30;1m${exploded.join(' ').replace(versionRegExp, '[0;36;1m$&')}[m`
: `[0;32;22m${exploded.join(' ')}[m`
;
// format line and display
console.log(`[0;36;1m${first}[0;36;22m${second}${remainder}`);
});
推荐阅读
- json - swift json帮助在表格视图控制器上显示数据
- r - 使用 raster 和 sp 库在 R 中将坐标的 data.frame 从 Lambert93 重新投影到 WGS83
- google-api - GKE 集群 API 中区域和位置端点之间的区别
- python - Django send_mail 引发错误“‘ascii’编解码器无法编码字符”
- javascript - 出现错误:`ReferenceError: Cannot access 'imported const' before initialization` in react
- c++ - 如何获取向量c ++的列表
- html - Html 基础 - 在同一行与不同行编写的控件
- lua - Lua 回调的返回值
- typescript - TypeScript 类中的 # 符号是什么意思?
- c++ - 比较 std::ostream 是否为 std::cout ("no match for 'operator=='")