用于彩色输出到控制台的节点模板引擎

时间:2022-07-21 00:17:42

How to render colored text to the console produced by a templating engine which supports looping and variable substitution.

如何将彩色文本渲染到由支持循环和变量替换的模板引擎生成的控制台。

I've done some tests with swig, but it simply escapes the \u001b[32m stuff.

我已经用swig做了一些测试,但它只是逃脱了\ u001b [32m的东西。

Is there something in between sprintf and a real HTML templating engine?

sprintf和真正的HTML模板引擎之间有什么东西吗?

Update:
I've tried swig for a while now and just using console.log would've actually been more maintainable/readable :)

更新:我已经试过swig一段时间了,只是使用console.log实际上会更易于维护/可读:)

Am I the only one that would find something for outputting text to the console based on a model useful? Or am I approaching this from the wrong angle?

我是唯一一个根据有用的模型找到输出文本到控制台的东西吗?或者我是从错误的角度接近这个?

1 个解决方案

#1


1  

Although this doesn't solve the problem of a templating system, it might help you towards what it is you are trying to achieve. There are two different modules that I would suggest you have a look at:

虽然这不能解决模板系统的问题,但它可能会帮助您实现您想要实现的目标。我建议你看看有两个不同的模块:

chalk - "Terminal string styling done right. Much color."

There is a node module called chalk that allows you to define colors/styles though a very simple api.

有一个名为chalk的节点模块,允许您通过非常简单的api定义颜色/样式。

It is compatible with sprintf substitution and can be combined with console.log to log pretty colored formatted output.

它与sprintf替换兼容,可以与console.log结合使用,以记录漂亮的彩色格式输出。

bunyan - "a JSON logging library for node.js services"

If however you are wanting to use it for logs, I'd suggest having a look at bunyan. It allows you to basically send anything to your logs formatted as JSON. It adds extra data like timestamps and error levels, so you don't have to. The output is just JSON, so it can easily be consumed programatically.

但是,如果您想将它用于日志,我建议您查看bunyan。它允许您基本上将任何内容发送到格式为JSON的日志。它会添加额外的数据,如时间戳和错误级别,因此您不必这样做。输出只是JSON,所以它可以很容易地以编程方式使用。

By piping the std.out into bunyan, it will colorise and format the output, making it easier to inspect.

通过将std.out连接到bunyan,它将使输出着色并格式化,使其更容易检查。

#1


1  

Although this doesn't solve the problem of a templating system, it might help you towards what it is you are trying to achieve. There are two different modules that I would suggest you have a look at:

虽然这不能解决模板系统的问题,但它可能会帮助您实现您想要实现的目标。我建议你看看有两个不同的模块:

chalk - "Terminal string styling done right. Much color."

There is a node module called chalk that allows you to define colors/styles though a very simple api.

有一个名为chalk的节点模块,允许您通过非常简单的api定义颜色/样式。

It is compatible with sprintf substitution and can be combined with console.log to log pretty colored formatted output.

它与sprintf替换兼容,可以与console.log结合使用,以记录漂亮的彩色格式输出。

bunyan - "a JSON logging library for node.js services"

If however you are wanting to use it for logs, I'd suggest having a look at bunyan. It allows you to basically send anything to your logs formatted as JSON. It adds extra data like timestamps and error levels, so you don't have to. The output is just JSON, so it can easily be consumed programatically.

但是,如果您想将它用于日志,我建议您查看bunyan。它允许您基本上将任何内容发送到格式为JSON的日志。它会添加额外的数据,如时间戳和错误级别,因此您不必这样做。输出只是JSON,所以它可以很容易地以编程方式使用。

By piping the std.out into bunyan, it will colorise and format the output, making it easier to inspect.

通过将std.out连接到bunyan,它将使输出着色并格式化,使其更容易检查。