From 2a73405cd060b543d56987650327b18078a137d7 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 10 Nov 2012 00:31:37 +0000 Subject: search and filter for the strace and lsof screens! --- CategoriesPanel.c | 6 ++ IncSet.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ IncSet.h | 56 ++++++++++++++ ListItem.c | 16 ++-- ListItem.h | 2 +- Makefile.am | 10 +-- OpenFilesScreen.c | 141 ++++++++++++++++++++++-------------- OpenFilesScreen.h | 1 - ProcessList.c | 7 +- ProcessList.h | 3 +- RichString.c | 27 +++---- RichString.h | 8 +- ScreenManager.c | 2 +- TraceScreen.c | 146 ++++++++++++++++++++++++------------- TraceScreen.h | 1 - Vector.c | 48 ++++++------ Vector.h | 4 +- htop.c | 161 +++++++++-------------------------------- htop.h | 6 +- 19 files changed, 559 insertions(+), 299 deletions(-) create mode 100644 IncSet.c create mode 100644 IncSet.h diff --git a/CategoriesPanel.c b/CategoriesPanel.c index 0114ae4b..449a5be6 100644 --- a/CategoriesPanel.c +++ b/CategoriesPanel.c @@ -101,6 +101,12 @@ static HandlerResult CategoriesPanel_eventHandler(Panel* super, int ch) { result = HANDLED; break; } + default: + if (isalpha(ch)) + result = Panel_selectByTyping(super, ch); + if (result == BREAK_LOOP) + result = IGNORED; + break; } if (result == HANDLED) { diff --git a/IncSet.c b/IncSet.c new file mode 100644 index 00000000..2b9e052b --- /dev/null +++ b/IncSet.c @@ -0,0 +1,213 @@ +/* +htop - IncSet.c +(C) 2005-2012 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + +#include "IncSet.h" +#include "String.h" +#include "Panel.h" +#include "ListItem.h" +#include "CRT.h" +#include +#include + +/*{ + +#include "FunctionBar.h" +#include "Panel.h" +#include + +#define INCMODE_MAX 40 + +typedef enum { + INC_SEARCH = 0, + INC_FILTER = 1 +} IncType; + +#define IncSet_filter(inc_) (inc_->filtering ? inc_->modes[INC_FILTER].buffer : NULL) + +typedef struct IncMode_ { + char buffer[INCMODE_MAX]; + int index; + FunctionBar* bar; + bool isFilter; +} IncMode; + +typedef struct IncSet_ { + IncMode modes[2]; + IncMode* active; + FunctionBar* bar; + FunctionBar* defaultBar; + bool filtering; +} IncSet; + +typedef const char* (*IncMode_GetPanelValue)(Panel*, int); + +}*/ + +static void IncMode_reset(IncMode* mode) { + mode->index = 0; + mode->buffer[0] = 0; +} + +static const char* searchFunctions[] = {"Next ", "Cancel ", " Search: ", NULL}; +static const char* searchKeys[] = {"F3", "Esc", " "}; +static int searchEvents[] = {KEY_F(3), 27, ERR}; + +static inline void IncMode_initSearch(IncMode* search) { + memset(search, 0, sizeof(IncMode)); + search->bar = FunctionBar_new(searchFunctions, searchKeys, searchEvents); + search->isFilter = false; +} + +static const char* filterFunctions[] = {"Done ", "Clear ", " Filter: ", NULL}; +static const char* filterKeys[] = {"Enter", "Esc", " "}; +static int filterEvents[] = {13, 27, ERR}; + +static inline void IncMode_initFilter(IncMode* filter) { + memset(filter, 0, sizeof(IncMode)); + filter->bar = FunctionBar_new(filterFunctions, filterKeys, filterEvents); + filter->isFilter = true; +} + +static inline void IncMode_done(IncMode* mode) { + FunctionBar_delete((Object*)mode->bar); +} + +IncSet* IncSet_new(FunctionBar* bar) { + IncSet* this = calloc(sizeof(IncSet), 1); + IncMode_initSearch(&(this->modes[INC_SEARCH])); + IncMode_initFilter(&(this->modes[INC_FILTER])); + this->active = NULL; + this->filtering = false; + this->bar = bar; + this->defaultBar = bar; + return this; +} + +void IncSet_delete(IncSet* this) { + IncMode_done(&(this->modes[0])); + IncMode_done(&(this->modes[1])); + free(this); +} + +static void updateWeakPanel(IncSet* this, Panel* panel, Vector* lines) { + Object* selected = Panel_getSelected(panel); + Panel_prune(panel); + if (this->filtering) { + int n = 0; + const char* incFilter = this->modes[INC_FILTER].buffer; + for (int i = 0; i < Vector_size(lines); i++) { + ListItem* line = (ListItem*)Vector_get(lines, i); + if (String_contains_i(line->value, incFilter)) { + Panel_add(panel, (Object*)line); + if (selected == (Object*)line) Panel_setSelected(panel, n); + n++; + } + } + } else { + for (int i = 0; i < Vector_size(lines); i++) { + Object* line = Vector_get(lines, i); + Panel_add(panel, line); + if (selected == line) Panel_setSelected(panel, i); + } + } +} + +static void search(IncMode* mode, Panel* panel, IncMode_GetPanelValue getPanelValue) { + int size = Panel_size(panel); + bool found = false; + for (int i = 0; i < size; i++) { + if (String_contains_i(getPanelValue(panel, i), mode->buffer)) { + Panel_setSelected(panel, i); + found = true; + break; + } + } + if (found) + FunctionBar_draw(mode->bar, mode->buffer); + else + FunctionBar_drawAttr(mode->bar, mode->buffer, CRT_colors[FAILED_SEARCH]); +} + +bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue getPanelValue, Vector* lines) { + if (ch == ERR) + return true; + IncMode* mode = this->active; + int size = Panel_size(panel); + bool filterChange = false; + bool doSearch = true; + if (ch == KEY_F(3)) { + if (size == 0) return true; + int here = Panel_getSelectedIndex(panel); + int i = here; + for(;;) { + i++; + if (i == size) i = 0; + if (i == here) break; + if (String_contains_i(getPanelValue(panel, i), mode->buffer)) { + Panel_setSelected(panel, i); + break; + } + } + doSearch = false; + } else if (isprint((char)ch) && (mode->index < INCMODE_MAX)) { + mode->buffer[mode->index] = ch; + mode->index++; + mode->buffer[mode->index] = 0; + if (mode->isFilter) { + filterChange = true; + if (mode->index == 1) this->filtering = true; + } + } else if ((ch == KEY_BACKSPACE || ch == 127) && (mode->index > 0)) { + mode->index--; + mode->buffer[mode->index] = 0; + if (mode->isFilter) { + filterChange = true; + if (mode->index == 0) { + this->filtering = false; + IncMode_reset(mode); + } + } + } else { + if (mode->isFilter) { + filterChange = true; + if (ch == 27) { + this->filtering = false; + IncMode_reset(mode); + } + } else { + IncMode_reset(mode); + } + this->active = NULL; + this->bar = this->defaultBar; + FunctionBar_draw(this->defaultBar, NULL); + doSearch = false; + } + if (doSearch) { + search(mode, panel, getPanelValue); + } + if (filterChange && lines) { + updateWeakPanel(this, panel, lines); + } + return filterChange; +} + +const char* IncSet_getListItemValue(Panel* panel, int i) { + ListItem* l = (ListItem*) Panel_get(panel, i); + if (l) + return l->value; + return ""; +} + +void IncSet_activate(IncSet* this, IncType type) { + this->active = &(this->modes[type]); + this->bar = this->active->bar; + FunctionBar_draw(this->active->bar, this->active->buffer); +} + +void IncSet_drawBar(IncSet* this) { + FunctionBar_draw(this->bar, this->active ? this->active->buffer : NULL); +} diff --git a/IncSet.h b/IncSet.h new file mode 100644 index 00000000..fe425330 --- /dev/null +++ b/IncSet.h @@ -0,0 +1,56 @@ +/* Do not edit this file. It was automatically generated. */ + +#ifndef HEADER_IncSet +#define HEADER_IncSet +/* +htop - IncSet.h +(C) 2005-2012 Hisham H. Muhammad +Released under the GNU GPL, see the COPYING file +in the source distribution for its full text. +*/ + + +#include "FunctionBar.h" +#include "Panel.h" +#include + +#define INCMODE_MAX 40 + +typedef enum { + INC_SEARCH = 0, + INC_FILTER = 1 +} IncType; + +#define IncSet_filter(inc_) (inc_->filtering ? inc_->modes[INC_FILTER].buffer : NULL) + +typedef struct IncMode_ { + char buffer[INCMODE_MAX]; + int index; + FunctionBar* bar; + bool isFilter; +} IncMode; + +typedef struct IncSet_ { + IncMode modes[2]; + IncMode* active; + FunctionBar* bar; + FunctionBar* defaultBar; + bool filtering; +} IncSet; + +typedef const char* (*IncMode_GetPanelValue)(Panel*, int); + + +IncSet* IncSet_new(FunctionBar* bar); + +void IncSet_delete(IncSet* this); + +bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue getPanelValue, Vector* lines); + +const char* IncSet_getListItemValue(Panel* panel, int i); + +void IncSet_activate(IncSet* this, IncType type); + +void IncSet_drawBar(IncSet* this); + +#endif diff --git a/ListItem.c b/ListItem.c index 9beed0cb..af50ca95 100644 --- a/ListItem.c +++ b/ListItem.c @@ -41,10 +41,12 @@ static void ListItem_delete(Object* cast) { static void ListItem_display(Object* cast, RichString* out) { ListItem* this = (ListItem*)cast; assert (this != NULL); + /* int len = strlen(this->value)+1; char buffer[len+1]; snprintf(buffer, len, "%s", this->value); - RichString_write(out, CRT_colors[DEFAULT_COLOR], buffer); + */ + RichString_write(out, CRT_colors[DEFAULT_COLOR], this->value/*buffer*/); } ListItem* ListItem_new(const char* value, int key) { @@ -57,11 +59,13 @@ ListItem* ListItem_new(const char* value, int key) { return this; } -void ListItem_append(ListItem* this, char* text) { - char* buf = malloc(strlen(this->value) + strlen(text) + 1); - sprintf(buf, "%s%s", this->value, text); - free(this->value); - this->value = buf; +void ListItem_append(ListItem* this, const char* text) { + int oldLen = strlen(this->value); + int textLen = strlen(text); + int newLen = strlen(this->value) + textLen; + this->value = realloc(this->value, newLen + 1); + memcpy(this->value + oldLen, text, textLen); + this->value[newLen] = '\0'; } const char* ListItem_getRef(ListItem* this) { diff --git a/ListItem.h b/ListItem.h index 8df792c1..083a5c99 100644 --- a/ListItem.h +++ b/ListItem.h @@ -26,7 +26,7 @@ extern char* LISTITEM_CLASS; ListItem* ListItem_new(const char* value, int key); -void ListItem_append(ListItem* this, char* text); +void ListItem_append(ListItem* this, const char* text); const char* ListItem_getRef(ListItem* this); diff --git a/Makefile.am b/Makefile.am index 05a72e7e..0aa33596 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,16 +20,16 @@ LoadAverageMeter.c MemoryMeter.c Meter.c MetersPanel.c Object.c Panel.c \ BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \ IOPriorityPanel.c SignalsPanel.c String.c SwapMeter.c TasksMeter.c TraceScreen.c \ UptimeMeter.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.c \ -HostnameMeter.c OpenFilesScreen.c Affinity.c IOPriority.c +HostnameMeter.c OpenFilesScreen.c Affinity.c IOPriority.c IncSet.c myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \ CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \ IOPriorityPanel.h CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \ Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.h \ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \ -ScreenManager.h Settings.h SignalsPanel.h String.h \ -SwapMeter.h TasksMeter.h TraceScreen.h UptimeMeter.h UsersTable.h Vector.h \ -Process.h AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IOPriority.h +ScreenManager.h Settings.h SignalsPanel.h String.h SwapMeter.h TasksMeter.h \ +TraceScreen.h UptimeMeter.h UsersTable.h Vector.h Process.h AffinityPanel.h \ +HostnameMeter.h OpenFilesScreen.h Affinity.h IOPriority.h IncSet.h SUFFIXES = .h @@ -37,7 +37,7 @@ BUILT_SOURCES = $(myhtopheaders) htop_SOURCES = $(myhtopheaders) $(myhtopsources) config.h profile: - $(MAKE) all CFLAGS="" AM_CPPFLAGS="-pg -O2" + $(MAKE) all CFLAGS="-pg" AM_CPPFLAGS="-pg -O2 -DNDEBUG" debug: $(MAKE) all CFLAGS="" AM_CPPFLAGS="-ggdb -DDEBUG" diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index 9adf8b4b..30d02af0 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -10,6 +10,8 @@ in the source distribution for its full text. #include "CRT.h" #include "ProcessList.h" #include "ListItem.h" +#include "IncSet.h" +#include "String.h" #include #include @@ -42,23 +44,20 @@ typedef struct OpenFilesScreen_ { pid_t pid; Panel* display; FunctionBar* bar; - bool tracing; } OpenFilesScreen; }*/ -static const char* ofsFunctions[] = {"Refresh", "Done ", NULL}; +static const char* ofsFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL}; -static const char* ofsKeys[] = {"F5", "Esc"}; +static const char* ofsKeys[] = {"F3", "F4", "F5", "Esc"}; -static int ofsEvents[] = {KEY_F(5), 27}; +static int ofsEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27}; OpenFilesScreen* OpenFilesScreen_new(Process* process) { OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen)); this->process = process; - this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, true, ListItem_compare); - this->bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents); - this->tracing = true; + this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, false, ListItem_compare); if (Process_isThread(process)) this->pid = process->tgid; else @@ -68,30 +67,29 @@ OpenFilesScreen* OpenFilesScreen_new(Process* process) { void OpenFilesScreen_delete(OpenFilesScreen* this) { Panel_delete((Object*)this->display); - FunctionBar_delete((Object*)this->bar); free(this); } -static void OpenFilesScreen_draw(OpenFilesScreen* this) { +static void OpenFilesScreen_draw(OpenFilesScreen* this, IncSet* inc) { attrset(CRT_colors[METER_TEXT]); mvhline(0, 0, ' ', COLS); - mvprintw(0, 0, "Files open in process %d - %s", this->pid, this->process->comm); + mvprintw(0, 0, "Snapshot of files open in process %d - %s", this->pid, this->process->comm); attrset(CRT_colors[DEFAULT_COLOR]); Panel_draw(this->display, true); - FunctionBar_draw(this->bar, NULL); + IncSet_drawBar(inc); } static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { char command[1025]; snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid); FILE* fd = popen(command, "r"); - OpenFiles_ProcessData* process = calloc(sizeof(OpenFiles_ProcessData), 1); - OpenFiles_FileData* file = NULL; - OpenFiles_ProcessData* item = process; + OpenFiles_ProcessData* pdata = calloc(sizeof(OpenFiles_ProcessData), 1); + OpenFiles_FileData* fdata = NULL; + OpenFiles_ProcessData* item = pdata; bool anyRead = false; if (!fd) { - process->error = 127; - return process; + pdata->error = 127; + return pdata; } while (!feof(fd)) { int cmd = fgetc(fd); @@ -107,53 +105,60 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { *newline = '\0'; if (cmd == 'f') { OpenFiles_FileData* nextFile = calloc(sizeof(OpenFiles_ProcessData), 1); - if (file == NULL) { - process->files = nextFile; + if (fdata == NULL) { + pdata->files = nextFile; } else { - file->next = nextFile; + fdata->next = nextFile; } - file = nextFile; - item = (OpenFiles_ProcessData*) file; + fdata = nextFile; + item = (OpenFiles_ProcessData*) fdata; } item->data[cmd] = entry; } - process->error = pclose(fd); - return process; + pdata->error = pclose(fd); + return pdata; } -static void OpenFilesScreen_scan(OpenFilesScreen* this) { +static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) { + Vector_add(lines, (Object*) ListItem_new(line, 0)); + if (!incFilter || String_contains_i(line, incFilter)) + Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1)); +} + +static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* inc) { Panel* panel = this->display; - int idx = MAX(Panel_getSelectedIndex(panel), 0); + int idx = Panel_getSelectedIndex(panel); Panel_prune(panel); - OpenFiles_ProcessData* process = OpenFilesScreen_getProcessData(this->pid); - if (process->error == 127) { - Panel_add(panel, (Object*) ListItem_new("Could not execute 'lsof'. Please make sure it is available in your $PATH.", 0)); - } else if (process->error == 1) { - Panel_add(panel, (Object*) ListItem_new("Failed listing open files.", 0)); + OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(this->pid); + if (pdata->error == 127) { + addLine("Could not execute 'lsof'. Please make sure it is available in your $PATH.", lines, panel, IncSet_filter(inc)); + } else if (pdata->error == 1) { + addLine("Failed listing open files.", lines, panel, IncSet_filter(inc)); } else { - OpenFiles_FileData* file = process->files; - while (file) { + OpenFiles_FileData* fdata = pdata->files; + while (fdata) { char entry[1024]; sprintf(entry, "%5s %4s %10s %10s %10s %s", - file->data['f'] ? file->data['f'] : "", - file->data['t'] ? file->data['t'] : "", - file->data['D'] ? file->data['D'] : "", - file->data['s'] ? file->data['s'] : "", - file->data['i'] ? file->data['i'] : "", - file->data['n'] ? file->data['n'] : ""); - Panel_add(panel, (Object*) ListItem_new(entry, 0)); + fdata->data['f'] ? fdata->data['f'] : "", + fdata->data['t'] ? fdata->data['t'] : "", + fdata->data['D'] ? fdata->data['D'] : "", + fdata->data['s'] ? fdata->data['s'] : "", + fdata->data['i'] ? fdata->data['i'] : "", + fdata->data['n'] ? fdata->data['n'] : ""); + addLine(entry, lines, panel, IncSet_filter(inc)); for (int i = 0; i < 255; i++) - if (file->data[i]) - free(file->data[i]); - OpenFiles_FileData* old = file; - file = file->next; + if (fdata->data[i]) + free(fdata->data[i]); + OpenFiles_FileData* old = fdata; + fdata = fdata->next; free(old); } for (int i = 0; i < 255; i++) - if (process->data[i]) - free(process->data[i]); + if (pdata->data[i]) + free(pdata->data[i]); } - free(process); + free(pdata); + Vector_insertionSort(lines); Vector_insertionSort(panel->items); Panel_setSelected(panel, idx); } @@ -161,14 +166,24 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this) { void OpenFilesScreen_run(OpenFilesScreen* this) { Panel* panel = this->display; Panel_setHeader(panel, " FD TYPE DEVICE SIZE NODE NAME"); - OpenFilesScreen_scan(this); - OpenFilesScreen_draw(this); - //CRT_disableDelay(); + + FunctionBar* bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents); + IncSet* inc = IncSet_new(bar); + + Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE, ListItem_compare); + + OpenFilesScreen_scan(this, lines, inc); + OpenFilesScreen_draw(this, inc); bool looping = true; while (looping) { + Panel_draw(panel, true); + + if (inc->active) + move(LINES-1, CRT_cursorX); int ch = getch(); + if (ch == KEY_MOUSE) { MEVENT mevent; int ok = getmouse(&mevent); @@ -177,19 +192,33 @@ void OpenFilesScreen_run(OpenFilesScreen* this) { Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV); ch = 0; } if (mevent.y == LINES - 1) - ch = FunctionBar_synthesizeEvent(this->bar, mevent.x); + ch = FunctionBar_synthesizeEvent(inc->bar, mevent.x); } + + if (inc->active) { + IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines); + continue; + } + switch(ch) { case ERR: continue; + case KEY_F(3): + case '/': + IncSet_activate(inc, INC_SEARCH); + break; + case KEY_F(4): + case '\\': + IncSet_activate(inc, INC_FILTER); + break; case KEY_F(5): clear(); - OpenFilesScreen_scan(this); - OpenFilesScreen_draw(this); + OpenFilesScreen_scan(this, lines, inc); + OpenFilesScreen_draw(this, inc); break; case '\014': // Ctrl+L clear(); - OpenFilesScreen_draw(this); + OpenFilesScreen_draw(this, inc); break; case 'q': case 27: @@ -198,11 +227,13 @@ void OpenFilesScreen_run(OpenFilesScreen* this) { break; case KEY_RESIZE: Panel_resize(panel, COLS, LINES-2); - OpenFilesScreen_draw(this); + OpenFilesScreen_draw(this, inc); break; default: Panel_onKey(panel, ch); } } - //CRT_enableDelay(); + + FunctionBar_delete((Object*)bar); + IncSet_delete(inc); } diff --git a/OpenFilesScreen.h b/OpenFilesScreen.h index 248cac4b..ce15ea34 100644 --- a/OpenFilesScreen.h +++ b/OpenFilesScreen.h @@ -29,7 +29,6 @@ typedef struct OpenFilesScreen_ { pid_t pid; Panel* display; FunctionBar* bar; - bool tracing; } OpenFilesScreen; diff --git a/ProcessList.c b/ProcessList.c index 04adac54..0138919a 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -112,7 +112,6 @@ typedef struct ProcessList_ { int following; bool userOnly; uid_t userId; - bool filtering; const char* incFilter; Hashtable* pidWhiteList; @@ -918,18 +917,16 @@ void ProcessList_expandTree(ProcessList* this) { } } -void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool userOnly, uid_t userId, bool filtering, const char* incFilter) { +void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool userOnly, uid_t userId, const char* incFilter) { if (!flags) { following = this->following; userOnly = this->userOnly; userId = this->userId; - filtering = this->filtering; incFilter = this->incFilter; } else { this->following = following; this->userOnly = userOnly; this->userId = userId; - this->filtering = filtering; this->incFilter = incFilter; } @@ -946,7 +943,7 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool if ( (!p->show) || (userOnly && (p->st_uid != userId)) - || (filtering && !(String_contains_i(p->comm, incFilter))) + || (incFilter && !(String_contains_i(p->comm, incFilter))) || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) ) hidden = true; diff --git a/ProcessList.h b/ProcessList.h index 34bd5e8b..7385a294 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -95,7 +95,6 @@ typedef struct ProcessList_ { int following; bool userOnly; uid_t userId; - bool filtering; const char* incFilter; Hashtable* pidWhiteList; @@ -185,6 +184,6 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at); void ProcessList_expandTree(ProcessList* this); -void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool userOnly, uid_t userId, bool filtering, const char* incFilter); +void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool userOnly, uid_t userId, const char* incFilter); #endif diff --git a/RichString.c b/RichString.c index eca2293e..33f5880d 100644 --- a/RichString.c +++ b/RichString.c @@ -83,16 +83,15 @@ static inline void RichString_setLen(RichString* this, int len) { #ifdef HAVE_LIBNCURSESW -inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) { +static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) { wchar_t data[len+1]; len = mbstowcs(data, data_c, len); if (len<0) return; - int oldLen = this->chlen; - int newLen = len + oldLen; + int newLen = from + len; RichString_setLen(this, newLen); - for (int i = oldLen, j = 0; i < newLen; i++, j++) { - memset(&this->chptr[i], 0, sizeof(this->chptr[i])); + memset(&this->chptr[from], 0, sizeof(CharType) * (newLen - from)); + for (int i = from, j = 0; i < newLen; i++, j++) { this->chptr[i].chars[0] = data[j]; this->chptr[i].attr = attrs; } @@ -120,11 +119,10 @@ int RichString_findChar(RichString* this, char c, int start) { #else -inline void RichString_appendn(RichString* this, int attrs, const char* data_c, int len) { - int oldLen = this->chlen; - int newLen = len + oldLen; +static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) { + int newLen = from + len; RichString_setLen(this, newLen); - for (int i = oldLen, j = 0; i < newLen; i++, j++) + for (int i = from, j = 0; i < newLen; i++, j++) this->chptr[i] = (isprint(data_c[j]) ? data_c[j] : '?') | attrs; this->chptr[newLen] = 0; } @@ -160,11 +158,14 @@ void RichString_setAttr(RichString* this, int attrs) { RichString_setAttrn(this, attrs, 0, this->chlen - 1); } -inline void RichString_append(RichString* this, int attrs, const char* data) { - RichString_appendn(this, attrs, data, strlen(data)); +void RichString_append(RichString* this, int attrs, const char* data) { + RichString_writeFrom(this, attrs, data, this->chlen, strlen(data)); +} + +void RichString_appendn(RichString* this, int attrs, const char* data, int len) { + RichString_writeFrom(this, attrs, data, this->chlen, len); } void RichString_write(RichString* this, int attrs, const char* data) { - RichString_setLen(this, 0); - RichString_appendn(this, attrs, data, strlen(data)); + RichString_writeFrom(this, attrs, data, 0, strlen(data)); } diff --git a/RichString.h b/RichString.h index 1b4d244a..f22692de 100644 --- a/RichString.h +++ b/RichString.h @@ -61,16 +61,12 @@ typedef struct RichString_ { #ifdef HAVE_LIBNCURSESW -extern void RichString_appendn(RichString* this, int attrs, const char* data_c, int len); - extern void RichString_setAttrn(RichString* this, int attrs, int start, int finish); int RichString_findChar(RichString* this, char c, int start); #else -extern void RichString_appendn(RichString* this, int attrs, const char* data_c, int len); - void RichString_setAttrn(RichString* this, int attrs, int start, int finish); int RichString_findChar(RichString* this, char c, int start); @@ -81,7 +77,9 @@ void RichString_prune(RichString* this); void RichString_setAttr(RichString* this, int attrs); -extern void RichString_append(RichString* this, int attrs, const char* data); +void RichString_append(RichString* this, int attrs, const char* data); + +void RichString_appendn(RichString* this, int attrs, const char* data, int len); void RichString_write(RichString* this, int attrs, const char* data); diff --git a/ScreenManager.c b/ScreenManager.c index 744ea2b0..777e9de3 100644 --- a/ScreenManager.c +++ b/ScreenManager.c @@ -144,7 +144,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { this->lastScan = now; } Header_draw(this->header); - ProcessList_rebuildPanel(this->header->pl, false, false, false, false, false, NULL); + ProcessList_rebuildPanel(this->header->pl, false, false, false, false, NULL); } for (int i = 0; i < panels; i++) { Panel* panel = (Panel*) Vector_get(this->panels, i); diff --git a/TraceScreen.c b/TraceScreen.c index c7250f67..5fc48023 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -10,6 +10,8 @@ in the source distribution for its full text. #include "CRT.h" #include "ProcessList.h" #include "ListItem.h" +#include "IncSet.h" +#include "String.h" #include #include @@ -30,39 +32,49 @@ in the source distribution for its full text. typedef struct TraceScreen_ { Process* process; Panel* display; - FunctionBar* bar; bool tracing; } TraceScreen; }*/ -static const char* tsFunctions[] = {"AutoScroll ", "Stop Tracing ", "Done ", NULL}; +static const char* tsFunctions[] = {"Search ", "Filter ", "AutoScroll ", "Stop Tracing ", "Done ", NULL}; -static const char* tsKeys[] = {"F4", "F5", "Esc"}; +static const char* tsKeys[] = {"F3", "F4", "F8", "F9", "Esc"}; -static int tsEvents[] = {KEY_F(4), KEY_F(5), 27}; +static int tsEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27}; TraceScreen* TraceScreen_new(Process* process) { TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen)); this->process = process; - this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare); - this->bar = FunctionBar_new(tsFunctions, tsKeys, tsEvents); + this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, false, ListItem_compare); this->tracing = true; return this; } void TraceScreen_delete(TraceScreen* this) { Panel_delete((Object*)this->display); - FunctionBar_delete((Object*)this->bar); free(this); } -static void TraceScreen_draw(TraceScreen* this) { +static void TraceScreen_draw(TraceScreen* this, IncSet* inc) { attrset(CRT_colors[PANEL_HEADER_FOCUS]); mvhline(0, 0, ' ', COLS); mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm); attrset(CRT_colors[DEFAULT_COLOR]); - FunctionBar_draw(this->bar, NULL); + IncSet_drawBar(inc); +} + +static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) { + Vector_add(lines, (Object*) ListItem_new(line, 0)); + if (!incFilter || String_contains_i(line, incFilter)) + Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1)); +} + +static inline void appendLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) { + ListItem* last = (ListItem*)Vector_get(lines, Vector_size(lines)-1); + ListItem_append(last, line); + if (incFilter && Panel_get(panel, Panel_size(panel)-1) != (Object*)last && String_contains_i(line, incFilter)) + Panel_add(panel, (Object*)last); } void TraceScreen_run(TraceScreen* this) { @@ -86,47 +98,63 @@ void TraceScreen_run(TraceScreen* this) { FILE* strace = fdopen(fdpair[0], "r"); Panel* panel = this->display; int fd_strace = fileno(strace); - TraceScreen_draw(this); CRT_disableDelay(); bool contLine = false; bool follow = false; bool looping = true; + + FunctionBar* bar = FunctionBar_new(tsFunctions, tsKeys, tsEvents); + IncSet* inc = IncSet_new(bar); + + Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE, ListItem_compare); + + TraceScreen_draw(this, inc); + while (looping) { - fd_set fds; - FD_ZERO(&fds); - FD_SET(fd_strace, &fds); - struct timeval tv; - tv.tv_sec = 0; tv.tv_usec = 500; - int ready = select(fd_strace+1, &fds, NULL, NULL, &tv); - int nread = 0; - if (ready > 0) - nread = fread(buffer, 1, 1000, strace); - if (nread && this->tracing) { - char* line = buffer; - buffer[nread] = '\0'; - for (int i = 0; i < nread; i++) { - if (buffer[i] == '\n') { - buffer[i] = '\0'; - if (contLine) { - ListItem_append((ListItem*)Panel_get(panel, - Panel_size(panel)-1), line); - contLine = false; - } else { - Panel_add(panel, (Object*) ListItem_new(line, 0)); + + Panel_draw(panel, true); + const char* incFilter = IncSet_filter(inc); + + if (inc->active) + move(LINES-1, CRT_cursorX); + int ch = getch(); + + if (ch == ERR) { + fd_set fds; + FD_ZERO(&fds); + FD_SET(STDIN_FILENO, &fds); + FD_SET(fd_strace, &fds); + //struct timeval tv; + //tv.tv_sec = 0; tv.tv_usec = 500; + int ready = select(fd_strace+1, &fds, NULL, NULL, NULL/*&tv*/); + int nread = 0; + if (ready > 0 && FD_ISSET(fd_strace, &fds)) + nread = fread(buffer, 1, 1000, strace); + if (nread && this->tracing) { + char* line = buffer; + buffer[nread] = '\0'; + for (int i = 0; i < nread; i++) { + if (buffer[i] == '\n') { + buffer[i] = '\0'; + if (contLine) { + appendLine(line, lines, panel, incFilter); + contLine = false; + } else { + addLine(line, lines, panel, incFilter); + } + line = buffer+i+1; } - line = buffer+i+1; } + if (line < buffer+nread) { + addLine(line, lines, panel, incFilter); + buffer[nread] = '\0'; + contLine = true; + } + if (follow) + Panel_setSelected(panel, Panel_size(panel)-1); } - if (line < buffer+nread) { - Panel_add(panel, (Object*) ListItem_new(line, 0)); - buffer[nread] = '\0'; - contLine = true; - } - if (follow) - Panel_setSelected(panel, Panel_size(panel)-1); - Panel_draw(panel, true); } - int ch = getch(); + if (ch == KEY_MOUSE) { MEVENT mevent; int ok = getmouse(&mevent); @@ -136,28 +164,43 @@ void TraceScreen_run(TraceScreen* this) { follow = false; ch = 0; } if (mevent.y == LINES - 1) - ch = FunctionBar_synthesizeEvent(this->bar, mevent.x); + ch = FunctionBar_synthesizeEvent(inc->bar, mevent.x); } + + if (inc->active) { + IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines); + continue; + } + switch(ch) { case ERR: continue; - case KEY_F(5): - this->tracing = !this->tracing; - FunctionBar_setLabel(this->bar, KEY_F(5), this->tracing?"Stop Tracing ":"Resume Tracing "); - TraceScreen_draw(this); - break; case KEY_HOME: Panel_setSelected(panel, 0); break; case KEY_END: Panel_setSelected(panel, Panel_size(panel)-1); break; - case 'f': + case KEY_F(3): + case '/': + IncSet_activate(inc, INC_SEARCH); + break; case KEY_F(4): + case '\\': + IncSet_activate(inc, INC_FILTER); + break; + case 'f': + case KEY_F(8): follow = !follow; if (follow) Panel_setSelected(panel, Panel_size(panel)-1); break; + case 't': + case KEY_F(9): + this->tracing = !this->tracing; + FunctionBar_setLabel(bar, KEY_F(9), this->tracing?"Stop Tracing ":"Resume Tracing "); + TraceScreen_draw(this, inc); + break; case 'q': case 27: case KEY_F(10): @@ -165,14 +208,17 @@ void TraceScreen_run(TraceScreen* this) { break; case KEY_RESIZE: Panel_resize(panel, COLS, LINES-2); - TraceScreen_draw(this); + TraceScreen_draw(this, inc); break; default: follow = false; Panel_onKey(panel, ch); } - Panel_draw(panel, true); } + + IncSet_delete(inc); + FunctionBar_delete((Object*)bar); + kill(child, SIGTERM); waitpid(child, NULL, 0); fclose(strace); diff --git a/TraceScreen.h b/TraceScreen.h index 62fb67a7..03d18567 100644 --- a/TraceScreen.h +++ b/TraceScreen.h @@ -16,7 +16,6 @@ in the source distribution for its full text. typedef struct TraceScreen_ { Process* process; Panel* display; - FunctionBar* bar; bool tracing; } TraceScreen; diff --git a/Vector.c b/Vector.c index 955d99c7..78ec2de5 100644 --- a/Vector.c +++ b/Vector.c @@ -27,13 +27,13 @@ typedef struct Vector_ { int arraySize; int growthRate; int items; - char* vectorType; + char* type; bool owner; } Vector; }*/ -Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare) { +Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare) { Vector* this; if (size == DEFAULT_SIZE) @@ -43,7 +43,7 @@ Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compa this->array = (Object**) calloc(size, sizeof(Object*)); this->arraySize = size; this->items = 0; - this->vectorType = vectorType_; + this->type = type; this->owner = owner; this->compare = compare; return this; @@ -65,7 +65,7 @@ static inline bool Vector_isConsistent(Vector* this) { assert(this->items <= this->arraySize); if (this->owner) { for (int i = 0; i < this->items; i++) - if (this->array[i] && this->array[i]->class != this->vectorType) + if (this->array[i] && this->array[i]->class != this->type) return false; return true; } else { @@ -89,12 +89,13 @@ void Vector_prune(Vector* this) { assert(Vector_isConsistent(this)); int i; - for (i = 0; i < this->items; i++) - if (this->array[i]) { - if (this->owner) + if (this->owner) { + for (i = 0; i < this->items; i++) + if (this->array[i]) { (this->array[i])->delete(this->array[i]); - this->array[i] = NULL; - } + //this->array[i] = NULL; + } + } this->items = 0; } @@ -181,24 +182,26 @@ void Vector_insertionSort(Vector* this) { static void Vector_checkArraySize(Vector* this) { assert(Vector_isConsistent(this)); if (this->items >= this->arraySize) { - int i; - i = this->arraySize; + //int i; + //i = this->arraySize; this->arraySize = this->items + this->growthRate; this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize); - for (; i < this->arraySize; i++) - this->array[i] = NULL; + //for (; i < this->arraySize; i++) + // this->array[i] = NULL; } assert(Vector_isConsistent(this)); } void Vector_insert(Vector* this, int idx, void* data_) { - assert(idx >= 0); - assert(((Object*)data_)->class == this->vectorType); Object* data = data_; + assert(idx >= 0); + assert(idx <= this->items); + assert(data_); + assert(data->class == this->type); assert(Vector_isConsistent(this)); Vector_checkArraySize(this); - assert(this->array[this->items] == NULL); + //assert(this->array[this->items] == NULL); for (int i = this->items; i > idx; i--) { this->array[i] = this->array[i-1]; } @@ -211,11 +214,11 @@ Object* Vector_take(Vector* this, int idx) { assert(idx >= 0 && idx < this->items); assert(Vector_isConsistent(this)); Object* removed = this->array[idx]; - assert (removed != NULL); + //assert (removed != NULL); this->items--; for (int i = idx; i < this->items; i++) this->array[i] = this->array[i+1]; - this->array[this->items] = NULL; + //this->array[this->items] = NULL; assert(Vector_isConsistent(this)); return removed; } @@ -251,7 +254,7 @@ void Vector_moveDown(Vector* this, int idx) { void Vector_set(Vector* this, int idx, void* data_) { assert(idx >= 0); - assert(((Object*)data_)->class == this->vectorType); + assert(((Object*)data_)->class == this->type); Object* data = data_; assert(Vector_isConsistent(this)); @@ -306,7 +309,7 @@ static void Vector_merge(Vector* this, Vector* v2) { */ void Vector_add(Vector* this, void* data_) { - assert(data_ && ((Object*)data_)->class == this->vectorType); + assert(data_ && ((Object*)data_)->class == this->type); Object* data = data_; assert(Vector_isConsistent(this)); int i = this->items; @@ -316,13 +319,14 @@ void Vector_add(Vector* this, void* data_) { } inline int Vector_indexOf(Vector* this, void* search_, Object_Compare compare) { - assert(((Object*)search_)->class == this->vectorType); + assert(((Object*)search_)->class == this->type); assert(this->compare); Object* search = search_; assert(Vector_isConsistent(this)); for (int i = 0; i < this->items; i++) { Object* o = (Object*)this->array[i]; - if (o && compare(search, o) == 0) + assert(o); + if (compare(search, o) == 0) return i; } return -1; diff --git a/Vector.h b/Vector.h index 24e2976c..dc3361a2 100644 --- a/Vector.h +++ b/Vector.h @@ -23,12 +23,12 @@ typedef struct Vector_ { int arraySize; int growthRate; int items; - char* vectorType; + char* type; bool owner; } Vector; -Vector* Vector_new(char* vectorType_, bool owner, int size, Object_Compare compare); +Vector* Vector_new(char* type, bool owner, int size, Object_Compare compare); void Vector_delete(Vector* this); diff --git a/htop.c b/htop.c index 44aa8579..a48e1d83 100644 --- a/htop.c +++ b/htop.c @@ -23,6 +23,7 @@ in the source distribution for its full text. #include "OpenFilesScreen.h" #include "AffinityPanel.h" #include "IOPriorityPanel.h" +#include "IncSet.h" #include #include @@ -38,9 +39,7 @@ in the source distribution for its full text. //#link m -#define INCSEARCH_MAX 40 - -#define COPYRIGHT "(C) 2004-2011 Hisham Muhammad" +#define COPYRIGHT "(C) 2004-2012 Hisham Muhammad" static void printVersionFlag() { fputs("htop " VERSION " - " COPYRIGHT "\n" @@ -263,15 +262,11 @@ static inline void setSortKey(ProcessList* pl, ProcessField sortKey, Panel* pane ProcessList_printHeader(pl, Panel_getHeader(panel)); } -typedef struct IncBuffer_ { - char buffer[INCSEARCH_MAX]; - int index; - FunctionBar* bar; -} IncBuffer; - -static void IncBuffer_reset(IncBuffer* inc) { - inc->index = 0; - inc->buffer[0] = 0; +static const char* getMainPanelValue(Panel* panel, int i) { + Process* p = (Process*) Panel_get(panel, i); + if (p) + return p->comm; + return ""; } int main(int argc, char** argv) { @@ -439,28 +434,13 @@ int main(int argc, char** argv) { pl->direction = 1; } ProcessList_printHeader(pl, Panel_getHeader(panel)); - - IncBuffer incSearch, incFilter; - bool filtering = false; - - memset(&incSearch, 0, sizeof(IncBuffer)); - const char* searchFunctions[] = {"Next ", "Cancel ", " Search: ", NULL}; - const char* searchKeys[] = {"F3", "Esc", " "}; - int searchEvents[] = {KEY_F(3), 27, ERR}; - incSearch.bar = FunctionBar_new(searchFunctions, searchKeys, searchEvents); - - memset(&incFilter, 0, sizeof(IncBuffer)); - const char* filterFunctions[] = {"Done ", "Clear ", " Filter: ", NULL}; - const char* filterKeys[] = {"Enter", "Esc", " "}; - int filterEvents[] = {13, 27, ERR}; - incFilter.bar = FunctionBar_new(filterFunctions, filterKeys, filterEvents); - - IncBuffer* incMode = NULL; const char* defaultFunctions[] = {"Help ", "Setup ", "Search", "Filter", "Tree ", "SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL}; FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL); + IncSet* inc = IncSet_new(defaultBar); + ProcessList_scan(pl); usleep(75000); @@ -494,7 +474,7 @@ int main(int argc, char** argv) { ProcessList_sort(pl); refreshTimeout = 1; } - ProcessList_rebuildPanel(pl, true, following, userOnly, userId, filtering, incFilter.buffer); + ProcessList_rebuildPanel(pl, true, following, userOnly, userId, IncSet_filter(inc)); } doRefresh = true; @@ -502,12 +482,12 @@ int main(int argc, char** argv) { Panel_draw(panel, true); int prev = ch; - if (incMode) + if (inc->active) move(LINES-1, CRT_cursorX); ch = getch(); if (ch == ERR) { - if (!incMode) + if (!inc->active) refreshTimeout--; if (prev == ch && !recalculate) { closeTimeout++; @@ -519,83 +499,6 @@ int main(int argc, char** argv) { continue; } - if (incMode) { - doRefresh = false; - int size = Panel_size(panel); - if (ch == KEY_F(3)) { - if (Panel_size(panel) == 0) continue; - int here = Panel_getSelectedIndex(panel); - int i = here+1; - while (i != here) { - if (i == size) - i = 0; - Process* p = (Process*) Panel_get(panel, i); - if (String_contains_i(p->comm, incMode->buffer)) { - Panel_setSelected(panel, i); - break; - } - i++; - } - continue; - } else if (isprint((char)ch) && (incMode->index < INCSEARCH_MAX)) { - incMode->buffer[incMode->index] = ch; - incMode->index++; - incMode->buffer[incMode->index] = 0; - if (incMode == &incFilter) { - doRefresh = true; - if (incFilter.index == 1) filtering = true; - } - } else if ((ch == KEY_BACKSPACE || ch == 127) && (incMode->index > 0)) { - incMode->index--; - incMode->buffer[incMode->index] = 0; - if (incMode == &incFilter) { - doRefresh = true; - if (incFilter.index == 0) { - filtering = false; - IncBuffer_reset(incMode); - } - } - } else { - if (incMode == &incFilter) { - doRefresh = true; - if (ch == 27) { - filtering = false; - IncBuffer_reset(incMode); - } - } - incMode = NULL; - FunctionBar_draw(defaultBar, NULL); - continue; - } - - bool found = false; - for (int i = 0; i < size; i++) { - Process* p = (Process*) Panel_get(panel, i); - if (String_contains_i(p->comm, incSearch.buffer)) { - Panel_setSelected(panel, i); - found = true; - break; - } - } - if (found) - FunctionBar_draw(incMode->bar, incMode->buffer); - else - FunctionBar_drawAttr(incMode->bar, incMode->buffer, CRT_colors[FAILED_SEARCH]); - continue; - } - if (isdigit((char)ch)) { - if (Panel_size(panel) == 0) continue; - pid_t pid = ch-48 + acc; - for (int i = 0; i < ProcessList_size(pl) && ((Process*) Panel_getSelected(panel))->pid != pid; i++) - Panel_setSelected(panel, i); - acc = pid * 10; - if (acc > 10000000) - acc = 0; - continue; - } else { - acc = 0; - } - if (ch == KEY_MOUSE) { MEVENT mevent; int ok = getmouse(&mevent); @@ -619,10 +522,7 @@ int main(int argc, char** argv) { follow = true; continue; } if (mevent.y == LINES - 1) { - FunctionBar* bar; - if (incMode) bar = incMode->bar; - else bar = defaultBar; - ch = FunctionBar_synthesizeEvent(bar, mevent.x); + ch = FunctionBar_synthesizeEvent(inc->bar, mevent.x); } } else if (mevent.bstate & BUTTON4_CLICKED) { ch = KEY_UP; @@ -634,13 +534,28 @@ int main(int argc, char** argv) { } } + if (inc->active) { + doRefresh = IncSet_handleKey(inc, ch, panel, getMainPanelValue, NULL); + continue; + } + + if (isdigit((char)ch)) { + if (Panel_size(panel) == 0) continue; + pid_t pid = ch-48 + acc; + for (int i = 0; i < ProcessList_size(pl) && ((Process*) Panel_getSelected(panel))->pid != pid; i++) + Panel_setSelected(panel, i); + acc = pid * 10; + if (acc > 10000000) + acc = 0; + continue; + } else { + acc = 0; + } + switch (ch) { case KEY_RESIZE: Panel_resize(panel, COLS, LINES-headerHeight-1); - if (incMode) - FunctionBar_draw(incMode->bar, incMode->buffer); - else - FunctionBar_draw(defaultBar, NULL); + IncSet_drawBar(inc); break; case 'M': { @@ -834,7 +749,7 @@ int main(int argc, char** argv) { const char* fuFunctions[] = {"Sort ", "Cancel ", NULL}; ProcessField* fields = pl->fields; for (int i = 0; fields[i]; i++) { - char* name = String_trim(Process_fieldTitles[fields[i]]); + char* name = String_trim(Process_fieldNames[fields[i]]); Panel_add(sortPanel, (Object*) ListItem_new(name, fields[i])); if (fields[i] == pl->sortKey) Panel_setSelected(sortPanel, i); @@ -891,16 +806,13 @@ int main(int argc, char** argv) { } case KEY_F(3): case '/': - incMode = &incSearch; - IncBuffer_reset(incMode); - FunctionBar_draw(incSearch.bar, incSearch.buffer); + IncSet_activate(inc, INC_SEARCH); break; case KEY_F(4): case '\\': - incMode = &incFilter; + IncSet_activate(inc, INC_FILTER); refreshTimeout = 0; doRefresh = true; - FunctionBar_draw(incFilter.bar, incFilter.buffer); continue; case 't': case KEY_F(5): @@ -943,8 +855,7 @@ int main(int argc, char** argv) { Settings_write(settings); Header_delete(header); ProcessList_delete(pl); - FunctionBar_delete((Object*)incFilter.bar); - FunctionBar_delete((Object*)incSearch.bar); + IncSet_delete(inc); FunctionBar_delete((Object*)defaultBar); Panel_delete((Object*)panel); if (killPanel) diff --git a/htop.h b/htop.h index 8d8ed7ed..7338010e 100644 --- a/htop.h +++ b/htop.h @@ -11,14 +11,10 @@ in the source distribution for its full text. //#link m -#define INCSEARCH_MAX 40 - -#define COPYRIGHT "(C) 2004-2011 Hisham Muhammad" +#define COPYRIGHT "(C) 2004-2012 Hisham Muhammad" typedef bool(*ForeachProcessFn)(Process*, size_t); -typedef struct IncBuffer_; - int main(int argc, char** argv); #endif -- cgit v1.2.3