gpt4 book ai didi

javascript - 以字符串形式获取 console.log 的颜色输出

转载 作者:行者123 更新时间:2023-12-04 07:34:34 27 4
gpt4 key购买 nike

在 nodejs 中,当我们运行 console.log({a: 3}) ,我们得到了一个漂亮的彩色输出,带有 3被染成棕色。
我们如何将 console.log 的原始彩色输出输出到标准输出?

let output = getConsoleLogOutput({a: 3});
// output is now '{ a: \x1B[33m3\x1B[39m }'

console.log(output);
console.log({a: 3});
// both console logs should print the same in the terminal
对于为什么我需要这样一个功能的上下文,我想设置一个本地浏览器 nodejs 终端,其中 eval 的输出发送到浏览器,显示在 xterm.js , 同时保留 console.log 的颜色.

最佳答案

您正在寻找 colors选项 util.inspect 该 Node 的 console.log内部使用:

colors <boolean> If true, the output is styled with ANSI color codes. Colors are customizable. See Customizing util.inspect colors. Default: false.


或者,也许您只是想 instantiate您自己的 Console ,它也为此提供了一个选项:

colorMode <boolean> | <string> Set color support for this Console instance. Setting to true enables coloring while inspecting values. Setting to false disables coloring while inspecting values. Setting to 'auto' makes color support depend on the value of the isTTY property and the value returned by getColorDepth() on the respective stream. This option can not be used, if inspectOptions.colors is set as well. Default: 'auto'.

关于javascript - 以字符串形式获取 console.log 的颜色输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67797159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com