]> git.mdlowis.com Git - archive/afm.git/commitdiff
cd implemented
authora bellenir <a@bellenir.com>
Fri, 18 Jul 2014 22:37:54 +0000 (22:37 +0000)
committera bellenir <a@bellenir.com>
Fri, 18 Jul 2014 22:37:54 +0000 (22:37 +0000)
source/main.c

index 10955cf8abf48fcd8cbbf0b8aae7bcde94efd678..07f34da93c91911be87335829bb212bc406d5853 100644 (file)
@@ -2,6 +2,9 @@
 #include <stdbool.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <unistd.h> /*TODO: anything using this is almost certainly broken on windows */
+#include <string.h>
+#include <sys/stat.h>
 
 static bool Running = true;
 //static int Term_X = 0;
@@ -10,10 +13,18 @@ static WINDOW* WindowLeft;
 static WINDOW* WindowRight;
 
 static int Idx = 0; /* TODO: this should be per-window */
-//static char* Cwd = ""; /* TODO; this should be per-window */
+static char Cwd[1024]; /* TODO; this should be per-window & smarter than 1024chars */
 static char** Files; /* TODO: this should be per-window */
 static int FileCount; /* TODO: ditto */
 
+bool is_dir(char* path){
+    struct stat s;
+    if( stat(path, &s) == 0){
+        return (s.st_mode & S_IFDIR);
+    }/*else error*/
+    return false;
+}
+
 /* TODO redraw less frequently */
 /* TODO detect filesystem changes */
 
@@ -32,11 +43,14 @@ void get_files(){
     /* TODO: malloc smartly, instead of tapping out at 1024 files */
     Files = malloc(sizeof(char*)*1024);
     Files[0] = ".."; /* parent directory; todo only add if cwd!=/ */
-    FILE* ls = popen("ls", "r");
+    char cmd[1028]="ls ";
+    strcpy(&cmd[3],Cwd);
+    FILE* ls = popen(cmd, "r");
     size_t len = 0;
     ssize_t read;
     i=1;
     while ((read = getline(&Files[i], &len, ls)) != -1){
+        if(Files[i][read-1] == '\n') Files[i][read-1] = 0;
         i++;
         if(i>1022) break;
     }
@@ -44,12 +58,49 @@ void get_files(){
     Files[i]=0; /*always end with nullpointer */
 }
 
+void cd(int file_index){
+    int last_slash=0, i=0;
+    bool ends_with_slash = false;
+    while(Cwd[i] != 0){
+        if(Cwd[i] == '/')
+            last_slash=i;
+        i++;
+    }
+    if(last_slash==i-1) { /* should only be true for root */
+        ends_with_slash = true;
+    }
+    if(file_index==0) { /* up */
+        //truncate Cwd including the last slash
+        Cwd[last_slash]=0;
+        if(last_slash==0){ /*but dont have an empty cwd */
+            Cwd[0]='/';
+            Cwd[1]=0;
+        }
+    }else{
+        //add file to Cwd:
+        int cwdend = i;
+        if(!ends_with_slash){
+            Cwd[i] = '/';
+            i++;
+        }
+        (void)cwdend;
+        strcpy(&Cwd[i], Files[file_index]);
+        //kill_newlines(Cwd);
+        Idx=0;
+        //if not a directory, revert
+        if(!is_dir(Cwd)) Cwd[cwdend]=0;
+    }
+}
+
 void list_files(WINDOW* win) {
     get_files();
-    int topbuff=1; /* lines to skip before printing (window border/title) */
+    int topbuff=2; /* lines to skip before printing (window border/title) */
     int i = 0;
     int rows, cols;
     getmaxyx(win, rows, cols);
+    wattron(win, A_UNDERLINE);
+    mvwaddnstr(win, 1, 1, &Cwd, cols-2);
+    wattroff(win, A_UNDERLINE);
     while (Files[i] != 0){
         if(i==Idx){
             wattron(win, A_STANDOUT);
@@ -61,7 +112,7 @@ void list_files(WINDOW* win) {
             wattroff(win, A_BOLD);
         }
         i++;
-        if(i>rows) break; /* TODO: implement scrolling to handle when there are more files than lines */
+        if((topbuff+i)>rows) break; /* TODO: implement scrolling to handle when there are more files than lines */
     }
 }
 
@@ -72,6 +123,8 @@ void handle_input(char ch) {
         Idx+=1;
     if(ch == 'k' && Idx>0)
         Idx-=1;
+    if(ch == 'e')
+        cd(Idx);
 }
 
 WINDOW* create_window(int x, int y, int height, int width) {
@@ -107,6 +160,7 @@ void handle_signal(int sig) {
 }
 
 int main(int argc, char** argv) {
+    getcwd(Cwd, sizeof(Cwd)); /* TODO: check for errors; fix in windows */
     /* Handle terminal resizing */
     signal(SIGWINCH, handle_signal);
     /* Initialize ncurses and user input settings */