kellyjonbrazil / jtbl

CLI tool to convert JSON and JSON Lines to terminal, CSV, HTTP, and markdown tables
MIT License
282 stars 9 forks source link

Add option --sort/--order #25

Open don-rumata opened 10 months ago

don-rumata commented 10 months ago

Hi. Can you add an option to sort columns in a table?

Now:

curl --silent http://<my-repo-in-intranet>/apt/debian/dists/stable/main/binary-amd64/Packages | /usr/local/bin/jc --pkg-index-deb -p | /usr/local/bin/jp "sort_by([], &package)[].{package: package, version: version, homepage: homepage}"  | /usr/local/bin/jtbl --markdown
| homepage                                       | package      | version    |
|------------------------------------------------|--------------|------------|
| https://schollz.com/software/croc/             | croc         | 9.6.6      |
| https://github.com/jgraph/drawio               | draw.io      | 22.1.2     |
|                                                | ifcplugin    | 3.1.1.0    |
| https://github.com/kellyjonbrazil/jc           | jc           | 1.24.0-1   |
| https://github.com/kellyjonbrazil/jtbl         | jtbl         | 1.6.0-1    |
|                                                | naps2        | 7.2.1      |
|                                                | pandoc       | 3.1.11-1   |
| https://rclone.org                             | rclone       | 1.65.0     |
|                                                | scenebuilder | 21.0.0     |
| https://github.com/ansible-semaphore/semaphore | semaphore    | 2.9.37     |
| www.hamrick.com                                | vuescan      | 9.8.22.0-0 |

How i want:

jtbl --markdown --order=package,version,homepage
| package       | version       | homepage                                       |
|---------------|---------------|------------------------------------------------|
| croc          | 9.6.6         | https://schollz.com/software/croc/             |
| draw.io       | 22.1.2        | https://github.com/jgraph/drawio               |
| ifcplugin     | 3.1.1.0       |                                                |
| jc            | 1.24.0-1      | https://github.com/kellyjonbrazil/jc           |
| jtbl          | 1.6.0-1       | https://github.com/kellyjonbrazil/jtbl         |
| naps2         | 7.2.1         |                                                |
| pandoc        | 3.1.11-1      |                                                |
| rclone        | 1.65.0        | https://rclone.org                             |
| scenebuilder  | 21.0.0        |                                                |
| semaphore     | 2.9.37        | https://github.com/ansible-semaphore/semaphore |
| vuescan       | 9.8.22.0-0    | www.hamrick.com                                |
kellyjonbrazil commented 6 months ago

I'll take a look at this - it might not be too hard to do.

kellyjonbrazil commented 6 months ago

Playing around with this and I thought I had added a fix to preserve order even when resizing back in v1.4.0: https://github.com/kellyjonbrazil/jtbl/issues/7

This seems to be working for me - If I specify the order via jq or similar then it keeps that order. Yet the example you have doesn't seem to be doing this:

% jc -a | jq '[.parsers[0:10][] | {name,version,details,description}]' | jtbl -m
| name           |   version | details                 | description                               |
|----------------|-----------|-------------------------|-------------------------------------------|
| acpi           |      1.7  |                         | `acpi` command parser                     |
| airport        |      1.5  |                         | `airport -I` command parser               |
| airport_s      |      1.6  |                         | `airport -s` command parser               |
| apt_cache_show |      1.0  | Using the rpm-qi parser | `apt-cache show` command parser           |
| apt_get_sqq    |      1.0  |                         | `apt-get -sqq` command parser             |
| arp            |      1.12 |                         | `arp` command parser                      |
| asciitable     |      1.2  |                         | ASCII and Unicode table parser            |
| asciitable_m   |      1.2  |                         | multi-line ASCII and Unicode table parser |
| blkid          |      1.6  |                         | `blkid` command parser                    |
| bluetoothctl   |      1.1  |                         | `bluetoothctl` command parser             |

% jc -a | jq '[.parsers[0:10][] | {name,version,description,details}]' | jtbl -m
| name           |   version | description                               | details                 |
|----------------|-----------|-------------------------------------------|-------------------------|
| acpi           |      1.7  | `acpi` command parser                     |                         |
| airport        |      1.5  | `airport -I` command parser               |                         |
| airport_s      |      1.6  | `airport -s` command parser               |                         |
| apt_cache_show |      1.0  | `apt-cache show` command parser           | Using the rpm-qi parser |
| apt_get_sqq    |      1.0  | `apt-get -sqq` command parser             |                         |
| arp            |      1.12 | `arp` command parser                      |                         |
| asciitable     |      1.2  | ASCII and Unicode table parser            |                         |
| asciitable_m   |      1.2  | multi-line ASCII and Unicode table parser |                         |
| blkid          |      1.6  | `blkid` command parser                    |                         |
| bluetoothctl   |      1.1  | `bluetoothctl` command parser             |                         |

I can still add an option for this, but it seems that this should work as a workaround. Is there something different about your example? (e.g. is jp not keeping the order specified?)

don-rumata commented 6 months ago

The jp is part of the jmespath.org project. It is used in Ansible. I use it because it's convenient. Adding the --order option would probably help those who do NOT use jp/jq.