search and filter for the strace and lsof screens!

This commit is contained in:
Hisham Muhammad 2012-11-10 00:31:37 +00:00
parent 259e1a2938
commit 2a73405cd0
19 changed files with 562 additions and 302 deletions

View File

@ -101,6 +101,12 @@ static HandlerResult CategoriesPanel_eventHandler(Panel* super, int ch) {
result = HANDLED; result = HANDLED;
break; break;
} }
default:
if (isalpha(ch))
result = Panel_selectByTyping(super, ch);
if (result == BREAK_LOOP)
result = IGNORED;
break;
} }
if (result == HANDLED) { if (result == HANDLED) {

213
IncSet.c Normal file
View File

@ -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 <string.h>
#include <stdlib.h>
/*{
#include "FunctionBar.h"
#include "Panel.h"
#include <stdbool.h>
#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);
}

56
IncSet.h Normal file
View File

@ -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 <stdbool.h>
#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

View File

@ -41,10 +41,12 @@ static void ListItem_delete(Object* cast) {
static void ListItem_display(Object* cast, RichString* out) { static void ListItem_display(Object* cast, RichString* out) {
ListItem* this = (ListItem*)cast; ListItem* this = (ListItem*)cast;
assert (this != NULL); assert (this != NULL);
/*
int len = strlen(this->value)+1; int len = strlen(this->value)+1;
char buffer[len+1]; char buffer[len+1];
snprintf(buffer, len, "%s", this->value); 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) { ListItem* ListItem_new(const char* value, int key) {
@ -57,11 +59,13 @@ ListItem* ListItem_new(const char* value, int key) {
return this; return this;
} }
void ListItem_append(ListItem* this, char* text) { void ListItem_append(ListItem* this, const char* text) {
char* buf = malloc(strlen(this->value) + strlen(text) + 1); int oldLen = strlen(this->value);
sprintf(buf, "%s%s", this->value, text); int textLen = strlen(text);
free(this->value); int newLen = strlen(this->value) + textLen;
this->value = buf; this->value = realloc(this->value, newLen + 1);
memcpy(this->value + oldLen, text, textLen);
this->value[newLen] = '\0';
} }
const char* ListItem_getRef(ListItem* this) { const char* ListItem_getRef(ListItem* this) {

View File

@ -26,7 +26,7 @@ extern char* LISTITEM_CLASS;
ListItem* ListItem_new(const char* value, int key); 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); const char* ListItem_getRef(ListItem* this);

View File

@ -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 \ BatteryMeter.c Process.c ProcessList.c RichString.c ScreenManager.c Settings.c \
IOPriorityPanel.c SignalsPanel.c String.c SwapMeter.c TasksMeter.c TraceScreen.c \ IOPriorityPanel.c SignalsPanel.c String.c SwapMeter.c TasksMeter.c TraceScreen.c \
UptimeMeter.c UsersTable.c Vector.c AvailableColumnsPanel.c AffinityPanel.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 \ myhtopheaders = AvailableColumnsPanel.h AvailableMetersPanel.h \
CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \ CategoriesPanel.h CheckItem.h ClockMeter.h ColorsPanel.h ColumnsPanel.h \
IOPriorityPanel.h CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \ IOPriorityPanel.h CPUMeter.h CRT.h DisplayOptionsPanel.h FunctionBar.h \
Hashtable.h Header.h htop.h ListItem.h LoadAverageMeter.h MemoryMeter.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 \ BatteryMeter.h Meter.h MetersPanel.h Object.h Panel.h ProcessList.h RichString.h \
ScreenManager.h Settings.h SignalsPanel.h String.h \ ScreenManager.h Settings.h SignalsPanel.h String.h SwapMeter.h TasksMeter.h \
SwapMeter.h TasksMeter.h TraceScreen.h UptimeMeter.h UsersTable.h Vector.h \ TraceScreen.h UptimeMeter.h UsersTable.h Vector.h Process.h AffinityPanel.h \
Process.h AffinityPanel.h HostnameMeter.h OpenFilesScreen.h Affinity.h IOPriority.h HostnameMeter.h OpenFilesScreen.h Affinity.h IOPriority.h IncSet.h
SUFFIXES = .h SUFFIXES = .h
@ -37,7 +37,7 @@ BUILT_SOURCES = $(myhtopheaders)
htop_SOURCES = $(myhtopheaders) $(myhtopsources) config.h htop_SOURCES = $(myhtopheaders) $(myhtopsources) config.h
profile: profile:
$(MAKE) all CFLAGS="" AM_CPPFLAGS="-pg -O2" $(MAKE) all CFLAGS="-pg" AM_CPPFLAGS="-pg -O2 -DNDEBUG"
debug: debug:
$(MAKE) all CFLAGS="" AM_CPPFLAGS="-ggdb -DDEBUG" $(MAKE) all CFLAGS="" AM_CPPFLAGS="-ggdb -DDEBUG"

View File

@ -10,6 +10,8 @@ in the source distribution for its full text.
#include "CRT.h" #include "CRT.h"
#include "ProcessList.h" #include "ProcessList.h"
#include "ListItem.h" #include "ListItem.h"
#include "IncSet.h"
#include "String.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -42,23 +44,20 @@ typedef struct OpenFilesScreen_ {
pid_t pid; pid_t pid;
Panel* display; Panel* display;
FunctionBar* bar; FunctionBar* bar;
bool tracing;
} OpenFilesScreen; } 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* OpenFilesScreen_new(Process* process) {
OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen)); OpenFilesScreen* this = (OpenFilesScreen*) malloc(sizeof(OpenFilesScreen));
this->process = process; this->process = process;
this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, true, ListItem_compare); this->display = Panel_new(0, 1, COLS, LINES-3, LISTITEM_CLASS, false, ListItem_compare);
this->bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents);
this->tracing = true;
if (Process_isThread(process)) if (Process_isThread(process))
this->pid = process->tgid; this->pid = process->tgid;
else else
@ -68,30 +67,29 @@ OpenFilesScreen* OpenFilesScreen_new(Process* process) {
void OpenFilesScreen_delete(OpenFilesScreen* this) { void OpenFilesScreen_delete(OpenFilesScreen* this) {
Panel_delete((Object*)this->display); Panel_delete((Object*)this->display);
FunctionBar_delete((Object*)this->bar);
free(this); free(this);
} }
static void OpenFilesScreen_draw(OpenFilesScreen* this) { static void OpenFilesScreen_draw(OpenFilesScreen* this, IncSet* inc) {
attrset(CRT_colors[METER_TEXT]); attrset(CRT_colors[METER_TEXT]);
mvhline(0, 0, ' ', COLS); 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]); attrset(CRT_colors[DEFAULT_COLOR]);
Panel_draw(this->display, true); Panel_draw(this->display, true);
FunctionBar_draw(this->bar, NULL); IncSet_drawBar(inc);
} }
static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char command[1025]; char command[1025];
snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid); snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid);
FILE* fd = popen(command, "r"); FILE* fd = popen(command, "r");
OpenFiles_ProcessData* process = calloc(sizeof(OpenFiles_ProcessData), 1); OpenFiles_ProcessData* pdata = calloc(sizeof(OpenFiles_ProcessData), 1);
OpenFiles_FileData* file = NULL; OpenFiles_FileData* fdata = NULL;
OpenFiles_ProcessData* item = process; OpenFiles_ProcessData* item = pdata;
bool anyRead = false; bool anyRead = false;
if (!fd) { if (!fd) {
process->error = 127; pdata->error = 127;
return process; return pdata;
} }
while (!feof(fd)) { while (!feof(fd)) {
int cmd = fgetc(fd); int cmd = fgetc(fd);
@ -107,53 +105,60 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
*newline = '\0'; *newline = '\0';
if (cmd == 'f') { if (cmd == 'f') {
OpenFiles_FileData* nextFile = calloc(sizeof(OpenFiles_ProcessData), 1); OpenFiles_FileData* nextFile = calloc(sizeof(OpenFiles_ProcessData), 1);
if (file == NULL) { if (fdata == NULL) {
process->files = nextFile; pdata->files = nextFile;
} else { } else {
file->next = nextFile; fdata->next = nextFile;
} }
file = nextFile; fdata = nextFile;
item = (OpenFiles_ProcessData*) file; item = (OpenFiles_ProcessData*) fdata;
} }
item->data[cmd] = entry; item->data[cmd] = entry;
} }
process->error = pclose(fd); pdata->error = pclose(fd);
return process; 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; Panel* panel = this->display;
int idx = MAX(Panel_getSelectedIndex(panel), 0); int idx = Panel_getSelectedIndex(panel);
Panel_prune(panel); Panel_prune(panel);
OpenFiles_ProcessData* process = OpenFilesScreen_getProcessData(this->pid); OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(this->pid);
if (process->error == 127) { if (pdata->error == 127) {
Panel_add(panel, (Object*) ListItem_new("Could not execute 'lsof'. Please make sure it is available in your $PATH.", 0)); addLine("Could not execute 'lsof'. Please make sure it is available in your $PATH.", lines, panel, IncSet_filter(inc));
} else if (process->error == 1) { } else if (pdata->error == 1) {
Panel_add(panel, (Object*) ListItem_new("Failed listing open files.", 0)); addLine("Failed listing open files.", lines, panel, IncSet_filter(inc));
} else { } else {
OpenFiles_FileData* file = process->files; OpenFiles_FileData* fdata = pdata->files;
while (file) { while (fdata) {
char entry[1024]; char entry[1024];
sprintf(entry, "%5s %4s %10s %10s %10s %s", sprintf(entry, "%5s %4s %10s %10s %10s %s",
file->data['f'] ? file->data['f'] : "", fdata->data['f'] ? fdata->data['f'] : "",
file->data['t'] ? file->data['t'] : "", fdata->data['t'] ? fdata->data['t'] : "",
file->data['D'] ? file->data['D'] : "", fdata->data['D'] ? fdata->data['D'] : "",
file->data['s'] ? file->data['s'] : "", fdata->data['s'] ? fdata->data['s'] : "",
file->data['i'] ? file->data['i'] : "", fdata->data['i'] ? fdata->data['i'] : "",
file->data['n'] ? file->data['n'] : ""); fdata->data['n'] ? fdata->data['n'] : "");
Panel_add(panel, (Object*) ListItem_new(entry, 0)); addLine(entry, lines, panel, IncSet_filter(inc));
for (int i = 0; i < 255; i++) for (int i = 0; i < 255; i++)
if (file->data[i]) if (fdata->data[i])
free(file->data[i]); free(fdata->data[i]);
OpenFiles_FileData* old = file; OpenFiles_FileData* old = fdata;
file = file->next; fdata = fdata->next;
free(old); free(old);
} }
for (int i = 0; i < 255; i++) for (int i = 0; i < 255; i++)
if (process->data[i]) if (pdata->data[i])
free(process->data[i]); free(pdata->data[i]);
} }
free(process); free(pdata);
Vector_insertionSort(lines);
Vector_insertionSort(panel->items); Vector_insertionSort(panel->items);
Panel_setSelected(panel, idx); Panel_setSelected(panel, idx);
} }
@ -161,14 +166,24 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this) {
void OpenFilesScreen_run(OpenFilesScreen* this) { void OpenFilesScreen_run(OpenFilesScreen* this) {
Panel* panel = this->display; Panel* panel = this->display;
Panel_setHeader(panel, " FD TYPE DEVICE SIZE NODE NAME"); Panel_setHeader(panel, " FD TYPE DEVICE SIZE NODE NAME");
OpenFilesScreen_scan(this);
OpenFilesScreen_draw(this); FunctionBar* bar = FunctionBar_new(ofsFunctions, ofsKeys, ofsEvents);
//CRT_disableDelay(); 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; bool looping = true;
while (looping) { while (looping) {
Panel_draw(panel, true); Panel_draw(panel, true);
if (inc->active)
move(LINES-1, CRT_cursorX);
int ch = getch(); int ch = getch();
if (ch == KEY_MOUSE) { if (ch == KEY_MOUSE) {
MEVENT mevent; MEVENT mevent;
int ok = getmouse(&mevent); int ok = getmouse(&mevent);
@ -177,19 +192,33 @@ void OpenFilesScreen_run(OpenFilesScreen* this) {
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV); Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
ch = 0; ch = 0;
} if (mevent.y == LINES - 1) } 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) { switch(ch) {
case ERR: case ERR:
continue; 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): case KEY_F(5):
clear(); clear();
OpenFilesScreen_scan(this); OpenFilesScreen_scan(this, lines, inc);
OpenFilesScreen_draw(this); OpenFilesScreen_draw(this, inc);
break; break;
case '\014': // Ctrl+L case '\014': // Ctrl+L
clear(); clear();
OpenFilesScreen_draw(this); OpenFilesScreen_draw(this, inc);
break; break;
case 'q': case 'q':
case 27: case 27:
@ -198,11 +227,13 @@ void OpenFilesScreen_run(OpenFilesScreen* this) {
break; break;
case KEY_RESIZE: case KEY_RESIZE:
Panel_resize(panel, COLS, LINES-2); Panel_resize(panel, COLS, LINES-2);
OpenFilesScreen_draw(this); OpenFilesScreen_draw(this, inc);
break; break;
default: default:
Panel_onKey(panel, ch); Panel_onKey(panel, ch);
} }
} }
//CRT_enableDelay();
FunctionBar_delete((Object*)bar);
IncSet_delete(inc);
} }

View File

@ -29,7 +29,6 @@ typedef struct OpenFilesScreen_ {
pid_t pid; pid_t pid;
Panel* display; Panel* display;
FunctionBar* bar; FunctionBar* bar;
bool tracing;
} OpenFilesScreen; } OpenFilesScreen;

View File

@ -112,7 +112,6 @@ typedef struct ProcessList_ {
int following; int following;
bool userOnly; bool userOnly;
uid_t userId; uid_t userId;
bool filtering;
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList; 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) { if (!flags) {
following = this->following; following = this->following;
userOnly = this->userOnly; userOnly = this->userOnly;
userId = this->userId; userId = this->userId;
filtering = this->filtering;
incFilter = this->incFilter; incFilter = this->incFilter;
} else { } else {
this->following = following; this->following = following;
this->userOnly = userOnly; this->userOnly = userOnly;
this->userId = userId; this->userId = userId;
this->filtering = filtering;
this->incFilter = incFilter; this->incFilter = incFilter;
} }
@ -946,7 +943,7 @@ void ProcessList_rebuildPanel(ProcessList* this, bool flags, int following, bool
if ( (!p->show) if ( (!p->show)
|| (userOnly && (p->st_uid != userId)) || (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)) ) || (this->pidWhiteList && !Hashtable_get(this->pidWhiteList, p->pid)) )
hidden = true; hidden = true;

View File

@ -95,7 +95,6 @@ typedef struct ProcessList_ {
int following; int following;
bool userOnly; bool userOnly;
uid_t userId; uid_t userId;
bool filtering;
const char* incFilter; const char* incFilter;
Hashtable* pidWhiteList; Hashtable* pidWhiteList;
@ -185,6 +184,6 @@ ProcessField ProcessList_keyAt(ProcessList* this, int at);
void ProcessList_expandTree(ProcessList* this); 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 #endif

View File

@ -83,16 +83,15 @@ static inline void RichString_setLen(RichString* this, int len) {
#ifdef HAVE_LIBNCURSESW #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]; wchar_t data[len+1];
len = mbstowcs(data, data_c, len); len = mbstowcs(data, data_c, len);
if (len<0) if (len<0)
return; return;
int oldLen = this->chlen; int newLen = from + len;
int newLen = len + oldLen;
RichString_setLen(this, newLen); RichString_setLen(this, newLen);
for (int i = oldLen, j = 0; i < newLen; i++, j++) { memset(&this->chptr[from], 0, sizeof(CharType) * (newLen - from));
memset(&this->chptr[i], 0, sizeof(this->chptr[i])); for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i].chars[0] = data[j]; this->chptr[i].chars[0] = data[j];
this->chptr[i].attr = attrs; this->chptr[i].attr = attrs;
} }
@ -120,11 +119,10 @@ int RichString_findChar(RichString* this, char c, int start) {
#else #else
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) {
int oldLen = this->chlen; int newLen = from + len;
int newLen = len + oldLen;
RichString_setLen(this, newLen); 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[i] = (isprint(data_c[j]) ? data_c[j] : '?') | attrs;
this->chptr[newLen] = 0; this->chptr[newLen] = 0;
} }
@ -160,11 +158,14 @@ void RichString_setAttr(RichString* this, int attrs) {
RichString_setAttrn(this, attrs, 0, this->chlen - 1); RichString_setAttrn(this, attrs, 0, this->chlen - 1);
} }
inline void RichString_append(RichString* this, int attrs, const char* data) { void RichString_append(RichString* this, int attrs, const char* data) {
RichString_appendn(this, attrs, data, strlen(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) { void RichString_write(RichString* this, int attrs, const char* data) {
RichString_setLen(this, 0); RichString_writeFrom(this, attrs, data, 0, strlen(data));
RichString_appendn(this, attrs, data, strlen(data));
} }

View File

@ -61,16 +61,12 @@ typedef struct RichString_ {
#ifdef HAVE_LIBNCURSESW #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); extern void RichString_setAttrn(RichString* this, int attrs, int start, int finish);
int RichString_findChar(RichString* this, char c, int start); int RichString_findChar(RichString* this, char c, int start);
#else #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); void RichString_setAttrn(RichString* this, int attrs, int start, int finish);
int RichString_findChar(RichString* this, char c, int start); 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); 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); void RichString_write(RichString* this, int attrs, const char* data);

View File

@ -144,7 +144,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
this->lastScan = now; this->lastScan = now;
} }
Header_draw(this->header); 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++) { for (int i = 0; i < panels; i++) {
Panel* panel = (Panel*) Vector_get(this->panels, i); Panel* panel = (Panel*) Vector_get(this->panels, i);

View File

@ -10,6 +10,8 @@ in the source distribution for its full text.
#include "CRT.h" #include "CRT.h"
#include "ProcessList.h" #include "ProcessList.h"
#include "ListItem.h" #include "ListItem.h"
#include "IncSet.h"
#include "String.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -30,39 +32,49 @@ in the source distribution for its full text.
typedef struct TraceScreen_ { typedef struct TraceScreen_ {
Process* process; Process* process;
Panel* display; Panel* display;
FunctionBar* bar;
bool tracing; bool tracing;
} TraceScreen; } 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* TraceScreen_new(Process* process) {
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen)); TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
this->process = process; this->process = process;
this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare); this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, false, ListItem_compare);
this->bar = FunctionBar_new(tsFunctions, tsKeys, tsEvents);
this->tracing = true; this->tracing = true;
return this; return this;
} }
void TraceScreen_delete(TraceScreen* this) { void TraceScreen_delete(TraceScreen* this) {
Panel_delete((Object*)this->display); Panel_delete((Object*)this->display);
FunctionBar_delete((Object*)this->bar);
free(this); free(this);
} }
static void TraceScreen_draw(TraceScreen* this) { static void TraceScreen_draw(TraceScreen* this, IncSet* inc) {
attrset(CRT_colors[PANEL_HEADER_FOCUS]); attrset(CRT_colors[PANEL_HEADER_FOCUS]);
mvhline(0, 0, ' ', COLS); mvhline(0, 0, ' ', COLS);
mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm); mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm);
attrset(CRT_colors[DEFAULT_COLOR]); 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) { void TraceScreen_run(TraceScreen* this) {
@ -86,47 +98,63 @@ void TraceScreen_run(TraceScreen* this) {
FILE* strace = fdopen(fdpair[0], "r"); FILE* strace = fdopen(fdpair[0], "r");
Panel* panel = this->display; Panel* panel = this->display;
int fd_strace = fileno(strace); int fd_strace = fileno(strace);
TraceScreen_draw(this);
CRT_disableDelay(); CRT_disableDelay();
bool contLine = false; bool contLine = false;
bool follow = false; bool follow = false;
bool looping = true; 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) { while (looping) {
fd_set fds;
FD_ZERO(&fds); Panel_draw(panel, true);
FD_SET(fd_strace, &fds); const char* incFilter = IncSet_filter(inc);
struct timeval tv;
tv.tv_sec = 0; tv.tv_usec = 500; if (inc->active)
int ready = select(fd_strace+1, &fds, NULL, NULL, &tv); move(LINES-1, CRT_cursorX);
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));
}
line = buffer+i+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(); 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;
}
}
if (line < buffer+nread) {
addLine(line, lines, panel, incFilter);
buffer[nread] = '\0';
contLine = true;
}
if (follow)
Panel_setSelected(panel, Panel_size(panel)-1);
}
}
if (ch == KEY_MOUSE) { if (ch == KEY_MOUSE) {
MEVENT mevent; MEVENT mevent;
int ok = getmouse(&mevent); int ok = getmouse(&mevent);
@ -136,28 +164,43 @@ void TraceScreen_run(TraceScreen* this) {
follow = false; follow = false;
ch = 0; ch = 0;
} if (mevent.y == LINES - 1) } 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) { switch(ch) {
case ERR: case ERR:
continue; 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: case KEY_HOME:
Panel_setSelected(panel, 0); Panel_setSelected(panel, 0);
break; break;
case KEY_END: case KEY_END:
Panel_setSelected(panel, Panel_size(panel)-1); Panel_setSelected(panel, Panel_size(panel)-1);
break; break;
case 'f': case KEY_F(3):
case '/':
IncSet_activate(inc, INC_SEARCH);
break;
case KEY_F(4): case KEY_F(4):
case '\\':
IncSet_activate(inc, INC_FILTER);
break;
case 'f':
case KEY_F(8):
follow = !follow; follow = !follow;
if (follow) if (follow)
Panel_setSelected(panel, Panel_size(panel)-1); Panel_setSelected(panel, Panel_size(panel)-1);
break; 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 'q':
case 27: case 27:
case KEY_F(10): case KEY_F(10):
@ -165,14 +208,17 @@ void TraceScreen_run(TraceScreen* this) {
break; break;
case KEY_RESIZE: case KEY_RESIZE:
Panel_resize(panel, COLS, LINES-2); Panel_resize(panel, COLS, LINES-2);
TraceScreen_draw(this); TraceScreen_draw(this, inc);
break; break;
default: default:
follow = false; follow = false;
Panel_onKey(panel, ch); Panel_onKey(panel, ch);
} }
Panel_draw(panel, true);
} }
IncSet_delete(inc);
FunctionBar_delete((Object*)bar);
kill(child, SIGTERM); kill(child, SIGTERM);
waitpid(child, NULL, 0); waitpid(child, NULL, 0);
fclose(strace); fclose(strace);

View File

@ -16,7 +16,6 @@ in the source distribution for its full text.
typedef struct TraceScreen_ { typedef struct TraceScreen_ {
Process* process; Process* process;
Panel* display; Panel* display;
FunctionBar* bar;
bool tracing; bool tracing;
} TraceScreen; } TraceScreen;

View File

@ -27,13 +27,13 @@ typedef struct Vector_ {
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
char* vectorType; char* type;
bool owner; bool owner;
} Vector; } 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; Vector* this;
if (size == DEFAULT_SIZE) 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->array = (Object**) calloc(size, sizeof(Object*));
this->arraySize = size; this->arraySize = size;
this->items = 0; this->items = 0;
this->vectorType = vectorType_; this->type = type;
this->owner = owner; this->owner = owner;
this->compare = compare; this->compare = compare;
return this; return this;
@ -65,7 +65,7 @@ static inline bool Vector_isConsistent(Vector* this) {
assert(this->items <= this->arraySize); assert(this->items <= this->arraySize);
if (this->owner) { if (this->owner) {
for (int i = 0; i < this->items; i++) 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 false;
return true; return true;
} else { } else {
@ -89,12 +89,13 @@ void Vector_prune(Vector* this) {
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i; int i;
for (i = 0; i < this->items; i++) if (this->owner) {
if (this->array[i]) { for (i = 0; i < this->items; i++)
if (this->owner) if (this->array[i]) {
(this->array[i])->delete(this->array[i]); (this->array[i])->delete(this->array[i]);
this->array[i] = NULL; //this->array[i] = NULL;
} }
}
this->items = 0; this->items = 0;
} }
@ -181,24 +182,26 @@ void Vector_insertionSort(Vector* this) {
static void Vector_checkArraySize(Vector* this) { static void Vector_checkArraySize(Vector* this) {
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
if (this->items >= this->arraySize) { if (this->items >= this->arraySize) {
int i; //int i;
i = this->arraySize; //i = this->arraySize;
this->arraySize = this->items + this->growthRate; this->arraySize = this->items + this->growthRate;
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize); this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize);
for (; i < this->arraySize; i++) //for (; i < this->arraySize; i++)
this->array[i] = NULL; // this->array[i] = NULL;
} }
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }
void Vector_insert(Vector* this, int idx, void* data_) { void Vector_insert(Vector* this, int idx, void* data_) {
assert(idx >= 0);
assert(((Object*)data_)->class == this->vectorType);
Object* data = data_; Object* data = data_;
assert(idx >= 0);
assert(idx <= this->items);
assert(data_);
assert(data->class == this->type);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Vector_checkArraySize(this); Vector_checkArraySize(this);
assert(this->array[this->items] == NULL); //assert(this->array[this->items] == NULL);
for (int i = this->items; i > idx; i--) { for (int i = this->items; i > idx; i--) {
this->array[i] = this->array[i-1]; 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(idx >= 0 && idx < this->items);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
Object* removed = this->array[idx]; Object* removed = this->array[idx];
assert (removed != NULL); //assert (removed != NULL);
this->items--; this->items--;
for (int i = idx; i < this->items; i++) for (int i = idx; i < this->items; i++)
this->array[i] = this->array[i+1]; this->array[i] = this->array[i+1];
this->array[this->items] = NULL; //this->array[this->items] = NULL;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
return removed; return removed;
} }
@ -251,7 +254,7 @@ void Vector_moveDown(Vector* this, int idx) {
void Vector_set(Vector* this, int idx, void* data_) { void Vector_set(Vector* this, int idx, void* data_) {
assert(idx >= 0); assert(idx >= 0);
assert(((Object*)data_)->class == this->vectorType); assert(((Object*)data_)->class == this->type);
Object* data = data_; Object* data = data_;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
@ -306,7 +309,7 @@ static void Vector_merge(Vector* this, Vector* v2) {
*/ */
void Vector_add(Vector* this, void* data_) { void Vector_add(Vector* this, void* data_) {
assert(data_ && ((Object*)data_)->class == this->vectorType); assert(data_ && ((Object*)data_)->class == this->type);
Object* data = data_; Object* data = data_;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i = this->items; 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) { 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); assert(this->compare);
Object* search = search_; Object* search = search_;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
for (int i = 0; i < this->items; i++) { for (int i = 0; i < this->items; i++) {
Object* o = (Object*)this->array[i]; Object* o = (Object*)this->array[i];
if (o && compare(search, o) == 0) assert(o);
if (compare(search, o) == 0)
return i; return i;
} }
return -1; return -1;

View File

@ -23,12 +23,12 @@ typedef struct Vector_ {
int arraySize; int arraySize;
int growthRate; int growthRate;
int items; int items;
char* vectorType; char* type;
bool owner; bool owner;
} Vector; } 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); void Vector_delete(Vector* this);

161
htop.c
View File

@ -23,6 +23,7 @@ in the source distribution for its full text.
#include "OpenFilesScreen.h" #include "OpenFilesScreen.h"
#include "AffinityPanel.h" #include "AffinityPanel.h"
#include "IOPriorityPanel.h" #include "IOPriorityPanel.h"
#include "IncSet.h"
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
@ -38,9 +39,7 @@ in the source distribution for its full text.
//#link m //#link m
#define INCSEARCH_MAX 40 #define COPYRIGHT "(C) 2004-2012 Hisham Muhammad"
#define COPYRIGHT "(C) 2004-2011 Hisham Muhammad"
static void printVersionFlag() { static void printVersionFlag() {
fputs("htop " VERSION " - " COPYRIGHT "\n" 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)); ProcessList_printHeader(pl, Panel_getHeader(panel));
} }
typedef struct IncBuffer_ { static const char* getMainPanelValue(Panel* panel, int i) {
char buffer[INCSEARCH_MAX]; Process* p = (Process*) Panel_get(panel, i);
int index; if (p)
FunctionBar* bar; return p->comm;
} IncBuffer; return "";
static void IncBuffer_reset(IncBuffer* inc) {
inc->index = 0;
inc->buffer[0] = 0;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -440,27 +435,12 @@ int main(int argc, char** argv) {
} }
ProcessList_printHeader(pl, Panel_getHeader(panel)); 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 ", const char* defaultFunctions[] = {"Help ", "Setup ", "Search", "Filter", "Tree ",
"SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL}; "SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL};
FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL); FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL);
IncSet* inc = IncSet_new(defaultBar);
ProcessList_scan(pl); ProcessList_scan(pl);
usleep(75000); usleep(75000);
@ -494,7 +474,7 @@ int main(int argc, char** argv) {
ProcessList_sort(pl); ProcessList_sort(pl);
refreshTimeout = 1; refreshTimeout = 1;
} }
ProcessList_rebuildPanel(pl, true, following, userOnly, userId, filtering, incFilter.buffer); ProcessList_rebuildPanel(pl, true, following, userOnly, userId, IncSet_filter(inc));
} }
doRefresh = true; doRefresh = true;
@ -502,12 +482,12 @@ int main(int argc, char** argv) {
Panel_draw(panel, true); Panel_draw(panel, true);
int prev = ch; int prev = ch;
if (incMode) if (inc->active)
move(LINES-1, CRT_cursorX); move(LINES-1, CRT_cursorX);
ch = getch(); ch = getch();
if (ch == ERR) { if (ch == ERR) {
if (!incMode) if (!inc->active)
refreshTimeout--; refreshTimeout--;
if (prev == ch && !recalculate) { if (prev == ch && !recalculate) {
closeTimeout++; closeTimeout++;
@ -519,83 +499,6 @@ int main(int argc, char** argv) {
continue; 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) { if (ch == KEY_MOUSE) {
MEVENT mevent; MEVENT mevent;
int ok = getmouse(&mevent); int ok = getmouse(&mevent);
@ -619,10 +522,7 @@ int main(int argc, char** argv) {
follow = true; follow = true;
continue; continue;
} if (mevent.y == LINES - 1) { } if (mevent.y == LINES - 1) {
FunctionBar* bar; ch = FunctionBar_synthesizeEvent(inc->bar, mevent.x);
if (incMode) bar = incMode->bar;
else bar = defaultBar;
ch = FunctionBar_synthesizeEvent(bar, mevent.x);
} }
} else if (mevent.bstate & BUTTON4_CLICKED) { } else if (mevent.bstate & BUTTON4_CLICKED) {
ch = KEY_UP; 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) { switch (ch) {
case KEY_RESIZE: case KEY_RESIZE:
Panel_resize(panel, COLS, LINES-headerHeight-1); Panel_resize(panel, COLS, LINES-headerHeight-1);
if (incMode) IncSet_drawBar(inc);
FunctionBar_draw(incMode->bar, incMode->buffer);
else
FunctionBar_draw(defaultBar, NULL);
break; break;
case 'M': case 'M':
{ {
@ -834,7 +749,7 @@ int main(int argc, char** argv) {
const char* fuFunctions[] = {"Sort ", "Cancel ", NULL}; const char* fuFunctions[] = {"Sort ", "Cancel ", NULL};
ProcessField* fields = pl->fields; ProcessField* fields = pl->fields;
for (int i = 0; fields[i]; i++) { 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])); Panel_add(sortPanel, (Object*) ListItem_new(name, fields[i]));
if (fields[i] == pl->sortKey) if (fields[i] == pl->sortKey)
Panel_setSelected(sortPanel, i); Panel_setSelected(sortPanel, i);
@ -891,16 +806,13 @@ int main(int argc, char** argv) {
} }
case KEY_F(3): case KEY_F(3):
case '/': case '/':
incMode = &incSearch; IncSet_activate(inc, INC_SEARCH);
IncBuffer_reset(incMode);
FunctionBar_draw(incSearch.bar, incSearch.buffer);
break; break;
case KEY_F(4): case KEY_F(4):
case '\\': case '\\':
incMode = &incFilter; IncSet_activate(inc, INC_FILTER);
refreshTimeout = 0; refreshTimeout = 0;
doRefresh = true; doRefresh = true;
FunctionBar_draw(incFilter.bar, incFilter.buffer);
continue; continue;
case 't': case 't':
case KEY_F(5): case KEY_F(5):
@ -943,8 +855,7 @@ int main(int argc, char** argv) {
Settings_write(settings); Settings_write(settings);
Header_delete(header); Header_delete(header);
ProcessList_delete(pl); ProcessList_delete(pl);
FunctionBar_delete((Object*)incFilter.bar); IncSet_delete(inc);
FunctionBar_delete((Object*)incSearch.bar);
FunctionBar_delete((Object*)defaultBar); FunctionBar_delete((Object*)defaultBar);
Panel_delete((Object*)panel); Panel_delete((Object*)panel);
if (killPanel) if (killPanel)

6
htop.h
View File

@ -11,14 +11,10 @@ in the source distribution for its full text.
//#link m //#link m
#define INCSEARCH_MAX 40 #define COPYRIGHT "(C) 2004-2012 Hisham Muhammad"
#define COPYRIGHT "(C) 2004-2011 Hisham Muhammad"
typedef bool(*ForeachProcessFn)(Process*, size_t); typedef bool(*ForeachProcessFn)(Process*, size_t);
typedef struct IncBuffer_;
int main(int argc, char** argv); int main(int argc, char** argv);
#endif #endif