incremental search and filter reintegrated!

This commit is contained in:
Hisham Muhammad 2015-03-23 17:04:53 -03:00
parent d0c72c3fb2
commit ade7993fcb
10 changed files with 63 additions and 43 deletions

View File

@ -224,13 +224,13 @@ static Htop_Reaction actionToggleTreeView(State* st) {
static Htop_Reaction actionIncFilter(State* st) { static Htop_Reaction actionIncFilter(State* st) {
IncSet* inc = ((MainPanel*)st->panel)->inc; IncSet* inc = ((MainPanel*)st->panel)->inc;
IncSet_activate(inc, INC_FILTER); IncSet_activate(inc, INC_FILTER, st->panel);
st->pl->incFilter = IncSet_filter(inc); st->pl->incFilter = IncSet_filter(inc);
return HTOP_REFRESH | HTOP_KEEP_FOLLOWING; return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
} }
static Htop_Reaction actionIncSearch(State* st) { static Htop_Reaction actionIncSearch(State* st) {
IncSet_activate(((MainPanel*)st->panel)->inc, INC_SEARCH); IncSet_activate(((MainPanel*)st->panel)->inc, INC_SEARCH, st->panel);
return HTOP_REFRESH | HTOP_KEEP_FOLLOWING; return HTOP_REFRESH | HTOP_KEEP_FOLLOWING;
} }

View File

@ -38,7 +38,6 @@ typedef struct IncMode_ {
typedef struct IncSet_ { typedef struct IncSet_ {
IncMode modes[2]; IncMode modes[2];
IncMode* active; IncMode* active;
FunctionBar* bar;
FunctionBar* defaultBar; FunctionBar* defaultBar;
bool filtering; bool filtering;
} IncSet; } IncSet;
@ -82,7 +81,6 @@ IncSet* IncSet_new(FunctionBar* bar) {
IncMode_initFilter(&(this->modes[INC_FILTER])); IncMode_initFilter(&(this->modes[INC_FILTER]));
this->active = NULL; this->active = NULL;
this->filtering = false; this->filtering = false;
this->bar = bar;
this->defaultBar = bar; this->defaultBar = bar;
return this; return this;
} }
@ -137,7 +135,7 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue
return true; return true;
IncMode* mode = this->active; IncMode* mode = this->active;
int size = Panel_size(panel); int size = Panel_size(panel);
bool filterChange = false; bool filterChanged = false;
bool doSearch = true; bool doSearch = true;
if (ch == KEY_F(3)) { if (ch == KEY_F(3)) {
if (size == 0) return true; if (size == 0) return true;
@ -158,14 +156,14 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue
mode->index++; mode->index++;
mode->buffer[mode->index] = 0; mode->buffer[mode->index] = 0;
if (mode->isFilter) { if (mode->isFilter) {
filterChange = true; filterChanged = true;
if (mode->index == 1) this->filtering = true; if (mode->index == 1) this->filtering = true;
} }
} else if ((ch == KEY_BACKSPACE || ch == 127) && (mode->index > 0)) { } else if ((ch == KEY_BACKSPACE || ch == 127) && (mode->index > 0)) {
mode->index--; mode->index--;
mode->buffer[mode->index] = 0; mode->buffer[mode->index] = 0;
if (mode->isFilter) { if (mode->isFilter) {
filterChange = true; filterChanged = true;
if (mode->index == 0) { if (mode->index == 0) {
this->filtering = false; this->filtering = false;
IncMode_reset(mode); IncMode_reset(mode);
@ -173,7 +171,7 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue
} }
} else { } else {
if (mode->isFilter) { if (mode->isFilter) {
filterChange = true; filterChanged = true;
if (ch == 27) { if (ch == 27) {
this->filtering = false; this->filtering = false;
IncMode_reset(mode); IncMode_reset(mode);
@ -182,17 +180,17 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue
IncMode_reset(mode); IncMode_reset(mode);
} }
this->active = NULL; this->active = NULL;
this->bar = this->defaultBar; Panel_setDefaultBar(panel);
FunctionBar_draw(this->defaultBar, NULL); FunctionBar_draw(this->defaultBar, NULL);
doSearch = false; doSearch = false;
} }
if (doSearch) { if (doSearch) {
search(mode, panel, getPanelValue); search(mode, panel, getPanelValue);
} }
if (filterChange && lines) { if (filterChanged && lines) {
updateWeakPanel(this, panel, lines); updateWeakPanel(this, panel, lines);
} }
return filterChange; return filterChanged;
} }
const char* IncSet_getListItemValue(Panel* panel, int i) { const char* IncSet_getListItemValue(Panel* panel, int i) {
@ -202,13 +200,24 @@ const char* IncSet_getListItemValue(Panel* panel, int i) {
return ""; return "";
} }
void IncSet_activate(IncSet* this, IncType type, FunctionBar** setBar) { void IncSet_activate(IncSet* this, IncType type, Panel* panel) {
this->active = &(this->modes[type]); this->active = &(this->modes[type]);
this->bar = this->active->bar;
FunctionBar_draw(this->active->bar, this->active->buffer); FunctionBar_draw(this->active->bar, this->active->buffer);
setBar = panel->currentBar = this->active->bar;
} }
void IncSet_drawBar(IncSet* this) { void IncSet_drawBar(IncSet* this) {
FunctionBar_draw(this->bar, this->active ? this->active->buffer : NULL); if (this->active) {
FunctionBar_draw(this->active->bar, this->active->buffer);
} else {
FunctionBar_draw(this->defaultBar, NULL);
}
}
int IncSet_synthesizeEvent(IncSet* this, int x) {
if (this->active) {
return FunctionBar_synthesizeEvent(this->active->bar, x);
} else {
return FunctionBar_synthesizeEvent(this->defaultBar, x);
}
} }

View File

@ -33,7 +33,6 @@ typedef struct IncMode_ {
typedef struct IncSet_ { typedef struct IncSet_ {
IncMode modes[2]; IncMode modes[2];
IncMode* active; IncMode* active;
FunctionBar* bar;
FunctionBar* defaultBar; FunctionBar* defaultBar;
bool filtering; bool filtering;
} IncSet; } IncSet;
@ -49,8 +48,10 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue
const char* IncSet_getListItemValue(Panel* panel, int i); const char* IncSet_getListItemValue(Panel* panel, int i);
void IncSet_activate(IncSet* this, IncType type); void IncSet_activate(IncSet* this, IncType type, Panel* panel);
void IncSet_drawBar(IncSet* this); void IncSet_drawBar(IncSet* this);
int IncSet_synthesizeEvent(IncSet* this, int x);
#endif #endif

View File

@ -21,24 +21,26 @@ typedef struct MainPanel_ {
Panel super; Panel super;
State* state; State* state;
IncSet* inc; IncSet* inc;
FunctionBar* fuBar;
Htop_Action *keys; Htop_Action *keys;
pid_t pidSearch; pid_t pidSearch;
} MainPanel; } MainPanel;
typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t); typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t);
#define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar)
}*/ }*/
static const char* MainFunctions[] = {"Help ", "Setup ", "Search", "Filter", "Tree ", "SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL}; static const char* MainFunctions[] = {"Help ", "Setup ", "Search", "Filter", "Tree ", "SortBy", "Nice -", "Nice +", "Kill ", "Quit ", NULL};
void MainPanel_updateTreeFunctions(MainPanel* this, bool mode) { void MainPanel_updateTreeFunctions(MainPanel* this, bool mode) {
FunctionBar* bar = MainPanel_getFunctionBar(this);
if (mode) { if (mode) {
FunctionBar_setLabel(this->fuBar, KEY_F(5), "Sorted"); FunctionBar_setLabel(bar, KEY_F(5), "Sorted");
FunctionBar_setLabel(this->fuBar, KEY_F(6), "Collap"); FunctionBar_setLabel(bar, KEY_F(6), "Collap");
} else { } else {
FunctionBar_setLabel(this->fuBar, KEY_F(5), "Tree "); FunctionBar_setLabel(bar, KEY_F(5), "Tree ");
FunctionBar_setLabel(this->fuBar, KEY_F(6), "SortBy"); FunctionBar_setLabel(bar, KEY_F(6), "SortBy");
} }
} }
@ -65,15 +67,16 @@ static HandlerResult MainPanel_eventHandler(Panel* super, int ch) {
Htop_Reaction reaction = HTOP_OK; Htop_Reaction reaction = HTOP_OK;
if (ch == 27) {
return HANDLED;
}
if (ch != ERR && this->inc->active) { if (ch != ERR && this->inc->active) {
bool redraw = IncSet_handleKey(this->inc, ch, super, (IncMode_GetPanelValue) MainPanel_getValue, NULL); bool filterChanged = IncSet_handleKey(this->inc, ch, super, (IncMode_GetPanelValue) MainPanel_getValue, NULL);
if (redraw) { if (filterChanged) {
this->state->pl->incFilter = IncSet_filter(this->inc);
reaction = HTOP_REFRESH | HTOP_REDRAW_BAR; reaction = HTOP_REFRESH | HTOP_REDRAW_BAR;
} }
reaction |= HTOP_KEEP_FOLLOWING; reaction |= HTOP_KEEP_FOLLOWING;
result = HANDLED;
} else if (ch == 27) {
return HANDLED;
} else if (ch != ERR && this->keys[ch]) { } else if (ch != ERR && this->keys[ch]) {
reaction |= (this->keys[ch])(this->state); reaction |= (this->keys[ch])(this->state);
result = HANDLED; result = HANDLED;
@ -169,10 +172,9 @@ PanelClass MainPanel_class = {
MainPanel* MainPanel_new() { MainPanel* MainPanel_new() {
MainPanel* this = AllocThis(MainPanel); MainPanel* this = AllocThis(MainPanel);
this->fuBar = FunctionBar_new(MainFunctions, NULL, NULL); Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, FunctionBar_new(MainFunctions, NULL, NULL));
Panel_init((Panel*) this, 1, 1, 1, 1, Class(Process), false, this->fuBar);
this->keys = calloc(KEY_MAX, sizeof(Htop_Action)); this->keys = calloc(KEY_MAX, sizeof(Htop_Action));
this->inc = IncSet_new(this->fuBar); this->inc = IncSet_new(MainPanel_getFunctionBar(this));
Action_setBindings(this->keys); Action_setBindings(this->keys);
Platform_setBindings(this->keys); Platform_setBindings(this->keys);

View File

@ -17,13 +17,14 @@ typedef struct MainPanel_ {
Panel super; Panel super;
State* state; State* state;
IncSet* inc; IncSet* inc;
FunctionBar* fuBar;
Htop_Action *keys; Htop_Action *keys;
pid_t pidSearch; pid_t pidSearch;
} MainPanel; } MainPanel;
typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t); typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t);
#define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar)
void MainPanel_updateTreeFunctions(MainPanel* this, bool mode); void MainPanel_updateTreeFunctions(MainPanel* this, bool mode);

View File

@ -198,7 +198,7 @@ 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(inc->bar, mevent.x); ch = IncSet_synthesizeEvent(inc, mevent.x);
} }
if (inc->active) { if (inc->active) {
@ -211,11 +211,11 @@ void OpenFilesScreen_run(OpenFilesScreen* this) {
continue; continue;
case KEY_F(3): case KEY_F(3):
case '/': case '/':
IncSet_activate(inc, INC_SEARCH); IncSet_activate(inc, INC_SEARCH, panel);
break; break;
case KEY_F(4): case KEY_F(4):
case '\\': case '\\':
IncSet_activate(inc, INC_FILTER); IncSet_activate(inc, INC_FILTER, panel);
break; break;
case KEY_F(5): case KEY_F(5):
clear(); clear();

View File

@ -67,6 +67,8 @@ struct Panel_ {
RichString header; RichString header;
}; };
#define Panel_setDefaultBar(this_) do{ (this_)->currentBar = (this_)->defaultBar; }while(0)
}*/ }*/
#ifndef MIN #ifndef MIN

View File

@ -56,6 +56,8 @@ struct Panel_ {
RichString header; RichString header;
}; };
#define Panel_setDefaultBar(this_) do{ (this_)->currentBar = (this_)->defaultBar; }while(0)
#ifndef MIN #ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b))

View File

@ -156,11 +156,16 @@ static void ScreenManager_drawPanels(ScreenManager* this, int focus) {
} }
} }
static Panel* setCurrentPanel(Panel* panel) {
FunctionBar_draw(panel->currentBar, NULL);
return panel;
}
void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) { void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
bool quit = false; bool quit = false;
int focus = 0; int focus = 0;
Panel* panelFocus = (Panel*) Vector_get(this->panels, focus); Panel* panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus));
double oldTime = 0.0; double oldTime = 0.0;
@ -180,7 +185,6 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
if (redraw) { if (redraw) {
ScreenManager_drawPanels(this, focus); ScreenManager_drawPanels(this, focus);
FunctionBar_draw(panelFocus->currentBar, NULL);
} }
int prevCh = ch; int prevCh = ch;
@ -199,7 +203,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
mevent.y > panel->y && mevent.y <= panel->y+panel->h && mevent.y > panel->y && mevent.y <= panel->y+panel->h &&
(this->allowFocusChange || panelFocus == panel) ) { (this->allowFocusChange || panelFocus == panel) ) {
focus = i; focus = i;
panelFocus = panel; panelFocus = setCurrentPanel(panel);
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
break; break;
} }
@ -255,7 +259,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
tryLeft: tryLeft:
if (focus > 0) if (focus > 0)
focus--; focus--;
panelFocus = (Panel*) Vector_get(this->panels, focus); panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus));
if (Panel_size(panelFocus) == 0 && focus > 0) if (Panel_size(panelFocus) == 0 && focus > 0)
goto tryLeft; goto tryLeft;
break; break;
@ -267,7 +271,7 @@ void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey) {
tryRight: tryRight:
if (focus < this->panelCount - 1) if (focus < this->panelCount - 1)
focus++; focus++;
panelFocus = (Panel*) Vector_get(this->panels, focus); panelFocus = setCurrentPanel((Panel*) Vector_get(this->panels, focus));
if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1) if (Panel_size(panelFocus) == 0 && focus < this->panelCount - 1)
goto tryRight; goto tryRight;
break; break;

View File

@ -79,7 +79,6 @@ static inline void appendLine(const char* line, Vector* lines, Panel* panel, con
} }
void TraceScreen_run(TraceScreen* this) { void TraceScreen_run(TraceScreen* this) {
// if (this->process->pid == getpid()) return;
char buffer[1001]; char buffer[1001];
int fdpair[2]; int fdpair[2];
int err = pipe(fdpair); int err = pipe(fdpair);
@ -167,7 +166,7 @@ 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(inc->bar, mevent.x); ch = IncSet_synthesizeEvent(inc, mevent.x);
} }
if (inc->active) { if (inc->active) {
@ -186,11 +185,11 @@ void TraceScreen_run(TraceScreen* this) {
break; break;
case KEY_F(3): case KEY_F(3):
case '/': case '/':
IncSet_activate(inc, INC_SEARCH); IncSet_activate(inc, INC_SEARCH, panel);
break; break;
case KEY_F(4): case KEY_F(4):
case '\\': case '\\':
IncSet_activate(inc, INC_FILTER); IncSet_activate(inc, INC_FILTER, panel);
break; break;
case 'f': case 'f':
case KEY_F(8): case KEY_F(8):