From 57d156974c0ae15b4d82f4c5845a43f73f8fe0e6 Mon Sep 17 00:00:00 2001 From: abellenir Date: Sat, 26 Jul 2014 08:50:50 +0000 Subject: [PATCH] add searching --- source/input.c | 30 ++++++++++++++++++++++++++++++ source/main.c | 1 + source/state.c | 10 ++++++++++ source/state.h | 4 +++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/source/input.c b/source/input.c index 00c712a..cdaf080 100644 --- a/source/input.c +++ b/source/input.c @@ -5,6 +5,8 @@ #include #include +#define ESC 27 + typedef void (*key_cb_t)(void); typedef struct { @@ -46,7 +48,35 @@ static void handle_page_down(void){ workdir_jump_down(state_get_focused_frame()->workdir); } + static void search_mode(void){ + int searchcap = 8; + char* searchstr = malloc(sizeof(char)*searchcap); + int searchlen = 0; + bool searching = true; + state_set_mode(MODE_SEARCH); + while(searching){ + char inpt = getch(); + if(inpt == ERR){ /* do nothing */ + }else if(inpt == ESC){ + searching = false; + }else if (inpt == '\n'){ + searching = false; + handle_cd(); + }else{ + if(searchlen+1 >= searchcap){ + searchcap *= 2; + searchstr = realloc(searchstr, sizeof(char)*searchcap); + } + searchstr[searchlen] = inpt; + searchlen += 1; + searchstr[searchlen] = 0; + workdir_seek(state_get_focused_frame()->workdir, searchstr); + } + if(state_get_screen_dirty()) screen_update(); + } + free(searchstr); + state_set_mode(MODE_NORMAL); } static binding_t Default_Bindings[] = { diff --git a/source/main.c b/source/main.c index 86ecf80..cf7f83a 100644 --- a/source/main.c +++ b/source/main.c @@ -30,6 +30,7 @@ int main(int argc, char** argv) { timeout(25); refresh(); screen_init(); + state_set_mode(MODE_NORMAL); while(state_get_running()) { if(state_get_screen_dirty()) screen_update(); input_handle_key(getch()); diff --git a/source/state.c b/source/state.c index e8158c3..5cf276e 100644 --- a/source/state.c +++ b/source/state.c @@ -16,6 +16,8 @@ static bool AardvarkOn = false; /** A pointer to the currently focused frame */ static frame_t* Focused_Frame = 0; +static Mode_T CurrentMode = 0; + bool state_get_running(void) { return Running; @@ -65,3 +67,11 @@ void state_set_focused_frame(frame_t *p_frame) Focused_Frame = p_frame; } +Mode_T state_get_mode(){ + return CurrentMode; +} + +void state_set_mode(Mode_T m){ + CurrentMode = m; +} + diff --git a/source/state.h b/source/state.h index 1da0923..cc342ef 100644 --- a/source/state.h +++ b/source/state.h @@ -10,7 +10,7 @@ #include #include "screen.h" -typedef enum{ MODE_NORMAL, MODE_SEARCH } MODE; +typedef enum{ MODE_NORMAL, MODE_SEARCH } Mode_T; bool state_get_running(void); void state_set_running(bool val); @@ -22,5 +22,7 @@ bool state_get_aardvark_mode(void); void state_set_aardvark_mode(bool val); frame_t* state_get_focused_frame(void); void state_set_focused_frame(frame_t* p_frame); +Mode_T state_get_mode(void); +void state_set_mode(Mode_T); #endif /* STATE_H */ -- 2.52.0