From 572546f8063095ff3195ef10823ec1f4882092f3 Mon Sep 17 00:00:00 2001 From: Hisham Date: Thu, 5 May 2016 10:30:06 -0300 Subject: Auto-follow process after a search. See #237. --- Action.c | 4 ++-- Action.h | 2 ++ IncSet.c | 42 +++++++++++++++++++++++++----------------- IncSet.h | 1 + MainPanel.c | 3 +++ 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Action.c b/Action.c index 3fc22177..ee34a4bc 100644 --- a/Action.c +++ b/Action.c @@ -328,7 +328,7 @@ static Htop_Reaction actionFilterByUser(State* st) { return HTOP_REFRESH | HTOP_REDRAW_BAR | HTOP_UPDATE_PANELHDR; } -static Htop_Reaction actionFollow(State* st) { +Htop_Reaction Action_follow(State* st) { st->pl->following = MainPanel_selectedPid((MainPanel*)st->panel); Panel_setSelectionColor(st->panel, CRT_colors[PANEL_SELECTION_FOLLOW]); return HTOP_KEEP_FOLLOWING; @@ -557,7 +557,7 @@ void Action_setBindings(Htop_Action* keys) { keys['='] = actionExpandOrCollapse; keys['-'] = actionExpandOrCollapse; keys['u'] = actionFilterByUser; - keys['F'] = actionFollow; + keys['F'] = Action_follow; keys['S'] = actionSetup; keys['C'] = actionSetup; keys[KEY_F(2)] = actionSetup; diff --git a/Action.h b/Action.h index 34fc298f..1dfdcb47 100644 --- a/Action.h +++ b/Action.h @@ -49,6 +49,8 @@ Htop_Reaction Action_setSortKey(Settings* settings, ProcessField sortKey); // ---------------------------------------- +Htop_Reaction Action_follow(State* st); + void Action_setBindings(Htop_Action* keys); diff --git a/IncSet.c b/IncSet.c index 01493824..c7f889cc 100644 --- a/IncSet.c +++ b/IncSet.c @@ -40,6 +40,7 @@ typedef struct IncSet_ { IncMode* active; FunctionBar* defaultBar; bool filtering; + bool found; } IncSet; typedef const char* (*IncMode_GetPanelValue)(Panel*, int); @@ -114,7 +115,7 @@ static void updateWeakPanel(IncSet* this, Panel* panel, Vector* lines) { } } -static void search(IncMode* mode, Panel* panel, IncMode_GetPanelValue getPanelValue) { +static bool search(IncMode* mode, Panel* panel, IncMode_GetPanelValue getPanelValue) { int size = Panel_size(panel); bool found = false; for (int i = 0; i < size; i++) { @@ -128,6 +129,7 @@ static void search(IncMode* mode, Panel* panel, IncMode_GetPanelValue getPanelVa FunctionBar_draw(mode->bar, mode->buffer); else FunctionBar_drawAttr(mode->bar, mode->buffer, CRT_colors[FAILED_SEARCH]); + return found; } bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue getPanelValue, Vector* lines) { @@ -151,23 +153,29 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue } } doSearch = false; - } else if (ch < 255 && isprint((char)ch) && (mode->index < INCMODE_MAX)) { - mode->buffer[mode->index] = ch; - mode->index++; - mode->buffer[mode->index] = 0; - if (mode->isFilter) { - filterChanged = true; - if (mode->index == 1) this->filtering = true; + } else if (ch < 255 && isprint((char)ch)) { + if (mode->index < INCMODE_MAX) { + mode->buffer[mode->index] = ch; + mode->index++; + mode->buffer[mode->index] = 0; + if (mode->isFilter) { + filterChanged = 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) { - filterChanged = true; - if (mode->index == 0) { - this->filtering = false; - IncMode_reset(mode); + } else if ((ch == KEY_BACKSPACE || ch == 127)) { + if (mode->index > 0) { + mode->index--; + mode->buffer[mode->index] = 0; + if (mode->isFilter) { + filterChanged = true; + if (mode->index == 0) { + this->filtering = false; + IncMode_reset(mode); + } } + } else { + doSearch = false; } } else if (ch == KEY_RESIZE) { Panel_resize(panel, COLS, LINES-panel->y-1); @@ -187,7 +195,7 @@ bool IncSet_handleKey(IncSet* this, int ch, Panel* panel, IncMode_GetPanelValue doSearch = false; } if (doSearch) { - search(mode, panel, getPanelValue); + this->found = search(mode, panel, getPanelValue); } if (filterChanged && lines) { updateWeakPanel(this, panel, lines); diff --git a/IncSet.h b/IncSet.h index db05eccd..27538f4b 100644 --- a/IncSet.h +++ b/IncSet.h @@ -35,6 +35,7 @@ typedef struct IncSet_ { IncMode* active; FunctionBar* defaultBar; bool filtering; + bool found; } IncSet; typedef const char* (*IncMode_GetPanelValue)(Panel*, int); diff --git a/MainPanel.c b/MainPanel.c index f9b45d64..ed7f3ad1 100644 --- a/MainPanel.c +++ b/MainPanel.c @@ -83,6 +83,9 @@ static HandlerResult MainPanel_eventHandler(Panel* super, int ch) { result = HANDLED; } else if (ch != ERR && this->inc->active) { bool filterChanged = IncSet_handleKey(this->inc, ch, super, (IncMode_GetPanelValue) MainPanel_getValue, NULL); + if (this->inc->found) { + reaction |= Action_follow(this->state); + } if (filterChanged) { this->state->pl->incFilter = IncSet_filter(this->inc); reaction = HTOP_REFRESH | HTOP_REDRAW_BAR; -- cgit v1.2.3