csv2md icon indicating copy to clipboard operation
csv2md copied to clipboard

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

Open lamyergeier opened this issue 1 year ago • 1 comments

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!

lamyergeier avatar Apr 29 '24 13:04 lamyergeier

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.

lzakharov avatar Apr 29 '24 17:04 lzakharov

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 |

lamyergeier avatar May 17 '24 15:05 lamyergeier

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.

lzakharov avatar May 18 '24 06:05 lzakharov

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

lamyergeier avatar May 18 '24 07:05 lamyergeier

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

lzakharov avatar May 18 '24 12:05 lzakharov

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.

lzakharov avatar May 18 '24 12:05 lzakharov

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

benjamin-awd avatar May 26 '24 07:05 benjamin-awd