--sort-key flag in the command-line, overriding the

saved setting in .htoprc for the session.
(thanks to Rodolfo Borges)
This commit is contained in:
Hisham Muhammad 2006-08-04 20:54:37 +00:00
parent bf44e233e6
commit b10821aae9
6 changed files with 41 additions and 9 deletions

View File

@ -1,4 +1,10 @@
What's new in version 0.6.4
* --sort-key flag in the command-line, overriding the
saved setting in .htoprc for the session.
(thanks to Rodolfo Borges)
What's new in version 0.6.3 What's new in version 0.6.3
* Performance improvements: uses much less CPU than the * Performance improvements: uses much less CPU than the

View File

@ -44,6 +44,15 @@ void ColumnsPanel_delete(Object* object) {
free(this); free(this);
} }
int ColumnsPanel_fieldNameToIndex(const char* name) {
for (int j = 1; j <= LAST_PROCESSFIELD; j++) {
if (String_eq(name, Process_fieldNames[j])) {
return j;
}
}
return 0;
}
void ColumnsPanel_update(Panel* super) { void ColumnsPanel_update(Panel* super) {
ColumnsPanel* this = (ColumnsPanel*) super; ColumnsPanel* this = (ColumnsPanel*) super;
int size = Panel_getSize(super); int size = Panel_getSize(super);
@ -53,12 +62,9 @@ void ColumnsPanel_update(Panel* super) {
this->settings->pl->fields = (ProcessField*) malloc(sizeof(ProcessField) * (size+1)); this->settings->pl->fields = (ProcessField*) malloc(sizeof(ProcessField) * (size+1));
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
char* text = ((ListItem*) Panel_get(super, i))->value; char* text = ((ListItem*) Panel_get(super, i))->value;
for (int j = 1; j <= LAST_PROCESSFIELD; j++) { int j = ColumnsPanel_fieldNameToIndex(text);
if (String_eq(text, Process_fieldNames[j])) { if (j > 0)
this->settings->pl->fields[i] = j; this->settings->pl->fields[i] = j;
break;
}
}
} }
this->settings->pl->fields[size] = 0; this->settings->pl->fields[size] = 0;
} }
@ -83,7 +89,7 @@ HandlerResult ColumnsPanel_eventHandler(Panel* super, int ch) {
case ']': case ']':
case '+': case '+':
{ {
if (selected < size - 2) if (selected < size - 2)
Panel_moveSelectedDown(super); Panel_moveSelectedDown(super);
result = HANDLED; result = HANDLED;
break; break;

View File

@ -24,6 +24,8 @@ ColumnsPanel* ColumnsPanel_new(Settings* settings, ScreenManager* scr);
void ColumnsPanel_delete(Object* object); void ColumnsPanel_delete(Object* object);
int ColumnsPanel_fieldNameToIndex(const char* name);
void ColumnsPanel_update(Panel* super); void ColumnsPanel_update(Panel* super);
HandlerResult ColumnsPanel_eventHandler(Panel* super, int ch); HandlerResult ColumnsPanel_eventHandler(Panel* super, int ch);

View File

@ -96,7 +96,7 @@ void String_printPointer(void* p) {
printf("%p", p); printf("%p", p);
} }
inline int String_eq(char* s1, char* s2) { inline int String_eq(const char* s1, const char* s2) {
if (s1 == NULL || s2 == NULL) { if (s1 == NULL || s2 == NULL) {
if (s1 == NULL && s2 == NULL) if (s1 == NULL && s2 == NULL)
return 1; return 1;

View File

@ -39,7 +39,7 @@ void String_printInt(int i);
void String_printPointer(void* p); void String_printPointer(void* p);
inline int String_eq(char* s1, char* s2); inline int String_eq(const char* s1, const char* s2);
char** String_split(char* s, char sep); char** String_split(char* s, char sep);

18
htop.c
View File

@ -46,6 +46,7 @@ void printHelpFlag() {
printf("Released under the GNU GPL.\n\n"); printf("Released under the GNU GPL.\n\n");
printf("-d DELAY Delay between updates, in tenths of seconds\n\n"); printf("-d DELAY Delay between updates, in tenths of seconds\n\n");
printf("-u USERNAME Show only processes of a given user\n\n"); printf("-u USERNAME Show only processes of a given user\n\n");
printf("--sort-key COLUMN Sort by this column (use --sort-key help for a column list)\n\n");
printf("Press F1 inside htop for online help.\n"); printf("Press F1 inside htop for online help.\n");
printf("See the man page for full information.\n\n"); printf("See the man page for full information.\n\n");
exit(0); exit(0);
@ -194,12 +195,25 @@ int main(int argc, char** argv) {
int delay = -1; int delay = -1;
bool userOnly = false; bool userOnly = false;
uid_t userId = 0; uid_t userId = 0;
int sortKey = 0;
if (argc > 0) { if (argc > 0) {
if (String_eq(argv[1], "--help")) { if (String_eq(argv[1], "--help")) {
printHelpFlag(); printHelpFlag();
} else if (String_eq(argv[1], "--version")) { } else if (String_eq(argv[1], "--version")) {
printVersionFlag(); printVersionFlag();
} else if (String_eq(argv[1], "--sort-key")) {
if (argc < 2) printHelpFlag();
if (String_eq(argv[2], "help")) {
for (int j = 1; j < LAST_PROCESSFIELD; j++)
printf ("%s\n", Process_fieldNames[j]);
exit(0);
}
sortKey = ColumnsPanel_fieldNameToIndex(argv[2]);
if (sortKey == -1) {
fprintf(stderr, "Error: invalid column \"%s\".\n", argv[2]);
exit(1);
}
} else if (String_eq(argv[1], "-d")) { } else if (String_eq(argv[1], "-d")) {
if (argc < 2) printHelpFlag(); if (argc < 2) printHelpFlag();
sscanf(argv[2], "%d", &delay); sscanf(argv[2], "%d", &delay);
@ -237,6 +251,10 @@ int main(int argc, char** argv) {
Header* header = Header_new(pl); Header* header = Header_new(pl);
settings = Settings_new(pl, header); settings = Settings_new(pl, header);
if (sortKey > 0) {
pl->sortKey = sortKey;
pl->treeView = false;
}
int headerHeight = Header_calculateHeight(header); int headerHeight = Header_calculateHeight(header);
// FIXME: move delay code to settings // FIXME: move delay code to settings