Textualize / rich-cli

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

Is it possible to format outputs of commands executed? #68

Closed StarOfSlytherin closed 2 years ago

StarOfSlytherin commented 2 years ago

For example, the mosquitto_sub command can print data received through MQTT. It has a built-in formatting option using the -F flag. But I would like to have a better looking output that would be possible with Rich CLI. What's the best way to use it?

I did the following and it worked so I assumed mosquitto would work too:

echo '{"test":"OK"}' | rich - --json
{
  "test": "OK"
}

I tried the following: mosquitto_sub -h test.mosquitto.org -t "181818/json" | rich - --json but did not get any output. Without | rich - --json I got this: {"test":"OK"}

harkabeeparolus commented 2 years ago

You can format command output, as proven by your echo example. So I do not think that this is a problem with Rich.

I don't have access to the _mosquittosub command, but I assume the problem is that it does not output a complete JSON document and close STDOUT. For example, tail -f does not work either, because it produces continuous output and never closes the file, so this example hangs forever:

$ echo '{"test":"OK"}' | tee foo.json
{"test":"OK"}
$ tail -f foo.json | rich --json -
[No output]

As long as the command that you execute finishes, and closes the pipe, you can format the output with Rich. If it doesn't, you can't. And since JSON specifically will be invalid if you don't have the entire file, it's not really technically possible to try to do anything different.

(This is in contrast with JSON Lines, JSONL, which you could certainly stream one line at a time.)

StarOfSlytherin commented 2 years ago

Thanks, I did not think that streaming JSON would be different from a normal JSON.

(This is in contrast with JSON Lines, JSONL, which you could certainly stream one line at a time.)

Does Rich CLI support this?

harkabeeparolus commented 2 years ago

As far as I can tell, rich-cli does not support JSONL. See also: #59

I don't believe it would be very difficult to add if there is enough interest. However, it's easy to convert JSONL to JSON with jq -s so that's what I would recommend. 😊

StarOfSlytherin commented 2 years ago

Looks like I can use jq -s. I'm getting some errors but I can fix that. Thanks for the help!