NorfairKing / safe-coloured-text

Home Page:https://cs-syd.eu/posts/2021-03-07-safe-coloured-text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Safe Coloured Text

Requirements / Design goals

  • Use String/Text/ByteString correctly: Some things are strings of characters, some things are text, some things are bytes. Those are not the same.
  • No partial functions
  • Represent chunks of coloured text purely
  • Be able to print chunks of coloured text quickly
  • Be able to turn chunks of coloured text into non-coloured text for when terminals don't support (certain) colours.
  • (Ideally) output minimal amounts of overhead
  • As few dependencies as possible.
  • Well-tested
  • British spelling (because I can)

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.

About

https://cs-syd.eu/posts/2021-03-07-safe-coloured-text


Languages

Language:Haskell 85.0%Language:Nix 15.0%