diff --git a/src/basic/macro.h b/src/basic/macro.h index 41c2c3289e..954bb2de2a 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -634,4 +634,6 @@ static inline int __coverity_check_and_return__(int condition) { _copy; \ }) +#define SIZE_ADD(x, y) ((x) >= SIZE_MAX - (y) ? SIZE_MAX : (x) + (y)) + #include "log.h" diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 4581c2e3c8..f199df655d 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -35,6 +35,7 @@ #include "string-table.h" #include "strv.h" #include "terminal-util.h" +#include "utf8.h" #include "verbs.h" static int arg_family = AF_UNSPEC; @@ -1302,19 +1303,39 @@ static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, } static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) { + const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */ + int pos1, pos2; + + if (ifname) + printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, ifindex, ifname, &pos2, ansi_normal()); + else + printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal()); + + size_t cols = columns(), position = pos2 - pos1 + 2; char **i; - printf("%sLink %i (%s)%s:", - ansi_highlight(), ifindex, ifname, ansi_normal()); + STRV_FOREACH(i, p) { + size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen). + * If that happens, we'll just print one item per line. */ - STRV_FOREACH(i, p) - printf(" %s", *i); + if (position <= indent || SIZE_ADD(SIZE_ADD(position, 1), our_len) < cols) { + printf(" %s", *i); + position = SIZE_ADD(SIZE_ADD(position, 1), our_len); + } else { + printf("\n%*s%s", indent, "", *i); + position = SIZE_ADD(our_len, indent); + } + } printf("\n"); return 0; } +static int status_print_strv_global(char **p) { + return status_print_strv_ifindex(0, NULL, p); +} + struct link_info { uint64_t scopes_mask; const char *llmnr; @@ -1636,19 +1657,6 @@ static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m return 0; } -static int status_print_strv_global(char **p) { - char **i; - - printf("%sGlobal%s:", ansi_highlight(), ansi_normal()); - - STRV_FOREACH(i, p) - printf(" %s", *i); - - printf("\n"); - - return 0; -} - struct global_info { char *current_dns; char *current_dns_ex;