mdcat icon indicating copy to clipboard operation
mdcat copied to clipboard

Render tables

Open swsnr opened this issue 6 years ago • 11 comments

Need a good library to render tables to TTY?

swsnr avatar Jan 07 '18 11:01 swsnr

The prettytable is quite nice. The default rendering style is orgmode like, but I recently discoverd that with its formatting features and a few special characters you can do somethinng like this:

#[macro_use] extern crate prettytable;
use prettytable::format::{TableFormat,LinePosition,LineSeparator};

let mut format = TableFormat::new();
format.column_separator('│');
format.borders('│');
format.separator(LinePosition::Top, LineSeparator::new('─', '┬', '┌', '┐'));
format.separator(LinePosition::Title, LineSeparator::new('─', '┼', '├', '┤'));
format.separator(LinePosition::Intern, LineSeparator::new('─', '┼', '├', '┤'));
format.separator(LinePosition::Bottom, LineSeparator::new('─', '┴', '└', '┘'));
format.padding(1, 1);

// Example
let mut table = table!(["A", "B", "C"],
                       ["foo", "bar", "baz"]);
table.set_format(format);
table.printstd();

That will print the table like this:

┌─────┬─────┬─────┐
│ A   │ B   │ C   │
├─────┼─────┼─────┤
│ foo │ bar │ baz │
└─────┴─────┴─────┘

(In the terminal it looks nicer. I get gaps between lines in the browser, which shouln't be there.)

frcl avatar Feb 11 '18 18:02 frcl

Thanks for the pointer, @frcl, I'll take a look.

swsnr avatar Feb 12 '18 14:02 swsnr

I'd like to take a crack at it! Do you think the program would have to detect if the terminal properly supports Unicode, or would it be ok to use it directly?

And do you have any idea about how wrapping a table would work? Or can that situation be ignored?

ericonr avatar Apr 15 '20 18:04 ericonr

@ericonr You can assume a modern terminal with decent font, unicode and all. It's 2020 after all.

As for wrapping, no, I don't. That's something you'll need to figure out if you'd like to try it. I don't know how it could look like, it just needs to, well, kinda work for the general case, as measured by my gut feeling :wink: )

swsnr avatar Apr 15 '20 18:04 swsnr

Maybe something like html-table-to-markdown-converter. An then we can write a simple display module. I am assuming the pushdown_markdown parser generates HTML. Maybe this converter could be used when we see a Hmtl table as well.

norman-abramovitz avatar Oct 29 '20 02:10 norman-abramovitz

Are there any recent plans to add support for this in mdcat?

nicolaipre avatar Dec 08 '20 12:12 nicolaipre

Are there any recent plans to add support for this in mdcat?

No, I do not have any such plans, hence the "Help wanted" label.

This won't get done any time soon unless some opens a good pull request about it.

swsnr avatar Dec 14 '20 15:12 swsnr

I am thinking of working on this issue during my holiday/lockdown break. It will depend on my availability with my family. I will need to familiarize myself with all the latest changes first.

norman-abramovitz avatar Dec 14 '20 16:12 norman-abramovitz

Check out termimad! It's all-in-one crate for rendering markdown in terminal. Wrapping, table balancing, and scrolling are essential features of termimad.

DrSensor avatar Jun 19 '21 22:06 DrSensor

Any update on this? It seems like Clima can kinda do this, but doesn't work on everything, specifically only local files

brian6932 avatar Nov 10 '21 13:11 brian6932

No there's not. If there was you'd see it. Meanwhile, pull requests welcome. My last comment still stands.

swsnr avatar Nov 10 '21 14:11 swsnr

Just tested mdcat with https://github.com/sainnhe/gruvbox-material/blob/master/README.md and that did not go well... uses images inside of tables. So I guess, that might be a good test case.

snaggen avatar Oct 18 '23 10:10 snaggen

Closing this, because I don't intend to do implement table support any time soon.

Pull requests are welcome.

swsnr avatar Oct 18 '23 18:10 swsnr

If you don't mind a little work, I have a script in my dot files which renders markdown, with images, tables etc. Its still rough but mostly works, more of an experiment. See https://github.com/jimcornmell/dotfiles#catmd

jimcornmell avatar Dec 05 '23 09:12 jimcornmell