]> git.mdlowis.com Git - archive/afm.git/commitdiff
workdirs in frames WIP
authora bellenir <a@bellenir.com>
Wed, 23 Jul 2014 21:48:38 +0000 (21:48 +0000)
committera bellenir <a@bellenir.com>
Wed, 23 Jul 2014 21:48:38 +0000 (21:48 +0000)
Rakefile
source/screen.c
source/screen.h
source/workdir.c
source/workdir.h

index 9aaa68eb642f4deb9869446dfff0160083ed7cb8..882e11b5f62c0b45facea45249c77e7a3cd98fad 100644 (file)
--- a/Rakefile
+++ b/Rakefile
@@ -23,6 +23,7 @@ Env = Rscons::Environment.new do |env|
   env['CPPPATH'] += Dir['modules/data-structures/source/**/']
   #env['CFLAGS'] += ['-Wall']
   env['CFLAGS'] += ['-Werror', '-pedantic', '--std=c99']
+  env['CFLAGS'] += ['-D_GNU_SOURCE', '-D_XOPEN_SOURCE=700']
   env['LIBS'] = ['ncurses']
 end
 at_exit { Env.process }
index 0f023285eeb493d4b1149d368fd5000d4ba5bb92..b0838d438d53fafb890aa716fe97acc5f662a479 100644 (file)
@@ -3,16 +3,19 @@
 #include "aardvark.h"
 #include "workdir.h"
 #include <ncurses.h>
+#include <unistd.h>
 #include "vec.h"
 #include "mem.h"
 
 typedef struct {
     WINDOW* p_win;
+    WorkDir_T* workdir;
 } frame_t;
 
 static void screen_place_windows(void);
 static frame_t* screen_frame_new(void);
 static void screen_frame_free(void* p_frame);
+void screen_frame_draw_files(frame_t* frame);
 
 static frame_t* Master;
 static vec_t* Screen_List;
@@ -54,6 +57,7 @@ static void screen_place_windows(void) {
     mvwin(p_frame->p_win, 0, 0);
     wresize(p_frame->p_win, lines, (vec_size(Screen_List) > 0) ? cols/2 : cols);
     wclear(p_frame->p_win);
+    //TODO: draw files: screen_frame_draw_files(p_frame);
     box(p_frame->p_win, 0 , 0);
     wrefresh(p_frame->p_win);
 
@@ -66,6 +70,7 @@ static void screen_place_windows(void) {
         mvwin(p_frame->p_win, pos, cols/2);
         wresize(p_frame->p_win, height, cols/2);
         wclear(p_frame->p_win);
+               //TODO: draw files: screen_frame_draw_files(p_frame);
         wmove(p_frame->p_win, 1, 1);
         wprintw(p_frame->p_win, "(%d, %d)", i*height, cols/2);
         box(p_frame->p_win, 0 , 0);
@@ -77,6 +82,8 @@ static void screen_place_windows(void) {
 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);
+    //TODO use current focused window's path
+    p_frame->workdir = 0; // TODO: create workdir: workdir_new(get_current_dir_name());
     return p_frame;
 }
 
@@ -85,4 +92,31 @@ static void screen_frame_free(void* p_frame_ptr) {
     wborder(p_frame->p_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
     wrefresh(p_frame->p_win);
     delwin(p_frame->p_win);
+    if(p_frame->workdir) mem_release(p_frame->workdir);
 }
+
+void screen_frame_draw_files(frame_t* frame){
+       int i = frame->workdir->top_index;
+       int rows, cols;
+       getmaxyx(frame->p_win, rows, cols);
+       //draw path
+       wattron(frame->p_win, A_UNDERLINE);
+       mvwaddnstr(frame->p_win, 1, 1, frame->workdir->path, cols-2);
+       wattroff(frame->p_win, A_UNDERLINE);
+       //list files
+       while (i < vec_size(frame->workdir->vfiles)){
+               if(i == frame->workdir->idx){
+                       wattron(frame->p_win, A_STANDOUT);
+                       wattron(frame->p_win, A_BOLD);
+               }
+        mvwaddnstr(frame->p_win, FrameTopBuffer+i-frame->workdir->top_index, 1, vec_at(frame->workdir->vfiles, i), cols-2);
+               if(i == frame->workdir->idx){
+                       wattroff(frame->p_win, A_STANDOUT);
+                       wattroff(frame->p_win, A_BOLD);
+               }
+               i++;
+        if((FrameTopBuffer+i-frame->workdir->top_index+FrameBotBuffer) > rows) break;
+       }
+}
+
+
index 44f7114f70926ff9eb25fda956bdaf62e16b4e7d..ef8a66976b597646762928f3f638352508510abc 100644 (file)
@@ -12,4 +12,8 @@ void screen_update(void);
 void screen_open(void);
 void screen_close(void);
 
+//number of lines to leave before/after dir contents in frame
+static int FrameTopBuffer = 2;
+static int FrameBotBuffer = 2;
+
 #endif /* SCREEN_H */
index f645bae0c9c74a89eeabe4fb61ecd234347873e8..0de7e75b5f52a9a759b5e9c509c61d260e980869 100644 (file)
@@ -1,4 +1,3 @@
-#define _XOPEN_SOURCE 700
 #include <ncurses.h>
 #include <string.h>
 #include <stdlib.h>
@@ -9,10 +8,7 @@
 #include "mem.h"
 #include "state.h"
 #include "workdir.h"
-
-//number of lines to leave before/after dir contents
-static int TopBuffer = 2;
-static int BotBuffer = 2;
+#include "screen.h"
 
 static void get_files(int windex);
 
@@ -22,31 +18,24 @@ static bool is_dir(char* path) {
         return (s.st_mode & S_IFDIR);
     }/*else error*/
     return false;
+    //TODO: oneliner: return ((stat(path, &s) == 0) && (s.st_mode & S_IFDIR));
 }
 
 void workdir_free(void* p_wd);
 
 WorkDir_T* workdir_new(char* path){
-       WorkDir_T* wd = mem_allocate(sizeof(WorkDir_T), workdir_free);
+       WorkDir_T* wd = mem_allocate(sizeof(WorkDir_T), &workdir_free);
        wd->idx = 0;
-       strcpy(wd->cwd, path);
-       wd->vfiles = vec_new(0);
+       wd->path = path;
+       workdir_ls(wd);
        wd->top_index = 0;
        return wd;
 }
 
 void workdir_free(void* p_wd){
-       //TODO: free shit.
-}
-
-/*
-void workdir_init(int windex) {
-    Windows[windex].idx = 0;
-    getcwd(Windows[windex].cwd, 1024);
-    Windows[windex].vfiles = vec_new(0);
+       WorkDir_T* wd = (WorkDir_T*)p_wd;
+       mem_release(wd->vfiles);
 }
-*/
-
 
 void workdir_next(WorkDir_T* wd) {
     //do nothing if at the end of the file list
@@ -56,8 +45,8 @@ void workdir_next(WorkDir_T* wd) {
         getmaxyx(stdscr, rows,cols);
         (void) cols;
         //scroll if necessary
-        if((TopBuffer+wd->idx+BotBuffer) > rows)
-            wd->top_index = wd->idx-(rows-TopBuffer-BotBuffer);
+        if((FrameTopBuffer+wd->idx+FrameBotBuffer) > rows)
+            wd->top_index = wd->idx-(rows-FrameTopBuffer-FrameBotBuffer);
     }
 }
 
@@ -71,66 +60,49 @@ void workdir_prev(WorkDir_T* wd) {
     }
 }
 
-void workdir_cd(WorkDir_T* wd) {
-    int last_slash=0, i=0;
-    bool ends_with_slash = false;
-    while(wd->cwd[i] != 0){
-        if(wd->cwd[i] == '/')
-            last_slash = i;
-        i++;
-    }
-    ends_with_slash = (last_slash == (i-1)); /* should only be true for root */
-    if(wd->idx == 0) { /* up */
-        //truncate cwd including the last slash
-        wd->cwd[last_slash]=0;
-        if(last_slash==0){ //at root. fixitfixitfixit.
-                       strcpy(wd->cwd, "/");
-            //wd->cwd[0]='/';
-            //wd->cwd[1]=0;
-        }
-    }else{
-        //add file to cwd:
-        int cwdend = i;
-        if(!ends_with_slash){
-            wd->cwd[i] = '/';
-            i++;
-        }
-        strcpy(&(wd->cwd[i]), vec_at(wd->vfiles, wd->idx));
-        wd->idx = 0;
-        wd->top_index = 0;
-        //if not a directory, revert
-        if(!is_dir(wd->cwd)) wd->cwd[cwdend]=0;
-    }
-    //TODO: refresh file list
+//go up a directory: remove everything after (including) last '/' character
+char* workdir_cd_up(WorkDir_T* wd){
+       int last_slash = 0, i = 0;
+       char* newpath;
+       while(wd->path[i] != 0){
+               if(wd->path[i] == '/') last_slash = i;
+               i++;
+       }
+       if(last_slash == 0){
+               newpath = mem_allocate(sizeof(char)*2, NULL);
+               strcpy(newpath, "/");
+       } else {
+               newpath = mem_allocate(sizeof(char)*last_slash, NULL);
+               strncpy(newpath, wd->path, last_slash);
+               newpath[last_slash-1]=0;
+       }
+       mem_release(wd->path);
+       wd->path = newpath;
 }
 
-/*
-void workdir_ls(void) {
-    int windex = state_get_focused_frame();
-    int i = Windows[windex].top_index;
-    int rows, cols;
-    get_files(windex);
-    getmaxyx(stdscr, rows, cols);
-    attron(A_UNDERLINE);
-    mvaddnstr(1, 1, Windows[windex].cwd, cols-2);
-    attroff(A_UNDERLINE);
-    while (i < vec_size(Windows[windex].vfiles)){
-        if(i == Windows[windex].idx){
-            attron(A_STANDOUT);
-            attron(A_BOLD);
-        }
-        mvaddnstr(TopBuffer+i-Windows[windex].top_index, 1, vec_at(Windows[windex].vfiles, i), cols-2);
-        if(i == Windows[windex].idx){
-            attroff(A_STANDOUT);
-            attroff(A_BOLD);
-        }
-        i++;
-        if((TopBuffer+i-Windows[windex].top_index+BotBuffer) > rows) break;
-    }
+//go down a directory: append '/subdir' to path
+char* workdir_cd_down(WorkDir_T* wd){
+       char* subdir = vec_at(wd->vfiles, wd->idx);
+       int newpathlen = strlen(wd->path) + strlen(subdir) + 2; //+2, for slash & end null;
+       char *newpath = mem_allocate(sizeof(char)*newpathlen, NULL);
+       strcpy(newpath, wd->path);
+       strcat(newpath, "/");
+       strcat(newpath, subdir);
+       return newpath;
+}
+
+void workdir_cd(WorkDir_T* wd) {
+       char* newpath  = (wd->idx == 0) ? workdir_cd_up(wd) : workdir_cd_down(wd);
+       if(is_dir(newpath)){
+               mem_release(wd->path);
+               wd->path = newpath;
+               wd->idx = 0;
+               wd->top_index = 0;
+       }
+    //TODO: refresh file list
 }
-*/
 
-void workdir_refresh_file_list(WorkDir_T* wd){
+void workdir_ls(WorkDir_T* wd){
     int i=0;
     char* dotdot = mem_allocate(sizeof(char)*3, NULL);
     char cmd[1028] = "ls "; //TODO: suck less
@@ -140,8 +112,8 @@ void workdir_refresh_file_list(WorkDir_T* wd){
     FILE* ls;
     if(wd->vfiles) mem_release(wd->vfiles);
     strcpy(dotdot, "..");
-    wd->vfiles = vec_new(1, dotdot); /* TODO: check if cwd = / */
-    strcpy(&cmd[3], wd->cwd);
+    wd->vfiles = vec_new(1, dotdot); /* TODO: check if path = / */
+    strcpy(&cmd[3], wd->path);
     ls = popen(cmd, "r");
     i = 1;
     while ((read = getline(&filename, &len, ls)) != -1){
index a04d8fde4ba50ee4036c1c62b7c667549a4725c0..6b57347f41ac954d236ca2add291cc3b6b2cec53 100644 (file)
 
 typedef struct {
     int idx;
-    char cwd[1024];
+    char* path;
     vec_t* vfiles;
     int top_index;
-    char* title;
 } WorkDir_T;
 
 WorkDir_T* workdir_new(char* path);
@@ -25,6 +24,6 @@ void workdir_next(WorkDir_T*);
 
 void workdir_cd(WorkDir_T*);
 
-void workdir_refresh_file_list(WorkDir_T*);
+void workdir_ls(WorkDir_T*);
 
 #endif /* WORKDIR_H */