summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c31
-rw-r--r--src/shared/util.h1
2 files changed, 25 insertions, 7 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 5f360085a2..af975956db 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3774,18 +3774,27 @@ int fd_columns(int fd) {
return ws.ws_col;
}
-unsigned columns(void) {
- static __thread int parsed_columns = 0;
+static unsigned columns_cached(bool cached) {
+ static __thread int parsed_columns = 0, env_columns = -1;
const char *e;
- if (_likely_(parsed_columns > 0))
+ if (_likely_(parsed_columns > 0 && cached))
return parsed_columns;
- e = getenv("COLUMNS");
- if (e)
- parsed_columns = atoi(e);
+ if (_unlikely_(env_columns == -1)) {
+ e = getenv("COLUMNS");
+ if (e)
+ env_columns = atoi(e);
+ else
+ env_columns = 0;
+ }
- if (parsed_columns <= 0)
+ if (env_columns > 0) {
+ parsed_columns = env_columns;
+ return parsed_columns;
+ }
+
+ if (parsed_columns <= 0 || !cached)
parsed_columns = fd_columns(STDOUT_FILENO);
if (parsed_columns <= 0)
@@ -3794,6 +3803,14 @@ unsigned columns(void) {
return parsed_columns;
}
+unsigned columns(void) {
+ return columns_cached(true);
+}
+
+unsigned columns_uncached(void) {
+ return columns_cached(false);
+}
+
int fd_lines(int fd) {
struct winsize ws;
zero(ws);
diff --git a/src/shared/util.h b/src/shared/util.h
index d25b7ee1ee..b315593bba 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -374,6 +374,7 @@ void status_welcome(void);
int fd_columns(int fd);
unsigned columns(void);
+unsigned columns_uncached(void);
int fd_lines(int fd);
unsigned lines(void);