Explain Codes LogoExplain Codes Logo

A library to convert ANSI escapes (terminal formatting/color codes) to HTML

linux
terminal-formating
ansi-escape-sequences
html-conversion
Anton ShumikhinbyAnton Shumikhin·Jan 29, 2025
TLDR

💡 Here's a hot tip: Use ansi_up JavaScript library to easily convert ANSI escape sequences into HTML. It's a simple and elegant solution for displaying ANSI-coded text in HTML format, complete with inline styles. Let's illustrate with a quick example:

const ansi_up = new AnsiUp(); // Creating new object, all good so far const textWithAnsi = "\u001b[31mThis is red\u001b[0m normal color."; // An artist's palette in text form const html = ansi_up.ansi_to_html(textWithAnsi); // The magic happens here! document.getElementById('output').innerHTML = html; // Voila, painting with text

Simply install ansi_up, and rub the ansi_to_html lamp to conjure your colorful text genie! 🧞

But wait, there's more! For command-line maestros on Linux, the aha (ANSI HTML Adapter) utility is your symphony. It transforms colorful terminal outputs into melodious HTML files:

ls --color=always | aha > ls-output.html // Rhapsody in ASCII

Linux-specific scenario: aha to the rescue

Aha Installation: Catch this hot potato

Whether you are on Ubuntu and love the efficiency of apt or are an open-source rockstar who prefers to compile from source, aha is ready for you.

sudo apt-get install aha // For Ubuntu folks or

Unleash the code warrior in you with GitHub:

git clone https://github.com/theZiz/aha.git // Fetch the repo cd aha // Dive into the directory make // Start cookin' sudo make install // Bon appétit

Striking the right note with aha

Strike the perfect 🎶 note every time you run your formatted terminal commands. Convert with default background or opt for a black background to simulate a typical terminal with the --black option:

ls --color=always | aha > ls-output.html // Default style ls --color=always | aha --black > ls-output.html // Terminal-themed opera

Precision in conversion: ANSI to HTML

Bring out the colors in your logs by handling different terminal styles and preserve those precious text colors and background colors when converting ANSI escape sequences to HTML. Thanks to aha, precision is guaranteed and all the formatting details are meticulously adhered to, creating an accurate visual representation of your HTML output.

Edge cases: Handle with care

Take note 📝! Some less common escape sequences and custom terminal themes may not convert accurately. Furthermore, performance may take a hit when converting exceedingly large outputs. As always, testing before production can save you from surprise 'gotchas'.

Aha vs ansi_up: Both efficient but different

While ansi_up is a go-to choice for JavaScript environments, aha is a lifesaver for Linux command line mavens. Compose a command line symphony with aha or weave a JavaScript spell with ansi_up. Both libraries are efficient and do a splendid job of adding color to your terminal logs.