wp-cli / ideas

💡 Ideas and feature requests are collected here
40 stars 1 forks source link

Retaining tabular formatting when piping output to another command #184

Closed baizmandesign closed 9 months ago

baizmandesign commented 9 months ago

Hi there,

This may be a question rather than a feature request. (Or, if I can build this, then consider it a proposal for a new feature.)

When running commands that generate long lists—for example, wp user list on a site with more than a couple dozen users—I like to pipe the output to a pager such as less. But when I do that, all of the nice tabular formatting gets stripped and the resultant text is much harder to read. I understand why this is the default behavior: it becomes much easier to feed the output to a command like awk. Makes sense to me.

Here's my question: can I pipe the output to a pager and still retain the nice tabular formatting?

An example command that is not piped:

$ wp user list
+----+-------------+-------------+-------------+-------------+-----------------+
| ID | user_login  | display_nam | user_email  | user_regist | roles           |
|    |             | e           |             | ered        |                 |
+----+-------------+-------------+-------------+-------------+-----------------+
| 1  | saulbaizman | saulbaizman | saul@xxxxxx | 2015-05-05  | administrator   |
|    |             |             | x.com       | 16:39:11    |                 |
+----+-------------+-------------+-------------+-------------+-----------------+

The same example command when piped to less:

$ wp user list | less
ID      user_login      display_name    user_email      user_registered roles
1       saulbaizman     saulbaizman     saul@xxxxxxx.com        2015-05-05 16:39:11     administrator
(END)

("(END)" is output from less.) As you can see, it becomes very difficult to match columns and values.

I'm imagining a new flag—perhaps a global flag that can be used with any command?—to retain the original tabular formatting:

$ wp --retain-formatting user list | less
+----+-------------+-------------+-------------+-------------+-----------------+
| ID | user_login  | display_nam | user_email  | user_regist | roles           |
|    |             | e           |             | ered        |                 |
+----+-------------+-------------+-------------+-------------+-----------------+
| 1  | saulbaizman | saulbaizman | saul@xxxxxx | 2015-05-05  | administrator   |
|    |             |             | x.com       | 16:39:11    |                 |
+----+-------------+-------------+-------------+-------------+-----------------+
(END)

What do you think?

danielbachhuber commented 9 months ago

Hey @baizmandesign!

Here's my question: can I pipe the output to a pager and still retain the nice tabular formatting?

Here's where the switch happens:

https://github.com/wp-cli/php-cli-tools/blob/a6bb94664ca36d0962f9c2ff25591c315a550c51/lib/cli/Table.php#L69-L73

You can manipulate the behavior with the SHELL_PIPED environment variable:

$ SHELL_PIPE=0 wp user list | less
+----+------------+--------------+---------------------+---------------------+----------------------+
| ID | user_login | display_name | user_email          | user_registered     | roles                |
+----+------------+--------------+---------------------+---------------------+----------------------+
| 1  | daniel     | daniel       | daniel@handbuilt.co | 2023-07-24 15:03:34 | administrator,author |
+----+------------+--------------+---------------------+---------------------+----------------------+

Here's a bit more detail: https://github.com/wp-cli/wp-cli/blob/e5164148f83acfea852c60e3c5a7fdcfe5b46803/php/utils.php#L1222-L1239

baizmandesign commented 9 months ago

This is great, thanks @danielbachhuber! For anyone else who finds this in the future, the environment variable is documented here:

https://make.wordpress.org/cli/handbook/references/internal-api/wp-cli-utils-ispiped/