From: a bellenir Date: Fri, 18 Jul 2014 22:37:54 +0000 (+0000) Subject: cd implemented X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=337c490a022b2d219382ddc1623045b7bac57b33;p=archive%2Fafm.git cd implemented --- diff --git a/source/main.c b/source/main.c index 10955cf..07f34da 100644 --- a/source/main.c +++ b/source/main.c @@ -2,6 +2,9 @@ #include #include #include +#include /*TODO: anything using this is almost certainly broken on windows */ +#include +#include 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 */