Library to convert limited part of SGR (Select Graphic Rendition) sequence of ANSI escape sequences to HTML.
- 0 — Reset
- 1 — Bold, 22 — Normal intensity
- 3 — Italic, 23 — Not italic
- 4 — Underline, 24 — Underline off
- 9 — Crossed-out, 29 — Not crossed out
- 30–39, 90–97 — foreground color
- 40–49, 100–107 — background color
See more info on wiki.
- All other SGR parameters are unsupported and produce no HTML code.
- include
src/ansi_esc2html.h
- either
- add
src/ansi_esc2html.cpp
to your project - or link against
ansi_esc2html
library
- add
- Example:
ANSI_SGR2HTML ansisgr2html;
std::string ansi = "\x1b[48;5;141m background color \x1b[49m";
std::string html = ansisgr2html.simpleParse(ansi);
//or
std::string html_strict = ansisgr2html.strictParse(ansi);
result HTML will be:
<body style="background-color:#111111;font-family:'Consolas','Droid Sans Mono',monospace; color:#eeeeee; white-space:pre"><span style="background-color:#af87ff"> background color </span></body>
Can only disable SGR parameters in reverse order they where enabled. Terminal emulators can disable them in any order. See example on wiki
This function is suitable for ANSI code generated from markup languages like XML or Markdown.
Can disable SGR parameters in any order. However it is ~40% slower than simpleParse.
This will produce release versions of static and dynamic libraries and test executable:
mkdir build
cd build/
cmake -DCMAKE_BUILD_TYPE=Release ..
# or for Windows
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
make
Tested on:
- Intel® Core™ i5-3570K (4 GHz overclocked).
- Input text 101 552 bytes. More then half is SGR data
- You can see bechmark code and data here
Results:
simpleParse
~ 1082 µsstrictParse
~ 1400 µs