cgtop: use full terminal width

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2012-07-26 20:23:28 +02:00
parent e66bb58bed
commit 11f96fac8f
3 changed files with 45 additions and 17 deletions

View File

@ -426,7 +426,7 @@ static int display(Hashmap *a) {
Iterator i; Iterator i;
Group *g; Group *g;
Group **array; Group **array;
unsigned rows, n = 0, j; unsigned rows, path_columns, n = 0, j;
assert(a); assert(a);
@ -446,13 +446,23 @@ static int display(Hashmap *a) {
if (rows <= 0) if (rows <= 0)
rows = 25; rows = 25;
printf("%s%-37s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n", path_columns = columns_uncached() - 42;
arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_ON : "", "Path", arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_OFF : "", if (path_columns < 10)
arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_ON : "", "Tasks", arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_OFF : "", path_columns = 10;
arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_ON : "", "%CPU", arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_ON : "", "Memory", arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_OFF : "", printf("%s%-*s%s %s%7s%s %s%6s%s %s%8s%s %s%8s%s %s%8s%s\n\n",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Input/s", arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : "", arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_ON : "", path_columns, "Path",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Output/s", arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : ""); arg_order == ORDER_PATH ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_ON : "", "Tasks",
arg_order == ORDER_TASKS ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_ON : "", "%CPU",
arg_order == ORDER_CPU ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_ON : "", "Memory",
arg_order == ORDER_MEMORY ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Input/s",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : "",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_ON : "", "Output/s",
arg_order == ORDER_IO ? ANSI_HIGHLIGHT_OFF : "");
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
char *p; char *p;
@ -463,8 +473,8 @@ static int display(Hashmap *a) {
g = array[j]; g = array[j];
p = ellipsize(g->path, 37, 33); p = ellipsize(g->path, path_columns, 33);
printf("%-37s", p ? p : g->path); printf("%-*s", path_columns, p ? p : g->path);
free(p); free(p);
if (g->n_tasks_valid) if (g->n_tasks_valid)

View File

@ -3774,18 +3774,27 @@ int fd_columns(int fd) {
return ws.ws_col; return ws.ws_col;
} }
unsigned columns(void) { static unsigned columns_cached(bool cached) {
static __thread int parsed_columns = 0; static __thread int parsed_columns = 0, env_columns = -1;
const char *e; const char *e;
if (_likely_(parsed_columns > 0)) if (_likely_(parsed_columns > 0 && cached))
return parsed_columns; return parsed_columns;
e = getenv("COLUMNS"); if (_unlikely_(env_columns == -1)) {
if (e) e = getenv("COLUMNS");
parsed_columns = atoi(e); 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); parsed_columns = fd_columns(STDOUT_FILENO);
if (parsed_columns <= 0) if (parsed_columns <= 0)
@ -3794,6 +3803,14 @@ unsigned columns(void) {
return parsed_columns; return parsed_columns;
} }
unsigned columns(void) {
return columns_cached(true);
}
unsigned columns_uncached(void) {
return columns_cached(false);
}
int fd_lines(int fd) { int fd_lines(int fd) {
struct winsize ws; struct winsize ws;
zero(ws); zero(ws);

View File

@ -374,6 +374,7 @@ void status_welcome(void);
int fd_columns(int fd); int fd_columns(int fd);
unsigned columns(void); unsigned columns(void);
unsigned columns_uncached(void);
int fd_lines(int fd); int fd_lines(int fd);
unsigned lines(void); unsigned lines(void);