F1LT3R / ansi-to-svg

😹 convert ANSI Escaped CLI strings to SVGs
MIT License
23 stars 5 forks source link

doesn't support spaces and multiple SGR attribute. #9

Open DrYak opened 3 years ago

DrYak commented 3 years ago

The current version struggles with this terminal output:

It should look like this: terminal.svg.gz

but there are two issues preventing this output:

multiple SGR attributes

Select Graphic Rendition (SGR) parameters do support "several attributes {...} in the same sequence, separated by semicolons". e.g.: \x[32;1m for bold+green. But ansi-to-svg fails to recognize them.

workaround for multi-SGR

split them into multiple individual ANSI SGR escape sequence:

sed -r 's@(\x1b\[)([[:digit:]]+);([[:digit:]]+)(m)@\1\3\4\1\2\4@g' terminal.txt > sgr-fixed.txt

spaces

There are a few issues with the way spaces are handled:

  1. string of spaces between text are pasted verbatim in the output SVG. The trouble is that SVG, just like HTML (And markdown) doesn display multiple space but considers them as a single separator and and displays one blank them. Thus:
          72_UK                      78_UK                      92_UK                      93_UK                      76_SA                      77_EU                     

    gets renderered as: 72_UK 78_UK 92_UK 93_UK 76_SA 77_EU

  2. even if the SVG <text> boxes start at fixed X coordinates, the left-most text still has the leading space spaces left. Somewhat luckily the above bug cancels out this bug and thus it doesn't affect the output much (until one tries the workaround. then it need fixing).
    <text x="84.01" y="14.55">          72_UK

    should be:

    <text x="84.01" y="14.55">72_UK

    (but both render the same, until workaround is used).

  3. harmless cosmetic: there are useless spaces at the end of SVG </text> blocks:
    77_EU                     </text>

    could also be written

    77_EU</text>

    (but they are both synonymous and render the same).

workaround for space

One needs to:

  1. use non-breaking spaces (e.g.: \u00A0) in the SVG output for string of multiple space.
  2. remove leading spaces (because now the non-breaking spaces cause the text to shift around, even if the <text>block is already shifted using coordinates).
  3. (optionnally) remove trailing space at the end of a block.

sed command:

sed -r 's@ +(</text)@\1@g;s@> +([[:alnum:]])@>\1@g;s@  @\xC2\xA0\xC2\xA0@g;s@\xC2\xA0 @\xC2\xA0\xC2\xA0@g' terminal.svg > fixedspaces.svg
ctrlcctrlv commented 1 year ago

yeah this thing is broken as hell :\