diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-11-04 14:59:31 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-11-19 11:07:30 +0100 |
commit | 03cc374fca745a849ff75e95befcf9a3684ce886 (patch) | |
tree | 88dd8c5f075cdcac0bf26a2c4005f5edebd37c4b | |
parent | 0a5497d3fa4b736308e1073ed3e42e1a1f31ef60 (diff) | |
download | systemd-03cc374fca745a849ff75e95befcf9a3684ce886.tar.gz |
shared/format-table: disable ellipsization when piped
Fixes #13461.
Before:
$ systemd-inhibit --no-pager
WHO UID USER PID COMM WHAT WHY MODE
ModemManager 0 root 1093 ModemManager sleep ModemManage… delay
NetworkManager 0 root 1400 NetworkManager sleep NetworkMana… delay
UPower 0 root 5141 upowerd sleep Pause devic… delay
zbyszek 1000 zbyszek 10036 gsd-power handle-lid-switch External mo… block
zbyszek 1000 zbyszek 10035 gsd-media-keys handle-power-key:handle-suspend-key:… GNOME handl… block
zbyszek 1000 zbyszek 10035 gsd-media-keys sleep GNOME handl… delay
zbyszek 1000 zbyszek 10036 gsd-power sleep GNOME needs… delay
7 inhibitors listed.
$ systemd-inhibit --no-pager|grep suspend
$ systemd-inhibit --no-pager|cat
WHO UID USER PID COMM WHAT WHY MODE
ModemManager 0 root 1093 ModemManager sleep Mode… delay
NetworkManager 0 root 1400 NetworkManager sleep Netw… delay
UPower 0 root 5141 upowerd sleep Paus… delay
zbyszek 1000 zbyszek 10036 gsd-power handle-lid-switch Exte… block
zbyszek 1000 zbyszek 10035 gsd-media-keys handle-power-key:h… GNOM… block
zbyszek 1000 zbyszek 10035 gsd-media-keys sleep GNOM… delay
zbyszek 1000 zbyszek 10036 gsd-power sleep GNOM… delay
After:
$ build/systemd-inhibit --no-pager
(same as above)
$ build/systemd-inhibit --no-pager|grep suspend
zbyszek 1000 zbyszek 10035 gsd-media-keys handle-power-key:handle-suspend-key:handle-hibernate-key GNOME handling keypresses block
$ build/systemd-inhibit --no-pager|cat
WHO UID USER PID COMM WHAT WHY MODE
ModemManager 0 root 1093 ModemManager sleep ModemManager needs to reset devices delay
NetworkManager 0 root 1400 NetworkManager sleep NetworkManager needs to turn off networks delay
UPower 0 root 5141 upowerd sleep Pause device polling delay
zbyszek 1000 zbyszek 10036 gsd-power handle-lid-switch External monitor attached or configuration changed recently block
zbyszek 1000 zbyszek 10035 gsd-media-keys handle-power-key:handle-suspend-key:handle-hibernate-key GNOME handling keypresses block
zbyszek 1000 zbyszek 10035 gsd-media-keys sleep GNOME handling keypresses delay
zbyszek 1000 zbyszek 10036 gsd-power sleep GNOME needs to lock the screen delay
7 inhibitors listed.
Note that this affect all tools that use format-table.c: machinectl, busctl,
loginctl, systemd-analyze, networkctl, portablectl.
(cherry picked from commit 0db41a8f1f8eda49ce60e7efa23d17a5e24673e3)
-rw-r--r-- | src/shared/format-table.c | 9 | ||||
-rw-r--r-- | src/test/test-format-table.c | 22 |
2 files changed, 22 insertions, 9 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c index e41cbb1720..4617ae8bad 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -2,6 +2,7 @@ #include <ctype.h> #include <net/if.h> +#include <unistd.h> #include "alloc-util.h" #include "fd-util.h" @@ -1611,10 +1612,12 @@ int table_print(Table *t, FILE *f) { } /* Calculate effective table width */ - if (t->width == (size_t) -1) - table_effective_width = pager_have() ? table_requested_width : MIN(table_requested_width, columns()); - else + if (t->width != (size_t) -1) table_effective_width = t->width; + else if (pager_have() || !isatty(STDOUT_FILENO)) + table_effective_width = table_requested_width; + else + table_effective_width = MIN(table_requested_width, columns()); if (table_maximum_width != (size_t) -1 && table_effective_width > table_maximum_width) table_effective_width = table_maximum_width; diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index def9cbda2f..96b1d77701 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include <unistd.h> + #include "alloc-util.h" #include "format-table.h" #include "string-util.h" @@ -154,12 +156,20 @@ int main(int argc, char *argv[]) { assert_se(table_format(t, &formatted) >= 0); printf("%s\n", formatted); - assert_se(streq(formatted, - " no a long f… no a long f… a long fi…\n" - " no fäää no fäää fäää \n" - " yes fäää yes fäää fäää \n" - " yes xxx yes xxx xxx \n" - "5min 5min \n")); + if (isatty(STDOUT_FILENO)) + assert_se(streq(formatted, + " no a long f… no a long f… a long fi…\n" + " no fäää no fäää fäää \n" + " yes fäää yes fäää fäää \n" + " yes xxx yes xxx xxx \n" + "5min 5min \n")); + else + assert_se(streq(formatted, + " no a long field no a long field a long field\n" + " no fäää no fäää fäää \n" + " yes fäää yes fäää fäää \n" + " yes xxx yes xxx xxx \n" + "5min 5min \n")); test_issue_9549(); |