Formatting messed up when piping wp commands
-
26-04-2021 - |
Question
Ok, so I have been using wp plugin update --all
in the past with a tee command. There has been no problem in the past, but after I ran an update on my system, everytime I run the command through a pipe, the formatting is messed up.
So this is the gist of the command used:
wp plugin update --all|awk '/Success/,EOF'| tee >(convert -font Courier -pointsize 14 label:@- img.png)
Previously it would produce a flawless output:
However, now when I pipe, even if I leave out the convert command, say something like this: `wp plugin update --all | tee test.txt' the output is messed up....
or
Has anyone got any ideas....driving me a bit crazy...
La solution
WP CLI needs to know some things about the terminal it's running in to format the table, aka the TTY.
But when you pipe, there is no TTY!
But you can trick it into thinking there is if you use this bash function:
faketty() {
0</dev/null script --quiet --flush --return --command "$(printf "%q " "$@")" /dev/null
}
then you can run WP CLI commands and it will think it’s running in an interactive shell, not a pipe, e.g.:
faketty wp post list | more
Autres conseils
You can use the SHELL_PIPE
ENV variable to preserve the ascii format, according to the WP-CLI docs:
To enable ASCII formatting even when the shell is piped, use the ENV variable SHELL_PIPE=0.
For a single WP-CLI command this might be sufficient, e.g.:
( SHELL_PIPE=0; wp plugin list > list.txt )
or
( SHELL_PIPE=0; wp plugin list | less )
to preserve the ascii table format when piping. This answer is helpful regarding subshell.