1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-08 08:58:27 +03:00

varlinkctl: respect $COLUMNS when rebreaking lines and we are not connected to a TTY

Let's provide a mechanism to select the number of screen columns for
rebreaking comments in Varlink IDL connected to a TTY, by honouring the
$COLUMNS env var then too. Previously we'd only honour when connected to
a TTY, but it's also useful otherwise for rebreaking ridiculously long
comments, hence honour it in this case too.
This commit is contained in:
Lennart Poettering 2024-10-21 11:10:35 +02:00
parent 6eabea49da
commit e8139b15e1
3 changed files with 38 additions and 11 deletions

View File

@ -944,24 +944,35 @@ int fd_columns(int fd) {
return ws.ws_col;
}
int getenv_columns(void) {
int r;
const char *e = getenv("COLUMNS");
if (!e)
return -ENXIO;
unsigned c;
r = safe_atou_bounded(e, 1, USHRT_MAX, &c);
if (r < 0)
return r;
return (int) c;
}
unsigned columns(void) {
const char *e;
int c;
if (cached_columns > 0)
return cached_columns;
c = 0;
e = getenv("COLUMNS");
if (e)
(void) safe_atoi(e, &c);
if (c <= 0 || c > USHRT_MAX) {
int c = getenv_columns();
if (c < 0) {
c = fd_columns(STDOUT_FILENO);
if (c <= 0)
if (c < 0)
c = 80;
}
assert(c > 0);
cached_columns = c;
return cached_columns;
}

View File

@ -95,6 +95,7 @@ void reset_dev_console_fd(int fd, bool switch_to_text);
int lock_dev_console(void);
int make_console_stdio(void);
int getenv_columns(void);
int fd_columns(int fd);
unsigned columns(void);
int fd_lines(int fd);

View File

@ -346,6 +346,21 @@ static int verb_info(int argc, char *argv[], void *userdata) {
return 0;
}
static size_t break_columns(void) {
int r;
/* Rebreak the interface data to the TTY width */
if (on_tty())
return columns();
/* if not connected to a tty, still allow the caller to control the columns via the usual env var */
r = getenv_columns();
if (r < 0)
return SIZE_MAX;
return r;
}
typedef struct GetInterfaceDescriptionData {
const char *description;
} GetInterfaceDescriptionData;
@ -448,7 +463,7 @@ static int verb_introspect(int argc, char *argv[], void *userdata) {
}
} else {
pager_open(arg_pager_flags);
r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, on_tty() ? columns() : SIZE_MAX);
r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, break_columns());
if (r < 0)
return log_error_errno(r, "Failed to format parsed interface description: %m");
}
@ -705,7 +720,7 @@ static int verb_validate_idl(int argc, char *argv[], void *userdata) {
pager_open(arg_pager_flags);
r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, on_tty() ? columns() : SIZE_MAX);
r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, break_columns());
if (r < 0)
return log_error_errno(r, "Failed to format parsed interface description: %m");