lzakharov / csv2md

Command line tool for converting CSV files into Markdown tables.
MIT License
102 stars 8 forks source link

[Bug] column alignment is not working and suggestion for better syntax #18

Closed lamyergeier closed 3 months ago

lamyergeier commented 4 months ago

Sample Data

cat file.tsv
handwritten 200 2024-04-29  36
youtube 10000   2024-04-29  21
firefox 5000    2024-04-29  39
epub    1000    2024-04-29  49
android 10000   2024-04-29  179

Convert to Markdown

 cat file.tsv | csv2md -d $'\t'  -c 0 -r 1 3 -H
| a           | b     | c          | d   |
| :---------: | ----: | ---------- | --: |
| handwritten | 200   | 2024-04-29 | 36  |
| youtube     | 10000 | 2024-04-29 | 21  |
| firefox     | 5000  | 2024-04-29 | 39  |
| epub        | 1000  | 2024-04-29 | 49  |
| android     | 10000 | 2024-04-29 | 179 |

Issues

  1. Columns are not aligned. Both, center and right alignment are not working.
  2. Standardized input to option: -C option takes values like comma-separated list of column indices or ranges, may be similar syntax can be used for -r and -c.
  3. Also if -r and -c is used with -C then the colmnn number for -r and -c should not be the affected by -C but original column number. Currently this is not the case!
lzakharov commented 4 months ago

Hello, thank you for the issue.

Columns are not aligned. Both, center and right alignment are not working.

What do you mean by that? As I can see, output contains right align colons in the header row. Can you please add expected output for this case?

Standardized input to option: -C option takes values like comma-separated list of column indices or ranges, may be similar syntax can be used for -r and -c.

You're right, it looks a bit inconsistent. But in my opinion, it's better to support multiple values for -C as it's done for -r and -c. Feel free to create PR with improvement.

Also if -r and -c is used with -C then the colmnn number for -r and -c should not be the affected by -C but original column number. Currently this is not the case!

I'm not sure if this is more expected behavior than the current ones. Can you please add some arguments for the proposed logic? Anyway, comments should be added to the flags.

lamyergeier commented 3 months ago

What do you mean by that? As I can see, output contains right align colons in the header row. Can you please add expected output for this case?

| a           |     b | c          |   d |
|:------------|------:|------------|----:|
| handwritten |   200 | 2024-04-29 |  36 |
| youtube     | 10000 | 2024-04-29 |  21 |
| firefox     |  5000 | 2024-04-29 |  39 |
| epub        |  1000 | 2024-04-29 |  49 |
| android     | 10000 | 2024-04-29 | 179 |
lzakharov commented 3 months ago

Sorry, but this it not the right output:

  1. column a should be centered due to -c 0;
  2. values in b and d should not be aligned with spaces. Column alignment managed within the header row.
lamyergeier commented 3 months ago

@lzakharov Your point 1 is correct. I made a mistake in the desired output. It should be as follows

|      a      |     b | c          |   d |
|:-----------:|------:|------------|----:|
| handwritten |   200 | 2024-04-29 |  36 |
|   youtube   | 10000 | 2024-04-29 |  21 |
|   firefox   |  5000 | 2024-04-29 |  39 |
|    epub     |  1000 | 2024-04-29 |  49 |
|   android   | 10000 | 2024-04-29 | 179 |

as you can see that column with headers a, b, d are all aligned with spaces (which you clarified in second point of yours, that this is not desirable).

lzakharov commented 3 months ago

There is no need to align columns with spaces - alignment must be done when rendering the markdown.

lzakharov commented 3 months ago

If you want to have tables in such format, you can try to configure additional markdown formatter. But I'm still not sure that anyone uses and supports such a format.

benjamin-awd commented 3 months ago

@lamyergeier you can try https://github.com/astanin/python-tabulate -- it's pretty good at handling different types of markdown formats.