From 12c82c29f8229d9362a013130929fe9c01b3be09 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 19 Jul 2014 21:50:32 -0400 Subject: [PATCH] i've broken everything --- source/input.c | 24 ++++ source/input.h | 12 ++ source/main.c | 277 ++++++++++++++++------------------------------- source/screen.c | 29 +++++ source/screen.h | 12 ++ source/workdir.c | 140 ++++++++++++++++++++++++ source/workdir.h | 18 +++ 7 files changed, 330 insertions(+), 182 deletions(-) create mode 100644 source/input.c create mode 100644 source/input.h create mode 100644 source/screen.c create mode 100644 source/screen.h create mode 100644 source/workdir.c create mode 100644 source/workdir.h diff --git a/source/input.c b/source/input.c new file mode 100644 index 0000000..1606ec3 --- /dev/null +++ b/source/input.c @@ -0,0 +1,24 @@ +#include "input.h" +#include "state.h" +#include "workdir.h" + +void input_handle_key(char ch) { + /* Assume screen is dirty by default */ + bool is_screen_dirty = true; + switch(ch){ + case 'a': state_set_aardvark_mode(!state_get_aardvark_mode()); + break; + case 'q': state_set_running(false); + break; + case 'j': workdir_next(); + break; + case 'k': workdir_prev(); + break; + case 'e': workdir_cd(); + break; + default: is_screen_dirty = false; + break; + } + /* Update the screen if we need to */ + state_set_screen_dirty(state_get_screen_dirty() || is_screen_dirty); +} diff --git a/source/input.h b/source/input.h new file mode 100644 index 0000000..75c96ab --- /dev/null +++ b/source/input.h @@ -0,0 +1,12 @@ +/** + @file input.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef INPUT_H +#define INPUT_H + +void input_handle_key(char ch); + +#endif /* INPUT_H */ diff --git a/source/main.c b/source/main.c index b78ad15..4b1cdfd 100644 --- a/source/main.c +++ b/source/main.c @@ -4,187 +4,100 @@ #include #include /*TODO: anything using this is almost certainly broken on windows */ #include -#include #include "aardvark.h" #include "state.h" - -typedef struct { - int idx; - char cwd[1024]; - char **files; - int file_count; - 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; - -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 */ - -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 scroll_down(){ - int index = state_get_focused_frame(); - //do nothing if at the end of the file list - if(Windows[index].idx < Windows[index].file_count){ - Windows[index].idx += 1; - int rows,cols; - getmaxyx(stdscr, rows,cols); - (void) cols; - if((TopBuffer+Windows[index].idx+BotBuffer) > rows) - Windows[index].top_index = Windows[index].idx-(rows-TopBuffer-BotBuffer); - } -} -void scroll_up(){ - int index = state_get_focused_frame(); - //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; - } -} - -void update_screen(void) { - /* Clear screen and update LINES and COLS */ - if(state_get_screen_resized()){ - endwin(); - state_set_screen_resized(false); - } - clear(); - //should probably redraw all, but since only one window exists, it doesn't matter - list_files(state_get_focused_frame()); - if(state_get_aardvark_mode()) aardvark_draw(); - /* Draw the Border */ - mvaddch(0, 0, ACS_ULCORNER); - mvhline(0, 1, ACS_HLINE, COLS-2); - mvaddch(0, COLS-1, ACS_URCORNER); - mvvline(1, 0, ACS_VLINE, LINES-2); - mvaddch(LINES-1, 0, ACS_LLCORNER); - mvhline(LINES-1, 1, ACS_HLINE, COLS-2); - mvaddch(LINES-1, COLS-1, ACS_LRCORNER); - mvvline(1, COLS-1, ACS_VLINE, LINES-2); - /* Refresh and mark complete */ - refresh(); - state_set_screen_dirty(false); -} - -void handle_input(char ch) { - /* Assume screen is dirty by default */ - bool is_screen_dirty = true; - switch(ch){ - case 'a': state_set_aardvark_mode(!state_get_aardvark_mode()); - break; - case 'q': state_set_running(false); - break; - case 'j': scroll_down(); - break; - case 'k': scroll_up(); - break; - case 'e': cd(state_get_focused_frame()); - break; - default: is_screen_dirty = false; - break; - } - /* Update the screen if we need to */ - state_set_screen_dirty(state_get_screen_dirty() || is_screen_dirty); -} +#include "input.h" +#include "screen.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); @@ -193,8 +106,8 @@ void handle_signal(int sig) { } void init_window_t(windex){ - Windows[windex].idx = 0; - getcwd(Windows[windex].cwd, 1024); + //Windows[windex].idx = 0; + //getcwd(Windows[windex].cwd, 1024); } int main(int argc, char** argv) { @@ -209,8 +122,8 @@ int main(int argc, char** argv) { timeout(25); refresh(); while(state_get_running()) { - if(state_get_screen_dirty()) update_screen(); - handle_input(getch()); + if(state_get_screen_dirty()) screen_update(); + input_handle_key(getch()); } erase(); refresh(); diff --git a/source/screen.c b/source/screen.c new file mode 100644 index 0000000..3e82e35 --- /dev/null +++ b/source/screen.c @@ -0,0 +1,29 @@ +#include "screen.h" +#include "state.h" +#include "aardvark.h" +#include "workdir.h" +#include + +void screen_update(void) { + /* Clear screen and update LINES and COLS */ + if(state_get_screen_resized()){ + endwin(); + state_set_screen_resized(false); + } + clear(); + //should probably redraw all, but since only one window exists, it doesn't matter + workdir_ls(); + if(state_get_aardvark_mode()) aardvark_draw(); + /* Draw the Border */ + mvaddch(0, 0, ACS_ULCORNER); + mvhline(0, 1, ACS_HLINE, COLS-2); + mvaddch(0, COLS-1, ACS_URCORNER); + mvvline(1, 0, ACS_VLINE, LINES-2); + mvaddch(LINES-1, 0, ACS_LLCORNER); + mvhline(LINES-1, 1, ACS_HLINE, COLS-2); + mvaddch(LINES-1, COLS-1, ACS_LRCORNER); + mvvline(1, COLS-1, ACS_VLINE, LINES-2); + /* Refresh and mark complete */ + refresh(); + state_set_screen_dirty(false); +} diff --git a/source/screen.h b/source/screen.h new file mode 100644 index 0000000..d400a41 --- /dev/null +++ b/source/screen.h @@ -0,0 +1,12 @@ +/** + @file screen.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef SCREEN_H +#define SCREEN_H + +void screen_update(void); + +#endif /* SCREEN_H */ diff --git a/source/workdir.c b/source/workdir.c new file mode 100644 index 0000000..9d644f9 --- /dev/null +++ b/source/workdir.c @@ -0,0 +1,140 @@ +#include "workdir.h" +#include "state.h" +#include +#include +#include +#include + +typedef struct { + int idx; + char cwd[1024]; + char **files; + int file_count; + 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; + +static void get_files(int windex); + +static bool is_dir(char* path) { + struct stat s; + if( stat(path, &s) == 0){ + return (s.st_mode & S_IFDIR); + }/*else error*/ + return false; +} + +void workdir_next(void) { + int index = state_get_focused_frame(); + //do nothing if at the end of the file list + if(Windows[index].idx < Windows[index].file_count){ + Windows[index].idx += 1; + int rows,cols; + getmaxyx(stdscr, rows,cols); + (void) cols; + if((TopBuffer+Windows[index].idx+BotBuffer) > rows) + Windows[index].top_index = Windows[index].idx-(rows-TopBuffer-BotBuffer); + } +} + +void workdir_prev(void) { + int index = state_get_focused_frame(); + //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; + } +} + +void workdir_cd(void) { + int windex = state_get_focused_frame(); + 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 workdir_ls(void) { + int windex = state_get_focused_frame(); + 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; + } +} + +static 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*/ +} diff --git a/source/workdir.h b/source/workdir.h new file mode 100644 index 0000000..6a6802f --- /dev/null +++ b/source/workdir.h @@ -0,0 +1,18 @@ +/** + @file workdir.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef WORKDIR_H +#define WORKDIR_H + +void workdir_prev(void); + +void workdir_next(void); + +void workdir_cd(void); + +void workdir_ls(void); + +#endif /* WORKDIR_H */ -- 2.49.0