]> git.mdlowis.com Git - archive/afm.git/commitdiff
WIP. builds
authora bellenir <a@bellenir.com>
Wed, 23 Jul 2014 20:21:47 +0000 (20:21 +0000)
committera bellenir <a@bellenir.com>
Wed, 23 Jul 2014 20:21:47 +0000 (20:21 +0000)
source/input.c
source/main.c
source/workdir.c
source/workdir.h

index 2f147d298cb9b3bd121ba5f3915d106f666a3134..09da1158a44f82b492801b2a72e40ce68dff33dd 100644 (file)
@@ -11,12 +11,9 @@ void input_handle_key(char ch) {
                   break;
         case 'q': state_set_running(false);
                   break;
-        case 'j': workdir_next();
-                  break;
-        case 'k': workdir_prev();
-                  break;
-        case 'e': workdir_cd();
-                  break;
+        //case 'j': workdir_next(); break;
+        //case 'k': workdir_prev(); break;
+        //case 'e': workdir_cd(); break;
         case 'n': screen_open();
                   break;
         case 'c': screen_close();
index 8417d56869499672a36964138796a3e993ed8dba..ad5aa237a79f71c97463fcfad70ce55e2c314e6c 100644 (file)
@@ -19,7 +19,6 @@ void handle_signal(int sig) {
 }
 
 int main(int argc, char** argv) {
-    workdir_init(0);
     /* Handle terminal resizing */
     signal(SIGWINCH, handle_signal);
     /* Initialize ncurses and user input settings */
index 487e132568664f1ede9f6359eb2351854ef78ddd..f645bae0c9c74a89eeabe4fb61ecd234347873e8 100644 (file)
 #include "state.h"
 #include "workdir.h"
 
-typedef struct {
-    int idx;
-    char cwd[1024];
-    vec_t* vfiles;
-    int top_index;
-    char* title;
-} Window_T;
-
-/*TODO: arbitrary number of windows */
-static Window_T Windows[1];
-
 //number of lines to leave before/after dir contents
 static int TopBuffer = 2;
 static int BotBuffer = 2;
@@ -35,67 +24,87 @@ static bool is_dir(char* path) {
     return false;
 }
 
+void workdir_free(void* p_wd);
+
+WorkDir_T* workdir_new(char* path){
+       WorkDir_T* wd = mem_allocate(sizeof(WorkDir_T), workdir_free);
+       wd->idx = 0;
+       strcpy(wd->cwd, path);
+       wd->vfiles = vec_new(0);
+       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);
 }
+*/
 
-void workdir_next(void) {
-    int index = state_get_focused_frame();
+
+void workdir_next(WorkDir_T* wd) {
     //do nothing if at the end of the file list
-    if(Windows[index].idx < vec_size(Windows[index].vfiles)-1){
+    if(wd->idx < vec_size(wd->vfiles)-1){
         int rows,cols;
-        Windows[index].idx += 1;
+        wd->idx += 1;
         getmaxyx(stdscr, rows,cols);
         (void) cols;
-        if((TopBuffer+Windows[index].idx+BotBuffer) > rows)
-            Windows[index].top_index = Windows[index].idx-(rows-TopBuffer-BotBuffer);
+        //scroll if necessary
+        if((TopBuffer+wd->idx+BotBuffer) > rows)
+            wd->top_index = wd->idx-(rows-TopBuffer-BotBuffer);
     }
 }
 
-void workdir_prev(void) {
-    int index = state_get_focused_frame();
+void workdir_prev(WorkDir_T* wd) {
     //do nothing if at the top of the file list
-    if(Windows[index].idx > 0){
-        Windows[index].idx -= 1;
-        if(Windows[index].idx < Windows[index].top_index)
-            Windows[index].top_index = Windows[index].idx;
+    if(wd->idx > 0){
+        wd->idx -= 1;
+        //scroll if necessary
+        if(wd->idx < wd->top_index)
+            wd->top_index = wd->idx;
     }
 }
 
-void workdir_cd(void) {
-    int windex = state_get_focused_frame();
+void workdir_cd(WorkDir_T* wd) {
     int last_slash=0, i=0;
     bool ends_with_slash = false;
-    while(Windows[windex].cwd[i] != 0){
-        if(Windows[windex].cwd[i] == '/')
+    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(Windows[windex].idx == 0) { /* up */
+    if(wd->idx == 0) { /* up */
         //truncate cwd including the last slash
-        Windows[windex].cwd[last_slash]=0;
+        wd->cwd[last_slash]=0;
         if(last_slash==0){ //at root. fixitfixitfixit.
-            Windows[windex].cwd[0]='/';
-            Windows[windex].cwd[1]=0;
+                       strcpy(wd->cwd, "/");
+            //wd->cwd[0]='/';
+            //wd->cwd[1]=0;
         }
     }else{
         //add file to cwd:
         int cwdend = i;
         if(!ends_with_slash){
-            Windows[windex].cwd[i] = '/';
+            wd->cwd[i] = '/';
             i++;
         }
-        strcpy(&Windows[windex].cwd[i], vec_at(Windows[windex].vfiles, Windows[windex].idx));
-        Windows[windex].idx = 0;
-        Windows[windex].top_index = 0;
+        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(Windows[windex].cwd)) Windows[windex].cwd[cwdend]=0;
+        if(!is_dir(wd->cwd)) wd->cwd[cwdend]=0;
     }
+    //TODO: refresh file list
 }
 
+/*
 void workdir_ls(void) {
     int windex = state_get_focused_frame();
     int i = Windows[windex].top_index;
@@ -119,26 +128,27 @@ void workdir_ls(void) {
         if((TopBuffer+i-Windows[windex].top_index+BotBuffer) > rows) break;
     }
 }
+*/
 
-static void get_files(int windex){
+void workdir_refresh_file_list(WorkDir_T* wd){
     int i=0;
     char* dotdot = mem_allocate(sizeof(char)*3, NULL);
-    char cmd[1028] = "ls ";
+    char cmd[1028] = "ls "; //TODO: suck less
     size_t len = 0; //unused. reflects sized allocated for buffer (filename) by getline
     ssize_t read;
     char* filename=0;
     FILE* ls;
-    if(Windows[windex].vfiles) mem_release(Windows[windex].vfiles);
+    if(wd->vfiles) mem_release(wd->vfiles);
     strcpy(dotdot, "..");
-    Windows[windex].vfiles = vec_new(1, dotdot); /* TODO: check if cwd = / */
-    strcpy(&cmd[3], Windows[windex].cwd);
+    wd->vfiles = vec_new(1, dotdot); /* TODO: check if cwd = / */
+    strcpy(&cmd[3], wd->cwd);
     ls = popen(cmd, "r");
     i = 1;
     while ((read = getline(&filename, &len, ls)) != -1){
         char* lol = mem_allocate(read*sizeof(char), NULL);
         filename[read-1]=0; //remove ending newline
         strcpy(lol, filename);
-        vec_push_back(Windows[windex].vfiles, lol);
+        vec_push_back(wd->vfiles, lol);
         i++;
         if(i>1022) break;
     }
index 1b28aa50d1abcbf00985250dc7ea081e7bf4c603..a04d8fde4ba50ee4036c1c62b7c667549a4725c0 100644 (file)
@@ -7,14 +7,24 @@
 #ifndef WORKDIR_H
 #define WORKDIR_H
 
-void workdir_init(int windex);
+#include "vec.h"
 
-void workdir_prev(void);
+typedef struct {
+    int idx;
+    char cwd[1024];
+    vec_t* vfiles;
+    int top_index;
+    char* title;
+} WorkDir_T;
 
-void workdir_next(void);
+WorkDir_T* workdir_new(char* path);
 
-void workdir_cd(void);
+void workdir_prev(WorkDir_T*);
 
-void workdir_ls(void);
+void workdir_next(WorkDir_T*);
+
+void workdir_cd(WorkDir_T*);
+
+void workdir_refresh_file_list(WorkDir_T*);
 
 #endif /* WORKDIR_H */