From 1e9974e28cc8b3f6278ce7d847eb0d487a371ab1 Mon Sep 17 00:00:00 2001 From: abellenir Date: Sat, 26 Jul 2014 08:52:26 +0000 Subject: [PATCH] refactor scrolling & index jumping --- source/screen.c | 18 ++++++++++-- source/screen.h | 1 + source/workdir.c | 71 ++++++++++++++++++------------------------------ source/workdir.h | 1 - 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/source/screen.c b/source/screen.c index 3c66018..ceeeb31 100644 --- a/source/screen.c +++ b/source/screen.c @@ -117,6 +117,7 @@ char* pwd(){ static frame_t* screen_frame_new(void) { frame_t* p_frame = (frame_t*)mem_allocate(sizeof(frame_t),&screen_frame_free); p_frame->p_win = newwin(1, 1, 0, 0); + p_frame->top_index = 0; bool first_window = !state_get_focused_frame(); char* path = first_window ? pwd() : state_get_focused_frame()->workdir->path; p_frame->workdir = workdir_new(path); @@ -133,11 +134,22 @@ static void screen_frame_free(void* p_frame_ptr) { if(p_frame->workdir) mem_release(p_frame->workdir); } +static void screen_frame_scroll(frame_t* p_frame){ + int rows,cols; + getmaxyx(p_frame->p_win, rows, cols); + if(p_frame->workdir->idx < p_frame->top_index) + p_frame->top_index = p_frame->workdir->idx; + else if (p_frame->top_index < p_frame->workdir->idx-(rows-FrameTopBuffer-FrameBotBuffer)) + p_frame->top_index = p_frame->workdir->idx-(rows-FrameTopBuffer-FrameBotBuffer); +} + void screen_frame_draw_files(frame_t* frame){ - int i = frame->workdir->top_index; + int i; int rows, cols; int pathlength = strlen(frame->workdir->path); getmaxyx(frame->p_win, rows, cols); + screen_frame_scroll(frame); + i = frame->top_index; //draw path wattron(frame->p_win, A_UNDERLINE); mvwaddnstr(frame->p_win, 1, 1, frame->workdir->path, cols-2); @@ -150,13 +162,13 @@ void screen_frame_draw_files(frame_t* frame){ wattron(frame->p_win, A_STANDOUT | A_BOLD); } if(dir) wattron(frame->p_win, COLOR_PAIR(DIRECTORY)); - mvwaddnstr(frame->p_win, FrameTopBuffer+i-frame->workdir->top_index, 1, file->name, cols-2); + mvwaddnstr(frame->p_win, FrameTopBuffer+i-frame->top_index, 1, file->name, cols-2); if(frame == state_get_focused_frame() && i == frame->workdir->idx){ wattroff(frame->p_win, A_STANDOUT | A_BOLD); } if(dir) wattroff(frame->p_win, COLOR_PAIR(DIRECTORY)); i++; - if((FrameTopBuffer+i-frame->workdir->top_index+FrameBotBuffer) > rows) break; + if((FrameTopBuffer+i-frame->top_index+FrameBotBuffer) > rows) break; } } diff --git a/source/screen.h b/source/screen.h index 546cc24..91bd67e 100644 --- a/source/screen.h +++ b/source/screen.h @@ -23,6 +23,7 @@ static int FrameBotBuffer = 2; typedef struct { WINDOW* p_win; WorkDir_T* workdir; + int top_index; } frame_t; enum ColorPairs { diff --git a/source/workdir.c b/source/workdir.c index ab40d4f..3993954 100644 --- a/source/workdir.c +++ b/source/workdir.c @@ -31,7 +31,6 @@ WorkDir_T* workdir_new(char* path){ mem_retain(wd->path); wd->vfiles = vec_new(0); workdir_ls(wd); - wd->top_index = 0; return wd; } @@ -48,29 +47,35 @@ void file_free(void* p_vfile){ mem_release(p_file->path); } +void workdir_set_idx(WorkDir_T* wd, int idx){ + wd->idx = idx; + if(idx < 0) wd->idx = 0; + if(idx >= vec_size(wd->vfiles)) wd->idx = vec_size(wd->vfiles)-1; + state_set_screen_dirty(true); +} + void workdir_next(WorkDir_T* wd) { - //do nothing if at the end of the file list - if(wd->idx < vec_size(wd->vfiles)-1){ - int rows,cols; - wd->idx += 1; - getmaxyx(stdscr, rows,cols); - (void) cols; - //scroll if necessary - if((FrameTopBuffer+wd->idx+FrameBotBuffer) > rows) - wd->top_index = wd->idx-(rows-FrameTopBuffer-FrameBotBuffer); - } - state_set_screen_dirty(true); + workdir_set_idx(wd, wd->idx+1); } void workdir_prev(WorkDir_T* wd) { - //do nothing if at the top of the file list - if(wd->idx > 0){ - wd->idx -= 1; - //scroll if necessary - if(wd->idx < wd->top_index) - wd->top_index = wd->idx; - } - state_set_screen_dirty(true); + workdir_set_idx(wd, wd->idx-1); +} + +void workdir_scroll_to_top(WorkDir_T* wd){ + workdir_set_idx(wd, 0); +} + +void workdir_scroll_to_bot(WorkDir_T* wd){ + workdir_set_idx(wd, vec_size(wd->vfiles) - 1); +} + +void workdir_jump_up(WorkDir_T* wd){ + workdir_set_idx(wd, wd->idx - 20); +} + +void workdir_jump_down(WorkDir_T* wd){ + workdir_set_idx(wd, wd->idx + 20); } void workdir_cd(WorkDir_T* wd) { @@ -80,7 +85,6 @@ void workdir_cd(WorkDir_T* wd) { wd->path = newpath; mem_retain(wd->path); wd->idx = 0; - wd->top_index = 0; } workdir_ls(wd); state_set_screen_dirty(true); @@ -152,29 +156,8 @@ void workdir_ls(WorkDir_T* wd){ void workdir_seek(WorkDir_T* wd, char* search){ int i = 0; if(strcmp(((File_T*)vec_at(wd->vfiles, 0))->name, "..") == 0) i++; - while(i < vec_size(wd->vfiles) && strcmp(search, ((File_T*)vec_at(wd->vfiles, i))->name) < 0) i++; - wd->idx = i; -} - -void workdir_scroll_to_top(WorkDir_T* wd){ - wd->idx = 0; - state_set_screen_dirty(true); -} - -void workdir_scroll_to_bot(WorkDir_T* wd){ - wd->idx = vec_size(wd->vfiles) - 1; - state_set_screen_dirty(true); -} - -void workdir_jump_up(WorkDir_T* wd){ - wd->idx -= 20; - if(wd->idx < 0) wd->idx = 0; - state_set_screen_dirty(true); + while(i < vec_size(wd->vfiles) && strcmp(((File_T*)vec_at(wd->vfiles, i))->name, search) < 0) i++; + workdir_set_idx(wd, i); } -void workdir_jump_down(WorkDir_T* wd){ - wd->idx += 20; - if(wd->idx >= vec_size(wd->vfiles)) wd->idx = vec_size(wd->vfiles)-1; - state_set_screen_dirty(true); -} diff --git a/source/workdir.h b/source/workdir.h index 531fa73..6226a3a 100644 --- a/source/workdir.h +++ b/source/workdir.h @@ -16,7 +16,6 @@ typedef struct { int idx; char* path; vec_t* vfiles; - int top_index; } WorkDir_T; typedef struct { -- 2.52.0