]> git.mdlowis.com Git - archive/afm.git/commitdiff
refactor scrolling & index jumping
authorabellenir <a@bellenir.com>
Sat, 26 Jul 2014 08:52:26 +0000 (08:52 +0000)
committerabellenir <a@bellenir.com>
Sat, 26 Jul 2014 08:52:26 +0000 (08:52 +0000)
source/screen.c
source/screen.h
source/workdir.c
source/workdir.h

index 3c66018fb37572db96e088d397498bd5084c2d19..ceeeb314994a2c0ef47bae0324d7f9dad828d3bc 100644 (file)
@@ -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;
     }
 }
 
index 546cc2402fc2fa30a2840eeddae6f3d94ab1bdb1..91bd67e6db89ef973a2addeda99b196aee5fe285 100644 (file)
@@ -23,6 +23,7 @@ static int FrameBotBuffer = 2;
 typedef struct {
     WINDOW* p_win;
     WorkDir_T* workdir;
+    int top_index;
 } frame_t;
 
 enum ColorPairs {
index ab40d4f01ce48839336f8e9ce050559612dc2475..3993954647625634291a505b61e50d3281e53d44 100644 (file)
@@ -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);
-}
 
index 531fa7365a049b92d64b6b6ee1c9ac198b310a6c..6226a3adbd75632461c428f92f16552daba4c0fe 100644 (file)
@@ -16,7 +16,6 @@ typedef struct {
     int idx;
     char* path;
     vec_t* vfiles;
-    int top_index;
 } WorkDir_T;
 
 typedef struct {