[systemd-devel] [PATCH] cgtop: use full terminal width
Zbigniew Jędrzejewski-Szmek
zbyszek at in.waw.pl
Thu Jul 26 11:31:44 PDT 2012
---
src/cgtop/cgtop.c | 10 +++++++---
src/shared/util.c | 31 ++++++++++++++++++++++++-------
src/shared/util.h | 1 +
3 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index 3009589..fde31b9 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -426,7 +426,7 @@ static int display(Hashmap *a) {
Iterator i;
Group *g;
Group **array;
- unsigned rows, n = 0, j;
+ unsigned rows, path_columns, n = 0, j;
assert(a);
@@ -446,6 +446,10 @@ static int display(Hashmap *a) {
if (rows <= 0)
rows = 25;
+ path_columns = columns_uncached() - 42;
+ if (path_columns < 10)
+ path_columns = 10;
+
printf("%s%-37s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n",
arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_ON : "", "Path", arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_ON : "", "Tasks", arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_OFF : "",
@@ -463,8 +467,8 @@ static int display(Hashmap *a) {
g = array[j];
- p = ellipsize(g->path, 37, 33);
- printf("%-37s", p ? p : g->path);
+ p = ellipsize(g->path, path_columns, 33);
+ printf("%-*s", path_columns, p ? p : g->path);
free(p);
if (g->n_tasks_valid)
diff --git a/src/shared/util.c b/src/shared/util.c
index 5f36008..af97595 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 d25b7ee..b315593 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);
--
1.7.11.1.282.g511ba53
More information about the systemd-devel
mailing list