Textualize / rich-cli

Rich-cli is a command line toolbox for fancy output in the terminal
https://www.textualize.io
MIT License
2.99k stars 77 forks source link

Allow delimiters for output from Stdin #25

Closed johnsamuelwrites closed 2 years ago

johnsamuelwrites commented 2 years ago

On running the following command

ps |rich - -m

I got the following output

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ PID TTY          TIME CMD                                                                                         │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
6697 pts/1    00:00:00 bash 6722 pts/1    00:00:36 fish 50260 pts/1    00:00:00 ps 50261 pts/1    00:00:00 rich

So rich-cli is able to inspect the first line (header) correctly, but not the subsequent lines.

Without rich-cli, I got the following output:

$ ps
    PID TTY          TIME CMD
   6697 pts/1    00:00:00 bash
   6722 pts/1    00:00:37 fish
  50453 pts/1    00:00:00 ps

Tested with commands like netstat.

One possible solution could be to allow the users to specify the delimiters.

I am wondering whether this could be a bug, since rich-cli is able to detect the first line.

willmcgugan commented 2 years ago

Why are you rendering the output of ps as markdown? What output are you expecting?

johnsamuelwrites commented 2 years ago

As a table (a rendering similar to that with --csv option). I also tried the following:

ps |rich - -m --csv

This gives the same output as above.

willmcgugan commented 2 years ago

The output of ps isn't markdown, so I'm not surprised it wouldn't render. The output can't be markdown and CSV at the same time (the combination of those two switches should really be an error).

The output isn't a CSV either. So I'm afraid there is not much that rich-cli can do about that.

audunsol commented 2 years ago

Just found this while looking for something similar, so just wanted to post a workaround to how to use rich-cli to nicely format ps output.

ps \
  | sed 's/\s*PID/PID/i' \
  | tr -s '[:blank:]' ',' \
  | rich - --csv

Outputs:

┏━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━┓
┃   PID ┃ TTY    ┃ TIME     ┃ CMD  ┃
┡━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━┩
│ 14142 │ pts/19 │ 00:00:00 │ ps   │
│ 14143 │ pts/19 │ 00:00:00 │ sed  │
│ 14144 │ pts/19 │ 00:00:00 │ tr   │
│ 14145 │ pts/19 │ 00:00:00 │ rich │
│ 22162 │ pts/19 │ 00:00:01 │ bash │
└───────┴────────┴──────────┴──────┘

For completeness, since I just looked at the same thing for kubectl output:

kubectl get pods | tr -s '[:blank:]' ',' | rich - --csv

Outputs your pods with Rich CSV table formatting as:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓
┃ NAME                                                    ┃ READY ┃ STATUS  ┃ RESTARTS ┃ AGE ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━╇━━━━━┩
│ cert-manager-cainjector-abcdefghij-54321                │ 1/1   │ Running │        0 │ 11h │
│ cert-manager-klmnopqrs-12345                            │ 1/1   │ Running │        1 │ 11h │
│ cert-manager-webhook-fedcba9876-6565                    │ 1/1   │ Running │        0 │  5h │
│ nginx-ingress-ingress-nginx-controller-123456789a-bcdef │ 2/2   │ Running │        0 │ 11h │
│ nginx-ingress-ingress-nginx-controller-123456789a-fedcb │ 2/2   │ Running │       42 │ 15h │
└─────────────────────────────────────────────────────────┴───────┴─────────┴──────────┴─────┘

It could of course be nice if rich-cli had an option to also accept white space separated output tables directly (like we get from a lot popular of command line tools, like docker, kubectl, ps, etc.), e.g.:

kubectl get pods | rich - --table

Or as I interpret the initial question/suggestion in this issue:

kubectl get pods | rich - --separator=whitespace

Anyways, thanks for a very nice CLI tool! :+1:

johnsamuelwrites commented 2 years ago

Thanks @audunsol for your solution. I like to see the output of ps in colors, thanks to rich-cli. Thanks @willmcgugan for this work.

test.

I want to test with other Linux commands.

Yes, I was looking for something similar to rich - --separator=whitespace

doctorfree commented 1 year ago

The above cool way to format ps output using rich did not work for me. That is because my process ids were not all the same length so the tr put some commas at the beginning of some lines which made the output not valid csv.

I modified the sed part to remove all leading and trailing whitespace from all lines. That seemed to work:

ps | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -s '[:blank:]' ',' | rich - --csv