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 15:47:25 +02:00
parent 6eabea49da
commit e8139b15e1
3 changed files with 38 additions and 11 deletions
+20 -9
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;
}
+1
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);