1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

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;
Group *g;
Group **array;
unsigned rows, n = 0, j;
unsigned rows, path_columns, n = 0, j;
assert(a);
@ -446,13 +446,23 @@ static int display(Hashmap *a) {
if (rows <= 0)
rows = 25;
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 : "",
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 : "");
path_columns = columns_uncached() - 42;
if (path_columns < 10)
path_columns = 10;
printf("%s%-*s%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_columns, "Path",
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++) {
char *p;
@ -463,8 +473,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)

View File

@ -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);

View File

@ -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);