ansi_up 0.0.1+4.0.4

ansi_up.js #

ansi_up is an easy to use library that transforms text containing ANSI color escape codes into HTML.

This module is a single Javascript file with no dependencies. It is "isomorphic" javascript. This is just another way of saying that the ansi_up.js file will work in both the browser or node.js. The js library is compiled from TypeScript and its type description ships with the NPM. This code has been used in production since 2011 and is actively maintained.

For example, turn this terminal output:

 30   30   30   30   30   30   30   30  
 31   31   31   31   31   31   31   31  
 32   32   32   32   32   32   32   32  

...into this browser output:

Browser Example #

    <script src="ansi_up.js" type="text/javascript"></script>
    <script type="text/javascript">

    var txt  = "\n\n\033[1;33;40m 33;40  \033[1;33;41m 33;41  \033[1;33;42m 33;42  \033[1;33;43m 33;43  \033[1;33;44m 33;44  \033[1;33;45m 33;45  \033[1;33;46m 33;46  \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

    var ansi_up = new AnsiUp;

    var html = ansi_up.ansi_to_html(txt);

    var cdiv = document.getElementById("console");

    cdiv.innerHTML = html;


Node Example #

    var AU = require('ansi_up');
    var ansi_up = new AU.default;

    var txt  = "\n\n\033[1;33;40m 33;40  \033[1;33;41m 33;41  \033[1;33;42m 33;42  \033[1;33;43m 33;43  \033[1;33;44m 33;44  \033[1;33;45m 33;45  \033[1;33;46m 33;46  \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

    var html = ansi_up.ansi_to_html(txt);

More examples are in the 'examples' directory in the repo.

Typescript Example #

    import {
        default as AnsiUp
    } from 'ansi_up';

    const ansi_up = new AnsiUp();

    const txt  = "\n\n\x1B[1;33;40m 33;40  \x1B[1;33;41m 33;41  \x1B[1;33;42m 33;42  \x1B[1;33;43m 33;43  \x1B[1;33;44m 33;44  \x1B[1;33;45m 33;45  \x1B[1;33;46m 33;46  \x1B[1m\x1B[0\n\n\x1B[1;33;42m >> Tests OK\n\n"

    let html = ansi_up.ansi_to_html(txt);

Installation #

$ npm install ansi_up

Versions #

  • Version 4.0 - Re-architect code to support [](terminal URL codes).
  • Version 3.0 - now treats ANSI bold sequences as CSS font-weight:bold
  • Version 2.0 - moved to a stateful, streaming version of the API
  • Version 1.3 - was the last of the older, deprecated API.

Quick Start #

  1. Use whatever module system to import the ansi_up module.
  2. Instantiate the object.
  3. For every piece of ansi escaped text string, call ansi_to_html.
  4. Append the emitted HTML to the previous HTML already emitted.
  5. DONE

You only need the ansi_to_html method. The other properties listed below allow you to override some of the escaping behaviour. You probably don't need to change these from their default values.

It is recommended that the HTML container that holds the span tags is styled with a monospace font. A PRE tag would work just fine for this. It is also recommended that the HTML container is styled with a black background. See the examples, for more CSS theming.

ansi_to_html (txt) #

This transforms ANSI terminal escape codes/sequences into SPAN tags that wrap and style the content.

This method only interprets ANSI SGR (Select Graphic Rendition) codes or escaped URL codes. For example, cursor movement codes are ignored and hidden from output.

This method also safely escapes any unsafe HTML characters.

The default style uses colors that are very close to the prescribed standard. The standard assumes that the text will have a black background. These colors are set as inline styles on the SPAN tags. Another option is to set the 'use_classes' property to true'. This will instead set classes on the spans so the colors can be set via CSS. The class names used are of the format ansi-*-fg/bg and ansi-bright-*-fg/bg where * is the colour name, i.e black/red/green/yellow/blue/magenta/cyan/white. See the examples directory for a complete CSS theme for these classes.

Properties #

escape_for_html #

(default: true)

This does the minimum escaping of text to make it compliant with HTML. In particular, the '&','<', and '>' characters are escaped. It is ** highly ** recommended that you do not set this to false. It will open the door security vulnerabilities.

use_classes #

(default: false)

This causes the SPAN tags to use classes to style the SPAN tags instead of specified RGB values.

url_whitelist #

(default: { 'http':1, 'https':1 };

This mapping is a whitelist of URI schemes that will be allowed to render HTML anchor tags.

Buffering #

In general, the ansi_to_html should emit HTML when invoked with a non-empty string. The only exceptions are an incomplete ESC sequence or an incomplete escaped URL. For those cases, the library will buffer the escape or the sequence for the escaped URL.

The library is also stateful. If a color is set in a prior invocation, then it will continue to emit that color in further invocations until the color/SGR attribute is changed.

Example of a Use Case #

I have used this library to 'tail' a file.

On a remote machine, I had process generating a log file. I had a web server running on the same machine. The server hosted a simple HTML page that used AJAX to poll an object with a range query. Specifically I used an HTTP/1.1 GET request with RFC 7233 Range query. The first range query would start at 0, but then progressively move forward after new data was received.

For each new chunk of data received, I would transform the data with ansi_up, and append the new spans to the innerHTML of a PRE tag.

UTF8 note #

One last important note, ansi_up takes its input in the form of a Javascript string. These strings are UTF8. When you take the output of some program and send it to Javascript, there will be buffering. Be sure that you do not send incomplete UTF8 sequences. Javascript will ignore or drop the sequence from the stream when it converts it to a string.

Building #

To build, a simple Makefile handles it all.

    $ make

Running tests #

To run the tests for ansi_up, run npm install to install dev dependencies. Then:

    $ make test

Credits #

This code was developed by Dru Nelson (

Thanks goes to the following contributors for their patches:

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  ansi_up: ^0.0.1+4.0.4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:ansi_up/ansi_up.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: web, other

Primary library: package:ansi_up/ansi_up.dart with components: js.

Health suggestions

Format lib/ansi_up.dart.

Run dartfmt to format lib/ansi_up.dart.

Maintenance issues and suggestions

Provide a file named (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and ansi_up.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
js ^0.6.1+1 0.6.1+1