Search code examples
visual-studio-codecolorsidevscode-extensionsansi-escape

View text or log files using color escape sequences as colored in vscode


I am writing some logs and graphs to .log and .txt files in NodeJS. I use chalk to color my logs and help things stand out. I am also using asciichart to generate some lo-fi but very helpful graphs.

As far as I understand both of these libraries use escape sequences to color text, i.e. \x1b[32m for green, \x1b[31m for red, etc. These escape sequences, when interpereted by a terminal console, get printed as actual color. This is nicely explained in this answer to the question "How to change node.js's console font color?"

I would like to be able to see similarly colored text within an actual text file. Obviously a text file cannot show colors, but I am wondering if a way exists to view a text file such that the escape characters are processed/parsed, and colors are shown, in the same way that happens within a terminal console.

For example, writing a colored asciichart graph to a file looks like this in vscode:

enter image description here

Obviously as a text file \x1b[34m╭\x1b[0m\x1b[34m─\x1b[0m would just show up as such.

Does anyknow know of any vscode extensions, vscode custom language settings, or any other text viewers for that matter, that would be able to view a .txt or .log file, such that the escape characters are used to color the text, rather than be shown as a big text mess above? (Could this feasibly be written as a vscode extension / custom language setting?). While the question https://unix.stackexchange.com/questions/262185/display-file-with-ansi-colors has some nice hints, it ultimately only shows how to view the file in the terminal, not in a more ui-friendly file-viewer.


Solution

  • Well I asked this a bit prematurely. It turns out ANSI Colors vscode extension does exactly this. I'll leave this here for anyone that may have the same needs in the future.