Make column width calculation dynamic.

Closes #228.
This commit is contained in:
Hisham Muhammad 2015-08-20 00:32:47 -03:00
parent 8bd603cb68
commit 9428010121
10 changed files with 69 additions and 105 deletions

View File

@ -26,6 +26,7 @@ in the source distribution for its full text.
#include <pwd.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#ifdef __ANDROID__
#define SYS_ioprio_get __NR_ioprio_get
@ -72,6 +73,11 @@ typedef enum ProcessFields {
TGID = 52,
} ProcessField;
typedef struct ProcessPidColumn_ {
int id;
char* label;
} ProcessPidColumn;
typedef struct Process_ {
Object super;
@ -145,14 +151,13 @@ typedef struct ProcessFieldData_ {
} ProcessFieldData;
// Implemented in platform-specific code:
void Process_setupColumnWidths();
void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
void Process_delete(Object* cast);
bool Process_isThread(Process* this);
extern ProcessFieldData Process_fields[];
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
extern ProcessPidColumn Process_pidColumns[];
extern char Process_pidFormat[20];
typedef Process*(*Process_New)(struct Settings_*);
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);
@ -176,6 +181,23 @@ static int Process_getuid = -1;
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
char Process_pidFormat[20] = "%7u ";
static char Process_titleBuffer[20][20];
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
int digits = ceil(log10(maxPid));
assert(digits < 20);
for (int i = 0; Process_pidColumns[i].label; i++) {
assert(i < 20);
sprintf(Process_titleBuffer[i], "%*s ", digits, Process_pidColumns[i].label);
Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i];
}
sprintf(Process_pidFormat, "%%%du ", digits);
}
void Process_humanNumber(RichString* str, unsigned long number, bool coloring) {
char buffer[11];
int len;
@ -426,7 +448,7 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) {
case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break;
case TIME: Process_printTime(str, this->time); return;
case TGID: snprintf(buffer, n, Process_pidFormat, this->tgid); break;
case TPGID: snprintf(buffer, n, Process_tpgidFormat, this->tpgid); break;
case TPGID: snprintf(buffer, n, Process_pidFormat, this->tpgid); break;
case TTY_NR: snprintf(buffer, n, "%5u ", this->tty_nr); break;
case USER: {
if (Process_getuid != (int) this->st_uid)

View File

@ -53,6 +53,11 @@ typedef enum ProcessFields {
TGID = 52,
} ProcessField;
typedef struct ProcessPidColumn_ {
int id;
char* label;
} ProcessPidColumn;
typedef struct Process_ {
Object super;
@ -126,14 +131,13 @@ typedef struct ProcessFieldData_ {
} ProcessFieldData;
// Implemented in platform-specific code:
void Process_setupColumnWidths();
void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
void Process_delete(Object* cast);
bool Process_isThread(Process* this);
extern ProcessFieldData Process_fields[];
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
extern ProcessPidColumn Process_pidColumns[];
extern char Process_pidFormat[20];
typedef Process*(*Process_New)(struct Settings_*);
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);
@ -154,6 +158,10 @@ typedef struct ProcessClass_ {
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
extern char Process_pidFormat[20];
void Process_setupColumnWidths();
void Process_humanNumber(RichString* str, unsigned long number, bool coloring);
void Process_colorNumber(RichString* str, unsigned long long number, bool coloring);

View File

@ -82,7 +82,6 @@ void Platform_setBindings(Htop_Action* keys) {
int Platform_numberOfFields = 100;
char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";
int Platform_getUptime() {
struct timeval bootTime, currTime;
@ -117,29 +116,15 @@ int Platform_getMaxPid() {
return 99999;
}
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
if (maxPid > 99999) {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = " TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = "TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
}
}
ProcessPidColumn Process_pidColumns[] = {
{ .id = PID, .label = "PID" },
{ .id = PPID, .label = "PPID" },
{ .id = TPGID, .label = "TPGID" },
{ .id = TGID, .label = "TGID" },
{ .id = PGRP, .label = "PGRP" },
{ .id = SESSION, .label = "SESN" },
{ .id = 0, .label = NULL },
};
double Platform_setCPUValues(Meter* mtr, int cpu) {
/* All just from CPUMeter.c */

View File

@ -24,7 +24,6 @@ void Platform_setBindings(Htop_Action* keys);
extern int Platform_numberOfFields;
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
int Platform_getUptime();
@ -32,8 +31,6 @@ void Platform_getLoadAverage(double* one, double* five, double* fifteen);
int Platform_getMaxPid();
void Process_setupColumnWidths();
double Platform_setCPUValues(Meter* mtr, int cpu);
void Platform_setMemoryValues(Meter* mtr);

View File

@ -75,32 +75,15 @@ ProcessFieldData Process_fields[] = {
[LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
};
char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
if (maxPid > 99999) {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = " TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = "TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
}
}
ProcessPidColumn Process_pidColumns[] = {
{ .id = PID, .label = "PID" },
{ .id = PPID, .label = "PPID" },
{ .id = TPGID, .label = "TPGID" },
{ .id = TGID, .label = "TGID" },
{ .id = PGRP, .label = "PGRP" },
{ .id = SESSION, .label = "SESN" },
{ .id = 0, .label = NULL },
};
FreeBSDProcess* FreeBSDProcess_new(Settings* settings) {
FreeBSDProcess* this = calloc(sizeof(FreeBSDProcess), 1);

View File

@ -33,9 +33,6 @@ extern ProcessClass FreeBSDProcess_class;
extern ProcessFieldData Process_fields[];
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
void Process_setupColumnWidths();
FreeBSDProcess* FreeBSDProcess_new(Settings* settings);

View File

@ -217,40 +217,19 @@ ProcessFieldData Process_fields[] = {
[LAST_PROCESSFIELD] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
};
char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
if (maxPid > 99999) {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
#ifdef HAVE_OPENVZ
Process_fields[VPID].title = " VPID ";
#endif
Process_fields[TPGID].title = " TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_fields[OOM].title = " OOM ";
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
#ifdef HAVE_OPENVZ
Process_fields[VPID].title = " VPID ";
#endif
Process_fields[TPGID].title = "TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_fields[OOM].title = " OOM ";
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
}
}
ProcessPidColumn Process_pidColumns[] = {
{ .id = PID, .label = "PID" },
{ .id = PPID, .label = "PPID" },
#ifdef HAVE_OPENVZ
{ .id = VPID, .label = "VPID" },
#endif
{ .id = TPGID, .label = "TPGID" },
{ .id = TGID, .label = "TGID" },
{ .id = PGRP, .label = "PGRP" },
{ .id = SESSION, .label = "SESN" },
{ .id = OOM, .label = "OOM" },
{ .id = 0, .label = NULL },
};
ProcessClass LinuxProcess_class = {
.super = {

View File

@ -129,10 +129,7 @@ typedef struct LinuxProcess_ {
extern ProcessFieldData Process_fields[];
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
void Process_setupColumnWidths();
extern ProcessPidColumn Process_pidColumns[];
extern ProcessClass LinuxProcess_class;

View File

@ -80,7 +80,6 @@ void Platform_setBindings(Htop_Action* keys) {
int Platform_numberOfFields = 100;
char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";
int Platform_getUptime() {
return 0;
@ -107,7 +106,6 @@ void Process_setupColumnWidths() {
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
@ -116,7 +114,6 @@ void Process_setupColumnWidths() {
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
}
}

View File

@ -24,7 +24,6 @@ void Platform_setBindings(Htop_Action* keys);
extern int Platform_numberOfFields;
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
int Platform_getUptime();