mre / prettyprint

Beautifully formatted output for your terminal 🌈
Apache License 2.0
200 stars 10 forks source link

Add support for different output formats #1

Open mre opened 5 years ago

mre commented 5 years ago

Would be interesting to also support HTML output, for example.

mre commented 5 years ago

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.

yarrow commented 5 years ago

I'd be interested in trying this. Some questions:

sharkdp commented 5 years ago

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).

mre commented 5 years ago

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.

yarrow commented 5 years ago

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. ^_^)

yarrow commented 5 years ago

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:

  1. Add tests for decorations and preprocessor to limit regressions;
  2. Refactor those modules;
  3. Add an HTML printer to printer.rs; and
  4. Add the HTML option to builder.rs.
mre commented 5 years ago

Sounds like a plan. 👍

yarrow commented 5 years ago

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.