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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-11-04 14:59:31 +01:00
parent 08e82b84ca
commit 0db41a8f1f
2 changed files with 22 additions and 9 deletions

View File

@ -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;

View File

@ -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();