From: a bellenir Date: Sun, 20 Jul 2014 03:24:30 +0000 (+0000) Subject: initialize cwd X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=40552e5e6e4ec5d16312a92de57d538096bdfc92;p=archive%2Fafm.git initialize cwd --- diff --git a/source/main.c b/source/main.c index 4b1cdfd..7a6cdbe 100644 --- a/source/main.c +++ b/source/main.c @@ -9,109 +9,18 @@ #include "state.h" #include "input.h" #include "screen.h" +#include "workdir.h" - -// -///* TODO redraw less frequently */ -///* TODO detect filesystem changes */ -// -//void get_files(int windex){ -// /*free existing contents*/ -// int i=0; -// if(Windows[windex].files){ -// /*fuck memory (this is broken) -// while(Files[i]){ -// free(Files[i]); -// i++; -// }*/ -// free(Windows[windex].files); -// } -// /* TODO: malloc smartly, instead of tapping out at 1024 files */ -// Windows[windex].files = malloc(sizeof(char*) * 1024); -// Windows[windex].files[0] = ".."; /* parent directory; TODO only add if cwd!=/ */ -// char cmd[1028] = "ls "; -// strcpy(&cmd[3], Windows[windex].cwd); -// FILE* ls = popen(cmd, "r"); -// size_t len = 0; -// ssize_t read; -// i = 1; -// while ((read = getline(&Windows[windex].files[i], &len, ls)) != -1){ -// if(Windows[windex].files[i][read-1] == '\n') Windows[windex].files[i][read-1] = 0; -// i++; -// if(i>1022) break; -// } -// Windows[windex].file_count = i-1; -// Windows[windex].files[i] = 0; /*always end with nullpointer; since file_count is a thing, can probably do without this*/ -//} -// -//void cd(int windex){ -// int last_slash=0, i=0; -// bool ends_with_slash = false; -// while(Windows[windex].cwd[i] != 0){ -// if(Windows[windex].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 */ -// //truncate cwd including the last slash -// Windows[windex].cwd[last_slash]=0; -// if(last_slash==0){ //at root. fixitfixitfixit. -// Windows[windex].cwd[0]='/'; -// Windows[windex].cwd[1]=0; -// } -// }else{ -// //add file to cwd: -// int cwdend = i; -// if(!ends_with_slash){ -// Windows[windex].cwd[i] = '/'; -// i++; -// } -// strcpy(&Windows[windex].cwd[i], Windows[windex].files[Windows[windex].idx]); -// Windows[windex].idx = 0; -// Windows[windex].top_index = 0; -// //if not a directory, revert -// if(!is_dir(Windows[windex].cwd)) Windows[windex].cwd[cwdend]=0; -// } -//} -// -//void list_files(int windex) { -// get_files(windex); -// int i = Windows[windex].top_index; -// int rows, cols; -// getmaxyx(stdscr, rows, cols); -// attron(A_UNDERLINE); -// mvaddnstr(1, 1, Windows[windex].cwd, cols-2); -// attroff(A_UNDERLINE); -// while (Windows[windex].files[i] != 0){ -// if(i==Windows[windex].idx){ -// attron(A_STANDOUT); -// attron(A_BOLD); -// } -// mvaddnstr(TopBuffer+i-Windows[windex].top_index, 1, Windows[windex].files[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; -// } -//} - void handle_signal(int sig) { signal(SIGWINCH, handle_signal); state_set_screen_dirty(true); state_set_screen_resized(true); } -void init_window_t(windex){ - //Windows[windex].idx = 0; - //getcwd(Windows[windex].cwd, 1024); -} int main(int argc, char** argv) { - init_window_t(0); + workdir_init(0); /* Handle terminal resizing */ signal(SIGWINCH, handle_signal); /* Initialize ncurses and user input settings */ diff --git a/source/workdir.c b/source/workdir.c index 9d644f9..fd2f976 100644 --- a/source/workdir.c +++ b/source/workdir.c @@ -4,6 +4,7 @@ #include #include #include +#include typedef struct { int idx; @@ -31,6 +32,11 @@ static bool is_dir(char* path) { return false; } +void workdir_init(int windex) { + Windows[windex].idx = 0; + getcwd(Windows[windex].cwd, 1024); +} + void workdir_next(void) { int index = state_get_focused_frame(); //do nothing if at the end of the file list diff --git a/source/workdir.h b/source/workdir.h index 6a6802f..1b28aa5 100644 --- a/source/workdir.h +++ b/source/workdir.h @@ -7,6 +7,8 @@ #ifndef WORKDIR_H #define WORKDIR_H +void workdir_init(int windex); + void workdir_prev(void); void workdir_next(void);