NorfairKing / safe-coloured-text

https://cs-syd.eu/posts/2021-03-07-safe-coloured-text
17 stars 3 forks source link

Safe Coloured Text

Requirements / Design goals

Few dependencies

Only bytestring, text and terminfo and the relevant validity library.

Well-tested

The safe-coloured-text library already has over 10'000 tests. Most of them are golden tests, and ensure that the output stays correct even if the library changes.

Minimal output

These two strings look the same, but the second is almost twice as long:

safe-coloured-text:

\ESC[34mTests:\ESC[m

  Passed:                   \ESC[32m0\ESC[m
  Failed:                   \ESC[32m0\ESC[m
  Test suite took  \ESC[33m         0.00 seconds\ESC[m

rainbow:

\ESC[0m\ESC[38;5;4mTests:\ESC[0m
\ESC[0m\ESC[0m
\ESC[0m\ESC[0m
\ESC[0m\ESC[0m
\ESC[0m\ESC[0m
\ESC[0m  \ESC[0m\ESC[0mPassed:                   \ESC[0m\ESC[0m\ESC[38;5;2m0\ESC[0m
\ESC[0m  \ESC[0m\ESC[0mFailed:                   \ESC[0m\ESC[0m\ESC[38;5;2m0\ESC[0m
\ESC[0m  \ESC[0m\ESC[0mTest suite took  \ESC[0m\ESC[0m\ESC[38;5;3m         0.00 seconds\ESC[0m
\ESC[0m\ESC[0m

The safe-coloured-text does not emit sequences if a chunk is completely plain, but it does not deal with inter-chunk inefficiencies.

Supports 24-bit colour

This library supports outputting sequences for 24-bit colours. My urxvt terminal emulator doesn't even support that, so whatever, but at least it's possible.