prettyprint
prettyprint copied to clipboard
Add support for different output formats
Would be interesting to also support HTML output, for example.
This seems to be harder than I thought. I tried a few ansi-to-html and ansi-to-svg translators, but none really worked besides aha, which is written in C. I'd prefer to have a Rust pendant for that. It's just a single file of quite readable code, so it can be done. If anyone wants to give it a try, that would be amazing.
I'd be interested in trying this. Some questions:
- We can only use
aha
via the MPL 1.1 license or LGPL 2 or later, so it can't be part ofprettyprint
(though because it's LGPL, it could be an independent crate that's one ofprettyprint
's dependencies.) Is that acceptable? (A quick Google search for other ansi-to-html projects showed quite a few MIT licenses, so I could work from those instead.) - I'm not familiar with the idiom "Rust pendant" — Rust module?
- Would code that only supports ANSI escape sequences for color be a good first implementation? (Rather than things like "Move cursor to position
x;y
, delete to end of line, move up one line, overwrite three characters", etc.)
Wouldn't there be another way by not rendering to ANSI at all? syntect
exposes the highlighted code in a structured way that could also be rendered to HTML directly. There is even a function in syntect
that does this (check out the syntect::html
module).
Thanks for offering your help here @yarrow.
I would make the Rust port of aha
an independent crate, yes. It would make for a nice library.
I'm not familiar with the idiom "Rust pendant" — Rust module?
Hehe, turns out this doesn't make any sense in English. I was referring to the french word for "counterpart". It's quite common to use that term in German. 😊
Would code that only supports ANSI escape sequences for color be a good first implementation? (Rather than things like "Move cursor to position x;y, delete to end of line, move up one line, overwrite three characters", etc.)
Of course. It sounds like a cool project to build. Limited in scope and well testable.
syntect exposes the highlighted code in a structured way that could also be rendered to HTML directly.
Oh, that's great @sharkdp, I didn't know that. Yeah that would be an alternative approach.
I think that I would even prefer that one for inclusion in prettyprint.
@yarrow, nonetheless a port of aha
might still be a good exercise and it might make for a great standalone tool. However, if you're more interested in looking into syntext::html
, that would also be an option. That would be a good candidate for a direct pull request to prettyprint. Up to you.
I'll look into both. (Unless @sharkdp is eager to do the syntect::html
implementation, in which case I'll just look into ansi-to-html. ^_^)
I'm going to work on using syntect
for HTML. This is a bit complicated, because bat
wrote it's header, footer, and line numbers using ANSI codes directly. To use syntect
for those I'll need to convert decorations.rs
and preprocessor.rs
to return syntect
-compatible data, to be turned into ansi by the current code and HTML by syntect
. So:
- Add tests for
decorations
andpreprocessor
to limit regressions; - Refactor those modules;
- Add an HTML printer to
printer.rs
; and - Add the HTML option to
builder.rs
.
Sounds like a plan. 👍
I'm still working on this — I got distracted by things like preventing line breaks in the middle of Unicode graphemes. I still intend to do those things, but after the HTML printing piece! I'm back on the main track now.