summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2012-07-26 20:23:28 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2012-07-26 23:32:01 +0200
commit11f96fac8f30423cb14f84622de9ed56b3b8f093 (patch)
treef06b14b9e651bb1ff6f253a28905d9f6a81ea00f /src/shared
parente66bb58bed3fe5ef152268ac257b2801a7679549 (diff)
cgtop: use full terminal width
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);