ANSI code translator

conversionescape-characterstext processing

Is there any tool that translates text with ANSI code to text-like output? tput cap names or the like?

Reason for the inquiry is that I would like to use script or the like and view the resulting file without having to decipher ANSI sequences. I know some, but not all, and a textual output would be easier to read.

In most cases I can look at source code for script or program that produces the ANSI text, but would be nice if there was a "debug" like tool.

The best would likely be C ? variable names, i.e.:

^[[K    ->   <clr_eol>

Know I can write a script for it … for example:

sed 's/$/<newline>/' rec001.txt | \
sed 's/\x07/\n<bell>/g' | \
sed 's/\x1b\[K/\n<clr_eol>/g' | \
sed 's/\x1b\[?12l\x1b\[?25h/\n<cursor_normal>/g' | \
sed 's/\x1b\[\([0-9]\+\);\([0-9]\+\)H/\n<cursor_position(\1, \2)>/g' | \
sed 's/\x1b\[?1049h/\n<enter_ca_mode>/g' | \
sed 's/\x1b\[?25l/\n<cursor_invisible>/g' | \
sed 's/\x0d/<CR>/g' | \
...

which is part of a quick test I did on a Irssi recording.

But wondering if there are any existing tool for the job.

As a side note we have aha, Ansi HTML adapter, which converts ANSI to HTML. But that is not the adapter I am looking for.


Best Answer

Someone might suggest a program. It is not a trivial problem to do well:

  • "ANSI sequences" are standardized in ECMA-48,
  • some of your examples (such as the cursor appearance and enter_ca_mode) are not in the standard,
  • some such as the enter_ca_mode 1049 code have variations (\E7\E[?47h). If a program relies on a terminal description, only one of the variations will be recognized,
  • some of the sequences are parameterized, which means that a program has to either have the rules built-in, or able to transform terminal capabilities such as \E[%i%p1%d;%p2%dH into an regular expression that can be matched against the inputs.

Given all of that, if someone suggests a program, it more likely will not solve those aspects, but simply be a terminal emulator without the display.

Related Question