6

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.

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

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

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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Laoujin
  • 9,962
  • 7
  • 42
  • 69
  • Can you share the code of what you've attempted so far? – JME May 08 '15 at 01:55
  • I tried using swig but the result was a total mess: newlines and whitespace don't matter in HTML but when output to the console excess whitespace is visible to the user. – Laoujin May 08 '15 at 12:46

1 Answers1

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.

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

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.

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

mrkiffie
  • 1,093
  • 8
  • 10
  • bunyan: Imagine I execute the following: `myGlobalPackage print status` which does some stuff and then prints a report to the console. It's really output for the user not just logging. – Laoujin May 08 '15 at 12:47
  • chalk: I know about chalk and colors, the idea is to separate my processing from the `console.logs`. These packages can print colored text but they do not decouple presentation from logic (which is what I want to achieve here) – Laoujin May 08 '15 at 12:48