Nullus157 / cbor-diag-rs

Support for parsing/encoding CBOR diagnostic notation and annotated hex
https://cbor.nemo157.com
Apache License 2.0
12 stars 7 forks source link

Colorize output #20

Open Nemo157 opened 5 years ago

Nemo157 commented 5 years ago

The CLI should support colorizing the output data to make it easier to read, both diagnostic and annotated hex encoding.

This will probably have to be generated by the library, easiest might be to inject ANSI escape codes in the output strings, can probably be used in the website with something like https://www.npmjs.com/package/ansi-to-html (how to support Windows terminals? hopefully there's something that can parse ANSI escapes and generate the correct commands, or maybe there's a library that gives higher-level colorized strings via annotated spans that can be converted into different formats as needed).

An example of what jq does for colorizing JSON which the diagnostic colorization can probably be based on:

jq-colors

Nemo157 commented 5 years ago

Some thoughts about colorizing:

echo '
^[[1;31m55799^[[0m^[[2;37m_1^[[31m(^[[1;37m{_^[[0m
    ^[[1;34m"timestamp"^[[1;37m: ^[[31m1(^[[0m^[[35m1567774432^[[0m^[[2;37m_2^[[31m)^[[0m,
    ^[[1;34m"foo"^[[1;37m: {_^[[0m
        ^[[1;34m"keys"^[[1;37m: [^[[0m
            ^[[32m"bar"^[[0m,
            ^[[32m"baz"^[[0m,
            ^[[32m"addr"^[[0m,
        ^[[1;37m]^[[0m,
        ^[[1;34m"data"^[[1;37m: [_
            ^[[1;37m[^[[0m
                ^[[35m11^[[0m,
                ^[[35m-46^[[0m^[[2;37m_0^[[0m,
                ^[[1;31m260^[[0m^[[2;37m_1^[[0m^[[31m(^[[0m^[[33mh'"'"'efe4dce59616'"'"'^[[31m)^[[0m,
            ^[[1;37m]^[[0m,
            ^[[1;37m[^[[0m
                ^[[35m8^[[0m,
                ^[[35m-14^[[0m,
                ^[[1;31m260^[[0m^[[2;37m_1^[[0m^[[31m(^[[0m^[[33mh'"'"'1d3fe80b8681'"'"'^[[31m)^[[0m,
            ^[[1;37m]^[[0m,
        ^[[1;37m]^[[0m,
    ^[[1;37m}^[[0m,
^[[1;37m}^[[0m^[[31m)
'

color-cbor

Probably too many colors, should merge some.

Fading out the bitwidth markers is sorta good, probably "faint + parent color" would work better than "faint + white"?

chrysn commented 1 year ago

I'd consider using an external component for that, that manages different outputs (HTML, LaTeX, ANSI) and color themes.

Sadly, I don't have a concrete recommendation which one to use -- in Python I'd go with pygments, but on Rust there appears to only be syntect, but I can neither tell how heavyweight that is, nor whether it'll process pre-annotated data. But maybe it's not even a bad thing to let that be done in a completely separate step, from the highlighter's own grammar: yes we do have all the additional information, but having a grammar in a syntax highlighter rather than cbor-diag could be beneficial in situations when cbor-diag won't even process things, eg. while text is being entered by a user.

The downside of letting that parser do everything is that coloring the associated CBOR hex dumps (suggestion in #133) would be harder in such a setup.