diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2012-07-26 20:23:28 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2012-07-26 23:32:01 +0200 |
commit | 11f96fac8f30423cb14f84622de9ed56b3b8f093 (patch) | |
tree | f06b14b9e651bb1ff6f253a28905d9f6a81ea00f /src/shared/util.c | |
parent | e66bb58bed3fe5ef152268ac257b2801a7679549 (diff) |
cgtop: use full terminal width
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 31 |
1 files changed, 24 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); |