From 85de46dfe5cee490b1dfa47998366ddc86fd6365 Mon Sep 17 00:00:00 2001 From: night0721 Date: Thu, 31 Oct 2024 23:19:40 +0000 Subject: [PATCH] Use ansi instead of ncurses and add directory preview --- Makefile | 6 +- ccc.c | 1917 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 1001 insertions(+), 922 deletions(-) diff --git a/Makefile b/Makefile index 64a3c01..31f1102 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,9 @@ PREFIX ?= /usr/local BINDIR = $(PREFIX)/bin MANDIR = $(PREFIX)/share/man/man1 -LDFLAGS != pkg-config --libs ncursesw -INCFLAGS != pkg-config --cflags ncursesw -CFLAGS = -O3 -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall $(INCFLAGS) +LDFLAGS != pkg-config --libs libsixel +INCFLAGS != pkg-config --cflags libsixel +CFLAGS = -O3 -march=native -mtune=native -pipe -g -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 $(INCFLAGS) SRC = ccc.c util.c file.c icons.c diff --git a/ccc.c b/ccc.c index 58717da..be2a48b 100644 --- a/ccc.c +++ b/ccc.c @@ -1,17 +1,19 @@ -#define _XOPEN_SOURCE 600 - #include #include +#include #include #include -#include /* directories etc. */ -#include +#include /* directories etc. */ #include #include #include -#include -#include -#include +#include +#include +#include +#include +#include + +#include #include "config.h" #include "file.h" @@ -19,21 +21,24 @@ #include "util.h" /* functions' definitions */ +void cleanup(); void show_help(); -void start_ccc(); +int read_key(); +void resize_dimensions(); char *check_trash_dir(); void change_dir(const char *buf, int selection, int ftype); void mkdir_p(const char *destdir); -void populate_files(const char *path, int ftype); +void populate_files(const char *path, int ftype, ArrayList **list); int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf); void add_file_stat(char *filename, char *path, int ftype); char *get_file_mode(mode_t mode); -void highlight_current_line(); +void list_files(); void show_file_content(); void edit_file(); void toggle_executable(); char *replace_home(char *str); int write_last_d(); +int sort_compare(const void *a, const void *b); void sort_files(); char *get_panel_string(char *prompt); void rename_file(); @@ -42,12 +47,13 @@ void create_dir(); void create_file(); void delete_files(); void wpprintw(const char *fmt, ...); -void init_windows(); -void draw_border_title(WINDOW *window, bool active); +void move_cursor(int row, int col); +int get_window_size(int *row, int *col); +void bprintf(const char *fmt, ...); /* global variables */ unsigned int focus = 0; -long current_selection = 0; +long sel_file = 0; bool file_picker = false; bool to_open_file = false; bool dirs_size = DIRS_SIZE; @@ -60,380 +66,379 @@ char *p_cwd; /* previous cwd */ int half_width; ArrayList *files; ArrayList *marked; -ArrayList *tmp1; -ArrayList *tmp2; -WINDOW *directory_border; -WINDOW *directory_content; -WINDOW *preview_border; -WINDOW *preview_content; -WINDOW *panel; +ArrayList *tmp1; /* temp store of dirs */ +ArrayList *tmp2; /* tmp store of files */ +int rows, cols; +struct termios oldt, newt; -unsigned long total_dir_size; +unsigned long total_dir_size = 0; -int main(int argc, char** argv) +int main(int argc, char **argv) { - if (argc == 3) { - if (strncmp(argv[2], "-p", 2) == 0) - file_picker = true; - } - if (argc <= 3 && argc != 1) { - if (strncmp(argv[1], "-h", 2) == 0) - die("Usage: ccc filename"); + if (argc == 3) { + if (strncmp(argv[2], "-p", 2) == 0) + file_picker = true; + } + if (argc <= 3 && argc != 1) { + if (strncmp(argv[1], "-h", 2) == 0) + die("Usage: ccc filename"); - struct stat st; - if (lstat(argv[1], &st)) { - perror("ccc"); - die("Error from lstat"); - } - /* chdir to directory from argument */ - if (S_ISDIR(st.st_mode) && chdir(argv[1])) { - perror("ccc"); - die("Error from chdir"); - } else if (S_ISREG(st.st_mode)) { - argv_cp = estrdup(argv[1]); - char *last_slash = strrchr(argv_cp, '/'); - *last_slash = '\0'; - if (chdir(argv[1])) { - perror("ccc"); - die("Error from chdir"); - } - to_open_file = true; - } - } + struct stat st; + if (lstat(argv[1], &st)) { + perror("ccc"); + die("Error from lstat"); + } + /* chdir to directory from argument */ + if (S_ISDIR(st.st_mode) && chdir(argv[1])) { + perror("ccc"); + die("Error from chdir"); + } else if (S_ISREG(st.st_mode)) { + argv_cp = estrdup(argv[1]); + char *last_slash = strrchr(argv_cp, '/'); + *last_slash = '\0'; + if (chdir(argv[1])) { + perror("ccc"); + die("Error from chdir"); + } + to_open_file = true; + } + } - /* check if it is interactive shell */ - if (!isatty(STDIN_FILENO)) - die("ccc: No tty detected. ccc requires an interactive shell to run.\n"); + /* check if it is interactive shell */ + if (!isatty(STDIN_FILENO)) + die("ccc: No tty detected. ccc requires an interactive shell to run.\n"); - /* initialize screen, don't print special chars, - * make ctrl + c work, don't show cursor - * enable arrow keys */ - setlocale(LC_ALL, ""); - initscr(); - noecho(); - cbreak(); - curs_set(0); - keypad(stdscr, TRUE); + /* initialize screen, don't print special chars, + * make ctrl + c work, don't show cursor + * enable arrow keys */ + bprintf("\033[?1049h\033[2J\033[?25l"); + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + /* Disable canonical mode and echo */ + newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + /* newt.c_oflag &= ~(OPOST); */ + newt.c_cflag |= (CS8); + newt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); + newt.c_cc[VMIN] = 0; + newt.c_cc[VTIME] = 1; + tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt); - /* check terminal has colors */ - if (!has_colors()) { - endwin(); - die("ccc: Color is not supported in your terminal.\n"); - } else { - use_default_colors(); - start_color(); - } + /* init files and marked arrays */ + marked = arraylist_init(100); + hashtable_init(); - /* colors */ - init_pair(1, COLOR_BLACK, -1); /* */ - init_pair(2, COLOR_RED, -1); /* */ - init_pair(3, COLOR_GREEN, -1); /* LNK */ - init_pair(4, COLOR_YELLOW, -1); /* BLK */ - init_pair(5, COLOR_BLUE, -1); /* DIR */ - init_pair(6, COLOR_MAGENTA, -1); /* */ - init_pair(7, COLOR_CYAN, -1); /* MARKED FILES */ - init_pair(8, COLOR_WHITE, -1); /* REG */ + cwd = memalloc(PATH_MAX); + getcwd(cwd, PATH_MAX); + p_cwd = memalloc(PATH_MAX); + p_cwd[0] = '\0'; + get_window_size(&rows, &cols); - /* init files and marked arrays */ - marked = arraylist_init(100); - hashtable_init(); + half_width = cols / 2 + WINDOW_OFFSET; + populate_files(cwd, 0, &files); - cwd = memalloc(PATH_MAX * sizeof(char)); - getcwd(cwd, PATH_MAX); - p_cwd = memalloc(PATH_MAX * sizeof(char)); - start_ccc(); - - populate_files(cwd, 0); - if (to_open_file) { - current_selection = arraylist_search(files, argv_cp, true); - highlight_current_line(); - } + if (to_open_file) { + sel_file = arraylist_search(files, argv_cp, true); + list_files(); + } - int ch, ch2; - while (1) { - if (COLS < 80 || LINES < 24) { - endwin(); - die("ccc: Terminal size needs to be at least 80x24\n"); - } - ch = getch(); - switch (ch) { - /* quit */ - case 'q': - if (write_last_d() == -1) { - /* prompt user so error message can be shown to user */ - getch(); - } - goto cleanup; + int ch, ch2; + int run = 1; + while (run) { + if (cols < 80 || rows < 24) { + cleanup(); + die("ccc: Terminal size needs to be at least 80x24"); + } + ch = read_key(); + switch (ch) { + /* quit */ + case 'q': + if (write_last_d() == -1) { + /* prompt user so error message can be shown to user */ + read_key(); + } + cleanup(); + run = 0; + break; - /* reload using z */ - case 'z': - change_dir(cwd, 0, 0); - break; + /* reload using z */ + case 'z': + change_dir(cwd, 0, 0); + break; - /* go back by backspace or h or left arrow */ - case BACKSPACE: /* PASSTHROUGH */ - case LEFT: /* PASSTHROUGH */ - case 'h':; - /* get parent directory */ - strcpy(p_cwd, cwd); - char *last_slash = strrchr(cwd, '/'); - if (last_slash != NULL) { - if (last_slash == cwd) { - change_dir("/", 0, 0); - break; - } - *last_slash = '\0'; - change_dir(cwd, 0, 0); - } - break; + /* go back by backspace or h or left arrow */ + case BACKSPACE: /* PASSTHROUGH */ + case LEFT: /* PASSTHROUGH */ + case 'h':; + /* get parent directory */ + strcpy(p_cwd, cwd); + char *last_slash = strrchr(cwd, '/'); + if (last_slash != NULL) { + if (last_slash == cwd) { + change_dir("/", 0, 0); + break; + } + *last_slash = '\0'; + change_dir(cwd, 0, 0); + } + break; - /* enter directory/open a file using enter or l or right arrow */ - case ENTER: /* PASSTHROUGH */ - case RIGHT: /* PASSTHROUGH */ - case 'l': - strcpy(p_cwd, cwd); - file c_file = files->items[current_selection]; - - /* check if it is directory or a regular file */ - if (strncmp(c_file.type, "DIR", 3) == 0) { - /* change cwd to directory */ - change_dir(c_file.path, 0, 0); - } else if (strncmp(c_file.type, "REG", 3) == 0) { - /* write opened file to a file for file pickers */ - if (file_picker) { - char *opened_file_path = memalloc(PATH_MAX * sizeof(char)); - strcpy(opened_file_path, "~/.cache/ccc/opened_file"); - opened_file_path = replace_home(opened_file_path); - FILE *opened_file = fopen(opened_file_path, "w+"); - fprintf(opened_file, "%s\n", c_file.path); - fclose(opened_file); - endwin(); - return 0; - } else { - edit_file(); - } - } - break; + /* enter directory/open a file using enter or l or right arrow */ + case ENTER: /* PASSTHROUGH */ + case RIGHT: /* PASSTHROUGH */ + case 'l': + strcpy(p_cwd, cwd); + file c_file = files->items[sel_file]; - /* jump up (ctrl u) */ - case CTRLU: - if ((current_selection - JUMP_NUM) > 0) - current_selection -= JUMP_NUM; - else - current_selection = 0; + /* Check if it is directory or a regular file */ + if (c_file.type == DRY) { + /* Change cwd to directory */ + change_dir(c_file.path, 0, 0); + } else if (c_file.type == REG) { + /* Write opened file to a file for file pickers */ + if (file_picker) { + char *opened_file_path = memalloc(PATH_MAX); + strcpy(opened_file_path, "~/.cache/ccc/opened_file"); + opened_file_path = replace_home(opened_file_path); + FILE *opened_file = fopen(opened_file_path, "w+"); + fprintf(opened_file, "%s\n", c_file.path); + fclose(opened_file); + cleanup(); + run = 0; + } else { + edit_file(); + } + } + break; - highlight_current_line(); - break; + /* jump up (ctrl u) */ + case CTRLU: + if ((sel_file - JUMP_NUM) > 0) + sel_file -= JUMP_NUM; + else + sel_file = 0; - /* go up by k or up arrow */ - case UP: /* PASSTHROUGH */ - case 'k': - if (current_selection > 0) - current_selection--; + list_files(); + break; - highlight_current_line(); - break; + /* go up by k or up arrow */ + case UP: /* PASSTHROUGH */ + case 'k': + if (sel_file > 0) + sel_file--; - /* jump down (ctrl d) */ - case CTRLD: - if ((current_selection + JUMP_NUM) < (files->length - 1)) - current_selection += JUMP_NUM; - else - current_selection = (files->length - 1); + list_files(); + break; - highlight_current_line(); - break; + /* jump down (ctrl d) */ + case CTRLD: + if ((sel_file + JUMP_NUM) < (files->length - 1)) + sel_file += JUMP_NUM; + else + sel_file = (files->length - 1); - /* go down by j or down arrow */ - case DOWN: /* PASSTHROUGH */ - case 'j': - if (current_selection < (files->length - 1)) - current_selection++; + list_files(); + break; - highlight_current_line(); - break; + /* go down by j or down arrow */ + case DOWN: /* PASSTHROUGH */ + case 'j': + if (sel_file < (files->length - 1)) + sel_file++; - /* jump to the bottom */ - case 'G': - current_selection = (files->length - 1); - highlight_current_line(); - break; + list_files(); + break; - /* jump to the top */ - case 'g': - ch2 = getch(); - switch (ch2) { - case 'g': - current_selection = 0; - highlight_current_line(); - break; - default: - break; - } - break; + /* jump to the bottom */ + case 'G': + sel_file = (files->length - 1); + list_files(); + break; - /* '~' go to $HOME */ - case TILDE:; - char *home = getenv("HOME"); - if (home == NULL) { - wpprintw("$HOME not defined"); - } else { - change_dir(home, 0, 0); - } - break; + /* jump to the top */ + case 'g': + ch2 = read_key(); + switch (ch2) { + case 'g': + sel_file = 0; + list_files(); + break; + default: + break; + } + break; - /* go to the trash dir */ - case 't':; - char *trash_dir = check_trash_dir(); - if (trash_dir != NULL) { - strcpy(p_cwd, cwd); - change_dir(trash_dir, 0, 0); - } - break; + /* '~' go to $HOME */ + case TILDE:; + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME not defined"); + } else { + change_dir(home, 0, 0); + } + break; - /* sort files */ - case 'u': - sort_files(); - break; + /* go to the trash dir */ + case 't':; + char *trash_dir = check_trash_dir(); + if (trash_dir != NULL) { + strcpy(p_cwd, cwd); + change_dir(trash_dir, 0, 0); + } + break; - /* show directories' sizes */ - case 'A': - dirs_size = !dirs_size; - change_dir(cwd, 0, 0); - break; + /* sort files */ + case 'u': + sort_files(); + break; - /* go to previous dir */ - case '-': - change_dir(p_cwd, 0, 0); - break; + /* show directories' sizes */ + case 'A': + dirs_size = !dirs_size; + change_dir(cwd, 0, 0); + break; - /* show help */ - case '?': - show_help(); - break; - - /* toggle hidden files */ - case '.': - show_hidden = !show_hidden; - change_dir(cwd, 0, 0); - break; + /* go to previous dir */ + case '-': + if (strlen(p_cwd) != 0) + change_dir(p_cwd, 0, 0); + break; - /* toggle file details */ - case 'i': - file_details = !file_details; - change_dir(cwd, 0, 0); - break; + /* show help */ + case '?': + show_help(); + break; - case 'w': - show_icons = !show_icons; - change_dir(cwd, 0, 0); - break; + /* toggle hidden files */ + case '.': + show_hidden = !show_hidden; + change_dir(cwd, 0, 0); + break; - case 'f': - create_file(); - break; + /* toggle file details */ + case 'i': + file_details = !file_details; + change_dir(cwd, 0, 0); + break; - case 'n': - create_dir(); - break; + case 'w': + show_icons = !show_icons; + change_dir(cwd, 0, 0); + break; - case 'r': - rename_file(); - break; + case 'f': + create_file(); + break; - case ':': - goto_dir(); - break; - - case 'X': - toggle_executable(); - break; + case 'n': + create_dir(); + break; - /* mark one file */ - case SPACE: - add_file_stat(files->items[current_selection].name, files->items[current_selection].path, 1); - highlight_current_line(); - break; + case 'r': + rename_file(); + break; - /* mark all files in directory */ - case 'a': - change_dir(cwd, current_selection, 2); /* reload current dir */ - break; + case ':': + goto_dir(); + break; - /* mark actions: */ - /* delete */ - case 'd': - delete_files(); - break; + case 'X': + toggle_executable(); + break; - /* move */ - case 'm': - if (marked->length) { - ; - } - break; + /* mark one file */ + case SPACE: + add_file_stat(files->items[sel_file].name, files->items[sel_file].path, 1); + list_files(); + break; - /* copy */ - case 'c': - if (marked->length) { - ; - } - break; + /* mark all files in directory */ + case 'a': + change_dir(cwd, sel_file, 2); /* reload current dir */ + break; - /* symbolic link */ - case 's': - if (marked->length) { - ; - } - break; + /* mark actions: */ + /* delete */ + case 'd': + delete_files(); + break; - /* bulk rename */ - case 'b': - if (marked->length) { - ; - } - break; - - /* escape */ - case ESC: - break; + /* move */ + case 'm': + if (marked->length) { + ; + } + break; - case KEY_RESIZE: - delwin(directory_border); - delwin(directory_content); - delwin(preview_border); - delwin(preview_content); - delwin(panel); - endwin(); - start_ccc(); - highlight_current_line(); - break; - default: - break; - } - } -cleanup: - free(argv_cp); - arraylist_free(files); - arraylist_free(marked); - endwin(); - return 0; + /* copy */ + case 'c': + if (marked->length) { + ; + } + break; + + /* symbolic link */ + case 's': + if (marked->length) { + ; + } + break; + + /* bulk rename */ + case 'b': + if (marked->length) { + ; + } + break; + + /* escape */ + case ESC: + break; + + case RESIZE: + half_width = cols / 2 + WINDOW_OFFSET; + list_files(); + break; + default: + break; + } + } + + return 0; +} + +void cleanup() +{ + if (argv_cp != NULL) + free(argv_cp); + if (files->length != 0) { + arraylist_free(files); + } + if (marked->length != 0) { + arraylist_free(marked); + } + /* Restore old terminal settings */ + tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt); + bprintf("\033[2J\033[?1049l\033[?25h"); } void show_help() { - wclear(directory_content); - wclear(preview_content); - wprintw(directory_content,"h: go to parent dir\nj: scroll down\nk: scroll up\nl: go to child dir\n\nleft: go to parent dir\ndown: scroll down\nup: scroll up\nright: go to child dir\n\nenter: go to child dir/open file\nbackspace: go to parent dir\n\ngg: go to top\nG: go to bottom\n\nctrl+u: jump up\nctrl+d: jump down\n\nt: go to trash dir\n~: go to home dir\n-: go to previous dir\nz: refresh current dir\n:: go to a directory by typing\nu: sort files\n\n.: toggle hidden files\ni: toggle file details\nX: toggle executable\n\nA: show directory disk usage/block size\n\nf: new file\nn: new dir\nr: rename\n\nspace: mark file\na: mark all files in directory\nd: trash\n\n?: show help\nq: exit with last dir written to file\nctrl+c exit without writing last dir"); - wpprintw("Visit https://github.com/night0721/ccc or use 'man ccc' for help"); - wrefresh(directory_content); - wrefresh(preview_content); -} - -void start_ccc() -{ - half_width = COLS / 2; - init_windows(); + bprintf("\033[2J"); + move_cursor(1, 1); + bprintf("h: go to parent dir\nj: scroll down\nk: scroll up\n" + "l: go to child dir\n\nleft: go to parent dir\ndown: scroll down\n" + "up: scroll up\nright: go to child dir\n\n" + "enter: go to child dir/open file\nbackspace: go to parent dir\n\n" + "gg: go to top\nG: go to bottom\n\nctrl+u: jump up\nctrl+d: jump down\n\n" + "t: go to trash dir\n~: go to home dir\n-: go to previous dir\n" + "z: refresh current dir\n:: go to a directory by typing\nu: sort files\n\n" + ".: toggle hidden files\ni: toggle file details\nX: toggle executable\n\n" + "A: show directory disk usage/block size\n\nf: new file\nn: new dir\n" + "r: rename\n\nspace: mark file\na: mark all files in directory\nd: trash" + "\n\n?: show help\nq: exit with last dir written to file\n" + "ctrl+c exit without writing last dir" + ); + wpprintw("Visit https://github.com/night0721/ccc or use 'man ccc' for help"); } /* @@ -441,32 +446,32 @@ void start_ccc() */ char *check_trash_dir() { - char *path = memalloc(PATH_MAX * sizeof(char)); + char *path = memalloc(PATH_MAX); - char *trash_dir; - #ifdef TRASH_DIR - trash_dir = TRASH_DIR; - strcpy(path, trash_dir); - #endif + char *trash_dir; +#ifdef TRASH_DIR + trash_dir = TRASH_DIR; + strcpy(path, trash_dir); +#endif - /* check if there is trash_dir */ - if (trash_dir == NULL) { - wpprintw("Trash directory not defined"); - return NULL; - } else { - /* if trash_dir has ~ then make it $HOME */ - /* use path as trash_dir */ - if (trash_dir[0] == '~') { - path = replace_home(path); - } + /* check if there is trash_dir */ + if (trash_dir == NULL) { + wpprintw("Trash directory not defined"); + return NULL; + } else { + /* if trash_dir has ~ then make it $HOME */ + /* use path as trash_dir */ + if (trash_dir[0] == '~') { + path = replace_home(path); + } - /* if has access to trash_dir */ - if (access(path, F_OK) != 0) { - /* create the directory with 755 permissions if it doesn't exist */ - mkdir_p(path); - } - } - return path; + /* if has access to trash_dir */ + if (access(path, F_OK) != 0) { + /* create the directory with 755 permissions if it doesn't exist */ + mkdir_p(path); + } + } + return path; } /* @@ -474,13 +479,13 @@ char *check_trash_dir() */ void change_dir(const char *buf, int selection, int ftype) { - if (cwd != buf) - strcpy(cwd, buf); - if (ftype == 0) - arraylist_free(files); - chdir(cwd); - current_selection = selection; - populate_files(cwd, ftype); + if (cwd != buf) + strcpy(cwd, buf); + if (ftype == 0) + arraylist_free(files); + chdir(cwd); + sel_file = selection; + populate_files(cwd, ftype, &files); } /* @@ -489,103 +494,102 @@ void change_dir(const char *buf, int selection, int ftype) */ void mkdir_p(const char *destdir) { - char *path = memalloc(PATH_MAX * sizeof(char)); - char dir_path[PATH_MAX] = ""; + char *path = memalloc(PATH_MAX); + char dir_path[PATH_MAX] = ""; - if (destdir[0] == '~') { - char *home = getenv("HOME"); - if (home == NULL) { - wpprintw("$HOME not defined"); - return; - } - /* replace ~ with home */ - snprintf(path, PATH_MAX, "%s%s", home, destdir + 1); - } else { - strcpy(path, destdir); - } + if (destdir[0] == '~') { + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME not defined"); + return; + } + /* replace ~ with home */ + snprintf(path, PATH_MAX, "%s%s", home, destdir + 1); + } else { + strcpy(path, destdir); + } - /* fix first / not appearing in the string */ - if (path[0] == '/') - strcat(dir_path, "/"); + /* fix first / not appearing in the string */ + if (path[0] == '/') + dir_path[0] = '/'; - char *token = strtok(path, "/"); - while (token != NULL) { - strcat(dir_path, token); - strcat(dir_path, "/"); + char *token = strtok(path, "/"); + while (token != NULL) { + strcat(dir_path, token); + strcat(dir_path, "/"); - if (mkdir(dir_path, 0755) == -1) { - struct stat st; - if (stat(dir_path, &st) == 0 && S_ISDIR(st.st_mode)) { - /* Directory already exists, continue to the next dir */ - token = strtok(NULL, "/"); - continue; - } - - wpprintw("mkdir failed: %s\n", strerror(errno)); - free(path); - return; - } - token = strtok(NULL, "/"); - } + if (mkdir(dir_path, 0755) == -1) { + struct stat st; + if (stat(dir_path, &st) == 0 && S_ISDIR(st.st_mode)) { + /* Directory already exists, continue to the next dir */ + token = strtok(NULL, "/"); + continue; + } - free(path); - return; + wpprintw("mkdir failed: %s", strerror(errno)); + free(path); + return; + } + token = strtok(NULL, "/"); + } + + free(path); + return; } /* * Read the provided directory and add all files in directory to an Arraylist * ftype: normal files = 0, marked = 1, marking ALL = 2 */ -void populate_files(const char *path, int ftype) +void populate_files(const char *path, int ftype, ArrayList **list) { - DIR *dp; - struct dirent *ep; + DIR *dp; + struct dirent *ep; - if ((dp = opendir(path)) != NULL) { - /* clear directory window to ready for printing */ - wclear(directory_content); - if (ftype == 0) { - tmp1 = arraylist_init(10); - tmp2 = arraylist_init(10); - } + if ((dp = opendir(path)) != NULL) { + if (ftype == 0) { + tmp1 = arraylist_init(10); + tmp2 = arraylist_init(10); + } - while ((ep = readdir(dp)) != NULL) { - char *filename = estrdup(ep->d_name); + while ((ep = readdir(dp)) != NULL) { + char *filename = estrdup(ep->d_name); - /* use strncmp to filter out dotfiles */ - if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2)) || (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) { - /* construct full file path */ - char *path = memalloc((strlen(cwd) + strlen(filename) + 2) * sizeof(char)); - strcpy(path, cwd); - strcat(path, "/"); - strcat(path, filename); /* add filename */ - - add_file_stat(filename, path, ftype); - } - else free(filename); - } - if (ftype == 0) { - files = arraylist_init(tmp1->length + tmp2->length); - files->length = tmp1->length + tmp2->length; - memcpy(files->items, tmp1->items, tmp1->length * sizeof(file)); - memcpy(files->items + tmp1->length, tmp2->items, tmp2->length * sizeof(file)); - free(tmp1->items); - free(tmp2->items); - free(tmp1); - free(tmp2); - } - closedir(dp); - wrefresh(directory_content); - highlight_current_line(); - } else { - wpprintw("stat failed: %s\n", strerror(errno)); - } + /* Filter out dotfiles */ + if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2)) + || (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) { + /* Construct full file path */ + int fpath_len = strlen(path) + strlen(filename) + 2; + char *fpath = memalloc(fpath_len); + snprintf(fpath, fpath_len, "%s/%s", path, filename); + add_file_stat(filename, fpath, ftype); + } + else free(filename); + } + if (ftype == 0) { + *list = arraylist_init(tmp1->length + tmp2->length); + (*list)->length = tmp1->length + tmp2->length; + qsort(tmp1->items, tmp1->length, sizeof(file), sort_compare); + qsort(tmp2->items, tmp2->length, sizeof(file), sort_compare); + memcpy((*list)->items, tmp1->items, tmp1->length * sizeof(file)); + memcpy((*list)->items + tmp1->length, tmp2->items, tmp2->length * sizeof(file)); + free(tmp1->items); + free(tmp2->items); + free(tmp1); + free(tmp2); + } + closedir(dp); + if (list == &files) + list_files(); + } else { + wpprintw("stat failed: %s", strerror(errno)); + } } int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - total_dir_size += sb->st_size; - return 0; + total_dir_size += sb->st_size; + return 0; } /* @@ -595,125 +599,121 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s */ void add_file_stat(char *filename, char *path, int ftype) { - struct stat file_stat; - if (stat(path, &file_stat) == -1) { - /* can't be triggered? */ - if (errno == EACCES) - arraylist_add(files, filename, path, NULL, NULL, NULL, 8, false, false); - } - - /* get file type and color, 4 chars for the type and icon */ - size_t type_size = 4 * sizeof(char); - size_t icon_size = 2 * sizeof(wchar_t); + struct stat file_stat; + if (stat(path, &file_stat) == -1) { + /* can't be triggered? */ + if (errno == EACCES) + arraylist_add(files, filename, path, NULL, REG, NULL, WHITE, false, false); + } - char *type = memalloc(type_size); - wchar_t *icon_str = memalloc(icon_size); + int type; + char *icon_str = memalloc(8); - filename[strlen(filename)] = '\0'; - /* handle file without extension - * ext is the extension if . exist in filename - * otherwise is nothing and handled through tenery operator */ - char *ext = strrchr(filename, '.'); - if (ext != NULL) { - ext += 1; - } - /* add file extension */ - icon *ext_icon = hashtable_search(ext != NULL ? ext : filename); - if (ext_icon == NULL) - wcsncpy(icon_str, L"", 2); - else - wcsncpy(icon_str, ext_icon->icon, 2); + filename[strlen(filename)] = '\0'; + /* handle file without extension + * ext is the extension if . exist in filename + * otherwise is nothing and handled through tenery operator */ + char *ext = strrchr(filename, '.'); + if (ext != NULL) { + ext += 1; + } + /* add file extension */ + icon *ext_icon = hashtable_search(ext != NULL ? ext : filename); + if (ext_icon == NULL) + memcpy(icon_str, "", 4); + else + memcpy(icon_str, ext_icon->icon, 5); - int color; + char color[12]; - if (S_ISDIR(file_stat.st_mode)) { - strncpy(type, "DIR", 4); /* directory type */ - color = 5; /* blue color */ - wcsncpy(icon_str, L"󰉋", 2); - } else if (S_ISREG(file_stat.st_mode)) { - strncpy(type, "REG", 4); /* regular file */ - color = 8; /* white color */ - } else if (S_ISLNK(file_stat.st_mode)) { - strncpy(type, "LNK", 4); /* symbolic link */ - color = 3; /* green color */ - } else if (S_ISCHR(file_stat.st_mode)) { - strncpy(type, "CHR", 4); /* character device */ - color = 8; /* white color */ - } else if (S_ISSOCK(file_stat.st_mode)) { - strncpy(type, "SOC", 4); /* socket */ - color = 8; /* white color */ - } else if (S_ISBLK(file_stat.st_mode)) { - strncpy(type, "BLK", 4); /* block device */ - color = 4; /* yellow color */ - } else if (S_ISFIFO(file_stat.st_mode)) { - strncpy(type, "FIF", 4); /* FIFO */ - color = 8; /* white color */ - } else { - color = 8; /* white color */ - } + if (S_ISDIR(file_stat.st_mode)) { + type = DRY; /* dir */ + strncpy(color, BLUE, 12); + memcpy(icon_str, "󰉋", 5); + } else if (S_ISREG(file_stat.st_mode)) { + type = REG; /* regular file */ + strncpy(color, WHITE, 12); + } else if (S_ISLNK(file_stat.st_mode)) { + type = LNK; /* symbolic link */ + strncpy(color, GREEN, 12); + } else if (S_ISCHR(file_stat.st_mode)) { + type = CHR; /* character device */ + strncpy(color, YELLOW, 12); + } else if (S_ISSOCK(file_stat.st_mode)) { + type = SOC; /* socket */ + strncpy(color, PINK, 12); + } else if (S_ISBLK(file_stat.st_mode)) { + type = BLK; /* block device */ + strncpy(color, YELLOW, 12); + } else if (S_ISFIFO(file_stat.st_mode)) { + type = FIF; /* FIFO */ + strncpy(color, PINK, 12); + } else { + strncpy(color, WHITE, 12); + } - /* if file is to be marked */ - if (ftype == 1 || ftype == 2) { - /* force if user is marking all files */ - bool force = ftype == 2 ? true : false; - arraylist_add(marked, filename, path, NULL, type, icon_str, 8, true, force); - /* free type and return without allocating more stuff */ - return; - } + /* If file is to be marked */ + if (ftype == 1 || ftype == 2) { + /* Force if user is marking all files */ + bool force = ftype == 2 ? true : false; + arraylist_add(marked, filename, path, NULL, type, icon_str, WHITE, true, force); + /* free type and return without allocating more stuff */ + return; + } - /* get last modified time */ - size_t time_size = 17 * sizeof(char); - char *time = memalloc(time_size); - /* format last modified time to a string */ - strftime(time, time_size, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); - - /* get file size */ - double bytes = file_stat.st_size; - - if (dirs_size) { - /* dirs_size is true, so calculate disk usage */ - if (S_ISDIR(file_stat.st_mode)) { - /* at most 15 fd opened */ - total_dir_size = 0; - nftw(path, &get_directory_size, 15, FTW_PHYS); - bytes = total_dir_size; - } - } - /* 4 before decimal + 1 dot + DECIMAL_PLACES (after decimal) + - unit length (1 for K, 3 for KiB, taking units[1] as B never changes) + 1 space + 1 null */ - int size_size = 4 + 1 + DECIMAL_PLACES + strlen(units[1]) + 1 + 1; - char *size = memalloc(size_size * sizeof(char)); - int unit = 0; - while (bytes > 1024) { - bytes /= 1024; - unit++; - } - /* display sizes and check if there are decimal places */ - if (bytes == (unsigned int) bytes) { - sprintf(size, "%d%s", (unsigned int) bytes, units[unit]); - } else { - sprintf(size, "%.*f%s", DECIMAL_PLACES, bytes, units[unit]); - } - /* get file mode string */ - char *mode_str = get_file_mode(file_stat.st_mode); - if (mode_str[0] == '-' && (mode_str[3] == 'x' || mode_str[6] == 'x' || mode_str[9] == 'x')) { - - } + /* get last modified time */ + size_t time_size = 17; + char *time = memalloc(time_size); + /* Format last modified time to a string */ + strftime(time, time_size, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); - /* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */ - size_t stat_size = 11 * sizeof(char) + time_size + size_size + 3 * sizeof(char); - char *total_stat = memalloc(stat_size); - snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size); + /* get file size */ + double bytes = file_stat.st_size; - /* DIR if color is 5 */ - if (color == 5) - arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, false, false); - else - arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, false, false); + if (dirs_size) { + /* dirs_size is true, so calculate disk usage */ + if (S_ISDIR(file_stat.st_mode)) { + /* at most 15 fd opened */ + total_dir_size = 0; + nftw(path, &get_directory_size, 15, FTW_PHYS); + bytes = total_dir_size; + } + } + /* 4 before decimal + 1 dot + DECIMAL_PLACES (after decimal) + + unit length (1 for K, 3 for KiB, taking units[1] as B never changes) + 1 space + 1 null */ + int size_size = 4 + 1 + DECIMAL_PLACES + strlen(units[1]) + 1 + 1; + char *size = memalloc(size_size); + int unit = 0; + while (bytes > 1024) { + bytes /= 1024; + unit++; + } + /* display sizes and check if there are decimal places */ + if (bytes == (unsigned int) bytes) { + sprintf(size, "%d%s", (unsigned int) bytes, units[unit]); + } else { + sprintf(size, "%.*f%s", DECIMAL_PLACES, bytes, units[unit]); + } + /* get file mode string */ + char *mode_str = get_file_mode(file_stat.st_mode); + if (mode_str[0] == '-' && (mode_str[3] == 'x' || mode_str[6] == 'x' || mode_str[9] == 'x')) { + strncpy(color, GREEN, 12); + } - free(time); - free(size); - free(mode_str); + /* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */ + size_t stat_size = 11 + time_size + size_size + 3; + char *total_stat = memalloc(stat_size); + snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size); + + /* DIR if color is blue */ + if (strncmp(color, BLUE, 12) == 0) + arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, false, false); + else + arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, false, false); + + free(time); + free(size); + free(mode_str); } /* @@ -722,112 +722,85 @@ void add_file_stat(char *filename, char *path, int ftype) */ char *get_file_mode(mode_t mode) { - char *mode_str = memalloc(11 * sizeof(char)); - mode_str[0] = S_ISDIR(mode) ? 'd' : '-'; /* Check if it's a directory */ - mode_str[1] = (mode & S_IRUSR) ? 'r' : '-'; - mode_str[2] = (mode & S_IWUSR) ? 'w' : '-'; - mode_str[3] = (mode & S_IXUSR) ? 'x' : '-'; - mode_str[4] = (mode & S_IRGRP) ? 'r' : '-'; - mode_str[5] = (mode & S_IWGRP) ? 'w' : '-'; - mode_str[6] = (mode & S_IXGRP) ? 'x' : '-'; - mode_str[7] = (mode & S_IROTH) ? 'r' : '-'; - mode_str[8] = (mode & S_IWOTH) ? 'w' : '-'; - mode_str[9] = (mode & S_IXOTH) ? 'x' : '-'; - mode_str[10] = '\0'; - return mode_str; + char *mode_str = memalloc(11); + mode_str[0] = S_ISDIR(mode) ? 'd' : '-'; + mode_str[1] = (mode & S_IRUSR) ? 'r' : '-'; + mode_str[2] = (mode & S_IWUSR) ? 'w' : '-'; + mode_str[3] = (mode & S_IXUSR) ? 'x' : '-'; + mode_str[4] = (mode & S_IRGRP) ? 'r' : '-'; + mode_str[5] = (mode & S_IWGRP) ? 'w' : '-'; + mode_str[6] = (mode & S_IXGRP) ? 'x' : '-'; + mode_str[7] = (mode & S_IROTH) ? 'r' : '-'; + mode_str[8] = (mode & S_IWOTH) ? 'w' : '-'; + mode_str[9] = (mode & S_IXOTH) ? 'x' : '-'; + mode_str[10] = '\0'; + return mode_str; } /* - * Highlight current line by reversing the color + * Print files in directory window */ -void highlight_current_line() +void list_files() { - #if DRAW_BORDERS - draw_border_title(directory_border, true); - #endif + long overflow = 0; + if (sel_file > rows - 4) { + /* overflown */ + overflow = sel_file - (rows - 4); + } - long overflow = 0; - if (current_selection > LINES - 4) { - /* overflown */ - overflow = current_selection - (LINES - 4); - } + /* calculate range of files to show */ + long range = files->length; + /* not highlight if no files in directory */ + if (range == 0 && errno == 0) { + move_cursor(1, half_width); + printf("empty directory"); + return; + } - /* calculate range of files to show */ - long range = files->length; - /* not highlight if no files in directory */ - if (range == 0 && errno == 0) { - #if DRAW_PREVIEW - wprintw(preview_content, "empty directory"); - wrefresh(preview_content); - #endif - return; - } + if (range > rows - 3) { + /* if there are more files than rows available to display + * shrink range to avaiable rows to display with + * overflow to keep the number of iterations to be constant */ + range = rows - 3 + overflow; + } - if (range > LINES - 3) { - /* if there are more files than lines available to display - * shrink range to avaiable lines to display with - * overflow to keep the number of iterations to be constant */ - range = LINES - 3 + overflow; - } - - wclear(directory_content); - long line_count = 0; - for (long i = overflow; i < range; i++) { - if ((overflow == 0 && i == current_selection) || (overflow != 0 && i == current_selection)) { - wattron(directory_content, A_REVERSE); + move_cursor(1, 1); + bprintf("\033[2J"); - /* check for marked files */ - long num_marked = marked->length; - if (num_marked > 0) { - /* Determine length of formatted string */ - int m_len = snprintf(NULL, 0, "[%ld] selected", num_marked); - char *selected = memalloc((m_len + 1) * sizeof(char)); + long line_count = 0; + for (long i = overflow; i < range; i++) { + int is_selected = 0; + if ((overflow == 0 && i == sel_file) || + (overflow != 0 && i == sel_file)) { + is_selected = 1; + /* check for marked files */ + long num_marked = marked->length; + if (num_marked > 0) { + /* Determine length of formatted string */ + int m_len = snprintf(NULL, 0, "[%ld] selected", num_marked); + char *selected = memalloc(m_len + 1); - snprintf(selected, m_len + 1, "[%ld] selected", num_marked); - wpprintw("(%ld/%ld) %s %s", current_selection + 1, files->length, selected, cwd); - } else { - wpprintw("(%ld/%ld) %s", current_selection + 1, files->length, cwd); - } - } - /* print the actual filename and stats */ - char *line = get_line(files, i, file_details, show_icons); - int color = files->items[i].color; - /* check is file marked for action */ - bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1; - if (is_marked) { - /* show file is selected */ - wattron(directory_content, COLOR_PAIR(7)); - } else { - /* print the whole directory with default colors */ - wattron(directory_content, COLOR_PAIR(color)); - } - - if (overflow > 0) - mvwprintw(directory_content, line_count, 0, "%s", line); - else - mvwprintw(directory_content, i, 0, "%s", line); + snprintf(selected, m_len + 1, "[%ld] selected", num_marked); + wpprintw("(%ld/%ld) %s %s", sel_file + 1, files->length, selected, cwd); + } else { + wpprintw("(%ld/%ld) %s", sel_file + 1, files->length, cwd); + } + } + /* print the actual filename and stats */ + char *line = get_line(files, i, file_details, show_icons); + char *color = files->items[i].color; + /* check is file marked for action */ + bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1; + move_cursor(i + 1, 1); + bprintf("\033[38;2;0;0;0m\033[%s;2;%sm%s\033[0m\n", + is_selected ? "48" : "38", is_marked ? GREEN : color, line); - if (is_marked) { - wattroff(directory_content, COLOR_PAIR(7)); - } else { - wattroff(directory_content, COLOR_PAIR(color)); - } + free(line); + line_count++; + } - wattroff(directory_content, A_REVERSE); - free(line); - line_count++; - } - - wrefresh(directory_content); - wrefresh(panel); - /* show file content every time cursor changes */ - #if DRAW_PREVIEW - show_file_content(); - #endif - #if DRAW_BORDERS - draw_border_title(preview_border, true); - #endif - wrefresh(preview_content); + /* show file content every time cursor changes */ + show_file_content(); } /* @@ -835,43 +808,100 @@ void highlight_current_line() */ void show_file_content() { - file current_file = files->items[current_selection]; + file current_file = files->items[sel_file]; - wclear(preview_content); - if (strncmp(current_file.type, "DIR", 3) == 0) - /* Print dir content */ - return; + move_cursor(1, half_width); + if (current_file.type == DRY) { + ArrayList *files_visit; + populate_files(current_file.name, 0, &files_visit); + for (long i = 0; i < files_visit->length; i++) { + char *line = get_line(files_visit, i, false, show_icons); + char *color = files_visit->items[i].color; + move_cursor(i + 1, half_width); + bprintf("\033[K\033[38;2;%sm%s\033[0m\n", color, line); + } + arraylist_free(files_visit); + return; + } + FILE *file = fopen(current_file.path, "r"); + if (file == NULL) { + bprintf("Unable to read %s", current_file.name); + return; + } + if (strstr(current_file.name, ".jpg") == NULL && strstr(current_file.name, ".png") == NULL) { + int c; + /* Check if its binary */ + while ((c = fgetc(file)) != EOF) { + if (c == '\0') { + bprintf("binary"); + return; + } + } + } + int pipe_fd[2]; + if (pipe(pipe_fd) == -1) { + perror("pipe"); + return; + } + int pid = fork(); + if (pid == 0) { + /* Child */ + /* + close(pipe_fd[0]); + dup2(pipe_fd[1], STDOUT_FILENO); + dup2(pipe_fd[1], STDERR_FILENO); + close(pipe_fd[1]); + */ + move_cursor(1, half_width); + if (strstr(current_file.name, ".jpg") || strstr(current_file.name, ".png")) { + sixel_encoder_t *encoder = NULL; + sixel_encoder_new(&encoder, NULL); + /* Should be enough for most terminal */ + char width[5]; + snprintf(width, 5, "%d", (cols - half_width) * 6); + sixel_encoder_setopt(encoder, 'w', width); + sixel_encoder_encode(encoder, current_file.name); + sixel_encoder_unref(encoder); + } else { + execlp("nsh", "nsh", current_file.name, NULL); + } + _exit(1); + } else if (pid > 0) { + /* Parent */ + /* + close(pipe_fd[1]); + */ + if (strstr(current_file.name, ".jpg") == NULL && strstr(current_file.name, ".png") == NULL) { + char buffer[4096]; + int row = 1; + FILE *stream = fdopen(pipe_fd[0], "r"); + while (fgets(buffer, sizeof(buffer), stream) != NULL && row <= rows - 1) { + buffer[strcspn(buffer, "\n")] = 0; + size_t len = strlen(buffer); + size_t offset = 0; - FILE *file = fopen(current_file.path, "r"); - if (file == NULL) { - mvwprintw(preview_content, 0, 0, "Unable to read %s", current_file.name); - return; - } - - int c; - /* check if its binary */ - while ((c = fgetc(file)) != EOF) { - if (c == '\0') { - mvwprintw(preview_content, 0, 0, "binary"); - return; - } - } + /* Print each line in chunks of `half_width` */ + while (len > 0 && row <= rows - 1) { + move_cursor(row++, half_width); - fseek(file, 0, SEEK_END); - long length = ftell(file); - /* check if file isn't empty */ - if (length != 0) { - fseek(file, 0, SEEK_SET); /* set cursor back to start of file */ - int max_length = (LINES - 3) * (COLS / 2 - 2); - char *buffer = memalloc(max_length * sizeof(char)); - fread(buffer, 1, max_length, file); - mvwprintw(preview_content, 0, 0, "%s", buffer); - free(buffer); - } else { - wclear(preview_content); - } - fclose(file); + /* Using all the window space on the right */ + if (len > (cols - half_width)) { + printf("%.*s\n", cols - half_width, buffer + offset); + offset += (cols - half_width); + len -= (cols - half_width); + } else { + printf("%s\n", buffer + offset); + break; + } + } + } + fclose(stream); + } + waitpid(pid, NULL, 0); + } else { + wpprintw("fork failed: %s", strerror(errno)); + } } /* @@ -879,209 +909,228 @@ void show_file_content() */ void edit_file() { - #ifdef EDITOR - char *editor = EDITOR; - #else - char *editor = getenv("EDITOR"); - #endif +#ifdef EDITOR + char *editor = EDITOR; +#else + char *editor = getenv("EDITOR"); +#endif - if (editor == NULL) { - wpprintw("$EDITOR not defined"); - return; - } else { - def_prog_mode(); /* save the tty modes */ - endwin(); /* end curses mode temporarily */ + if (editor == NULL) { + wpprintw("$EDITOR not defined"); + return; + } else { + char *filename = files->items[sel_file].path; + /* 1 for space 1 for null */ + int length = strlen(editor) + strlen(filename) + 2; + char command[length]; - char *filename = files->items[current_selection].path; - int length = strlen(editor) + strlen(filename) + 2; /* one for space one for null */ - char command[length]; - - snprintf(command, length, "%s %s", editor, filename); - system(command); - reset_prog_mode(); /* return to previous tty mode */ - refresh(); /* store the screen contents */ - } + snprintf(command, length, "%s %s", editor, filename); + system(command); + list_files(); + } } void toggle_executable() { - file current_file = files->items[current_selection]; - struct stat st; - if (stat(current_file.path, &st) == -1) { - wpprintw("stat failed: %s\n", strerror(errno)); - } - if (strncmp(current_file.type, "DIR", 3) == 0) - return; - /* chmod by xor executable bits */ - if (chmod(current_file.path, st.st_mode ^ (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) { - wpprintw("Error toggling executable: %s", strerror(errno)); - } + file f = files->items[sel_file]; + struct stat st; + if (stat(f.path, &st) == -1) { + wpprintw("stat failed: %s", strerror(errno)); + } + if (f.type == DRY) + return; + /* chmod by xor executable bits */ + if (chmod(f.path, st.st_mode ^ (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) { + wpprintw("Error toggling executable: %s", strerror(errno)); + } } char *replace_home(char *str) { - char *home = getenv("HOME"); - if (home == NULL) { - wpprintw("$HOME not defined"); - return str; - } - char *newstr = memalloc((strlen(str) + strlen(home)) * sizeof(char)); - /* replace ~ with home */ - snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1); - free(str); - return newstr; + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME not defined"); + return str; + } + char *newstr = memalloc(strlen(str) + strlen(home)); + /* replace ~ with home */ + snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1); + free(str); + return newstr; } int write_last_d() { - #ifdef LAST_D - char *last_d = estrdup(LAST_D); - #else - char *last_d = getenv("CCC_LAST_D"); - #endif - if (last_d == NULL) { - wpprintw("$CCC_LAST_D not defined (Press enter to continue)"); - return -1; - } else { - if (last_d[0] == '~') { - last_d = replace_home(last_d); - } - char *last_ddup = estrdup(last_d); - - char *last_d_dir = strrchr(last_ddup, '/'); - if (last_d_dir != NULL) { - *last_d_dir = '\0'; /* truncate string */ - } - mkdir_p(last_ddup); - FILE *last_d_file = fopen(last_d, "w"); - if (last_d_file == NULL) { - wpprintw("Cannot open last directory file (Press enter to continue)"); - return -1; - } - fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); - fclose(last_d_file); - free(last_ddup); - free(last_d); - } - return 0; +#ifdef LAST_D + char *last_d = estrdup(LAST_D); +#else + char *last_d = getenv("CCC_LAST_D"); +#endif + if (last_d == NULL) { + wpprintw("$CCC_LAST_D not defined (Press enter to continue)"); + return -1; + } else { + if (last_d[0] == '~') { + last_d = replace_home(last_d); + } + char *last_ddup = estrdup(last_d); + + char *last_d_dir = strrchr(last_ddup, '/'); + if (last_d_dir != NULL) { + *last_d_dir = '\0'; /* truncate string */ + } + mkdir_p(last_ddup); + FILE *last_d_file = fopen(last_d, "w"); + if (last_d_file == NULL) { + wpprintw("Cannot open last directory file (Press enter to continue)"); + return -1; + } + fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); + fclose(last_d_file); + free(last_ddup); + free(last_d); + } + return 0; } -int sort_compare(const void *a, const void *b) { - return strcmp(((file *) a)->name, ((file *) b)->name); +int sort_compare(const void *a, const void *b) +{ + return strcmp(((file *) a)->name, ((file *) b)->name); } void sort_files() { - qsort(files->items, files->length, sizeof(file), sort_compare); - highlight_current_line(); + qsort(files->items, files->length, sizeof(file), sort_compare); + list_files(); } char *get_panel_string(char *prompt) { - echo(); - wpprintw(prompt); - char *input = memalloc(PATH_MAX * sizeof(char)); - /* get string at y=0, x=length of prompt */ - mvwgetstr(panel, 0, strlen(prompt), input); - noecho(); - if (input[0] == '~') { - input = replace_home(input); - } - return input; + size_t bufsize = 128; + char *buf = memalloc(bufsize); + size_t buflen = 0; + buf[0] = '\0'; + while (1) { + wpprintw(prompt); + int c = read_key(); + if (c == BACKSPACE) { + if (buflen != 0) { + buf[--buflen] = '\0'; + } + } else if (c == '\033') { + wpprintw(""); + free(buf); + return NULL; + } else if (c == '\r') { + wpprintw(""); + if (buflen != 0) { + return buf; + } + } else if (!iscntrl(c) && c < 128) { + if (buflen == bufsize - 1) { + bufsize *= 2; + buf = realloc(buf, bufsize); + } + buf[buflen++] = c; + buf[buflen] = '\0'; + } + } + char *input = memalloc(PATH_MAX); + + if (input[0] == '~') { + input = replace_home(input); + } + return input; } void rename_file() { - char *filename = files->items[current_selection].path; - char *input = get_panel_string("Rename file: "); - char *newfilename = estrdup(filename); - /* remove basename of newfilename */ - char *last_slash = strrchr(newfilename, '/'); - *last_slash = '\0'; - /* add the slash back to newfilename */ - strcat(newfilename, "/"); - strcat(newfilename, input); - if (rename(filename, newfilename)) { - wpprintw("rename failed: %s (Press enter to continue)", strerror(errno)); - getch(); - } else { - change_dir(cwd, 0, 0); - wpprintw("Renamed %s to %s", filename, newfilename); - } - free(input); - free(newfilename); + char *filename = files->items[sel_file].path; + char *input = get_panel_string("Rename file: "); + char *newfilename = estrdup(filename); + /* remove basename of newfilename */ + char *last_slash = strrchr(newfilename, '/'); + *last_slash = '\0'; + /* add the slash back to newfilename */ + strcat(newfilename, "/"); + strcat(newfilename, input); + if (rename(filename, newfilename)) { + wpprintw("rename failed: %s (Press enter to continue)", strerror(errno)); + read_key(); + } else { + change_dir(cwd, 0, 0); + wpprintw("Renamed %s to %s", filename, newfilename); + } + free(input); + free(newfilename); } void goto_dir() { - char *input = get_panel_string("Goto dir: "); - struct stat st; - if (lstat(input, &st)) { - wpprintw("lstat failed: %s (Press enter to continue)", strerror(errno)); - getch(); - } - /* chdir to directory from argument */ - if (S_ISDIR(st.st_mode) && chdir(input)) { - wpprintw("chdir failed: %s (Press enter to continue)", strerror(errno)); - getch(); - } - getcwd(cwd, PATH_MAX); - change_dir(cwd, 0, 0); - free(input); + char *input = get_panel_string("Goto dir: "); + struct stat st; + if (lstat(input, &st)) { + wpprintw("lstat failed: %s (Press enter to continue)", strerror(errno)); + read_key(); + } + /* chdir to directory from argument */ + if (S_ISDIR(st.st_mode) && chdir(input)) { + wpprintw("chdir failed: %s (Press enter to continue)", strerror(errno)); + read_key(); + } + getcwd(cwd, PATH_MAX); + change_dir(cwd, 0, 0); + free(input); } void create_dir() { - char *input = get_panel_string("New dir: "); - char *newfilename = memalloc(PATH_MAX * sizeof(char)); - strcpy(newfilename, cwd); - strcat(newfilename, "/"); - strcat(newfilename, input); - if (access(newfilename, F_OK) != 0) { - mkdir_p(newfilename); - change_dir(cwd, 0, 0); - wpprintw("Created %s", input); - } else { - wpprintw("Directory already exist"); - } - free(input); - free(newfilename); + char *input = get_panel_string("New dir: "); + char *newfilename = memalloc(PATH_MAX); + snprintf(newfilename, PATH_MAX, "%s/%s", cwd, input); + if (access(newfilename, F_OK) != 0) { + mkdir_p(newfilename); + change_dir(cwd, 0, 0); + wpprintw("Created %s", input); + } else { + wpprintw("Directory already exist"); + } + free(input); + free(newfilename); } void create_file() { - char *input = get_panel_string("New file: "); - FILE *f = fopen(input, "w+"); - fclose(f); - change_dir(cwd, 0, 0); - wpprintw("Created %s", input); - free(input); + char *input = get_panel_string("New file: "); + FILE *f = fopen(input, "w+"); + fclose(f); + change_dir(cwd, 0, 0); + wpprintw("Created %s", input); + free(input); } void delete_files() { - if (marked->length) { - char *trash_dir = check_trash_dir(); - if (trash_dir != NULL) { - for (int i = 0; i < marked->length; i++) { - char *new_path = memalloc(PATH_MAX * sizeof(char)); - strcpy(new_path, trash_dir); - strcat(new_path, "/"); - strcat(new_path, marked->items[i].name); - if (rename(marked->items[i].path, new_path)) { - wpprintw("delete failed: %s\n", strerror(errno)); - } else { - change_dir(cwd, 0, 0); - } - } + if (marked->length) { + char *trash_dir = check_trash_dir(); + if (trash_dir != NULL) { + for (int i = 0; i < marked->length; i++) { + char *new_path = memalloc(PATH_MAX); + snprintf(new_path, PATH_MAX, "%s/%s", trash_dir, marked->items[i].name); + if (rename(marked->items[i].path, new_path)) { + wpprintw("delete failed: %s", strerror(errno)); + } else { + change_dir(cwd, 0, 0); + } + } for (int i = 0; i < marked->length; i++) { arraylist_remove(marked, 0); } - } else { - wpprintw("TODO: implement hard delete"); - } - } + } else { + wpprintw("TODO: implement hard delete"); + } + } } /* @@ -1089,97 +1138,127 @@ void delete_files() */ void wpprintw(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - wclear(panel); - vw_printw(panel, fmt, args); - va_end(args); - wrefresh(panel); + char buffer[256]; + va_list args; + + va_start(args, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, args); + va_end(args); + + move_cursor(rows, 1); + /* Clear line and print formatted string */ + bprintf("\033[K%s", buffer); } -void init_windows() +void move_cursor(int row, int col) { - /* offset for width of the content 1 and 2 */ - int width_left = half_width; - int width_right = half_width; - #ifdef WINDOW_OFFSET - width_left += WINDOW_OFFSET; - width_right -= WINDOW_OFFSET; - #endif + bprintf("\033[%d;%dH", row, col); +} - /*------------------------------+ - |----border(0)--||--border(2)--|| - || || || - || content (1) || content (3) || - || (directory) || (preview) || - || || || - |---------------||-------------|| - +==========panel (4)===========*/ - - /* lines, cols, y, x */ - panel = newwin(PH, COLS, LINES - PH, 0 ); - /* draw border around windows */ - #if DRAW_BORDERS - directory_border = newwin(LINES - PH, width_left, 0, 0 ); - directory_content = newwin(LINES - PH - 2, width_left - 2, 1, 1 ); +int read_key() +{ + int nread; + char c; + while ((nread = read(STDIN_FILENO, &c, 1)) != 1) { + if (nread == -1 && errno != EAGAIN) { + die("read"); + } + } - preview_border = newwin(LINES - PH, width_right, 0, width_left ); - preview_content = newwin(LINES - PH - 2, width_right - 2, 1, width_left + 1); - - draw_border_title(directory_border, true); - draw_border_title(preview_border, true); - #else - /* if there are no borders, then draw content in their places */ - directory_border = newwin(0, 0, COLS, LINES ); - preview_border = newwin(0, 0, COLS, LINES ); - /* -1 for the one space to the left */ - directory_content = newwin(LINES - PH - 1, width_left, 0, 1 ); - preview_content = newwin(LINES - PH, width_right, 0, width_left ); - #endif + if (c == '\033') { + char seq[3]; - scrollok(directory_content, true); - refresh(); + if (read(STDIN_FILENO, &seq[0], 1) != 1) + return '\033'; + if (read(STDIN_FILENO, &seq[1], 1) != 1) + return '\033'; + + if (seq[0] == '[') { + if (seq[1] >= '0' && seq[1] <= '9') { + if (read(STDIN_FILENO, &seq[2], 1) != 1) + return '\033'; + if (seq[2] == '~') { + switch (seq[1]) { + case '1': return HOME_KEY; + case '3': return DEL_KEY; + case '4': return END_KEY; + case '5': return PAGE_UP; + case '6': return PAGE_DOWN; + case '7': return HOME_KEY; + case '8': return END_KEY; + } + } + } else { + switch (seq[1]) { + case 'A': return ARROW_UP; + case 'B': return ARROW_DOWN; + case 'C': return ARROW_RIGHT; + case 'D': return ARROW_LEFT; + case 'F': return END_KEY; + case 'H': return HOME_KEY; + } + } + } else if (seq[0] == 'O') { + switch (seq[1]) { + case 'F': return END_KEY; + case 'H': return HOME_KEY; + } + } + return '\033'; + } else { + return c; + } +} + +int get_cursor_position(int *rows, int *cols) +{ + char buf[32]; + unsigned int i = 0; + bprintf("\033[6n"); + while (i < sizeof(buf) - 1) { + if (read(STDIN_FILENO, &buf[i], 1) != 1) { + break; + } + if (buf[i] == 'R') { + break; + } + i++; + } + buf[i] = '\0'; + if (buf[0] != '\033' || buf[1] != '[') { + return -1; + } + if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) { + return -1; + } + return 0; +} + +int get_window_size(int *row, int *col) +{ + struct winsize ws; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { + /* Can't get window size */ + bprintf("\033[999C\033[999B"); + return get_cursor_position(row, col); + } else { + *col = ws.ws_col; + *row = ws.ws_row; + return 0; + } } /* - * Draw the border of the window depending if it's active or not, + * printf, but write to STDOUT_FILENO */ -void draw_border_title(WINDOW *window, bool active) +void bprintf(const char *fmt, ...) { - /* check if the window is directory of preview */ - int width = half_width; - if (window == directory_border) { /* left */ - width += WINDOW_OFFSET; - } else if (window == preview_border) { /* right */ - width -= WINDOW_OFFSET; - } + char buffer[512]; + va_list args; - /* turn on color depends on active */ - if (active) { - wattron(window, COLOR_PAIR(7)); - } else { - wattron(window, COLOR_PAIR(5)); - } - /* draw top border */ - mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */ - mvwhline(window, 0, 1, ACS_HLINE, width - 2); /* top horizontal line */ - mvwaddch(window, 0, width - 1, ACS_URCORNER); /* upper right corner */ + va_start(args, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, args); + va_end(args); - /* draw side border */ - mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); /* left vertical line */ - mvwvline(window, 1, width - 1, ACS_VLINE, LINES - 2); /* right vertical line */ - - /* draw bottom border - * make space for the panel */ - mvwaddch(window, LINES - PH - 1, 0, ACS_LLCORNER); /* lower left corner */ - mvwhline(window, LINES - PH - 1, 1, ACS_HLINE, width - 2); /* bottom horizontal line */ - mvwaddch(window, LINES - PH - 1, width - 1, ACS_LRCORNER); /* lower right corner */ - - /* turn color off after turning it on */ - if (active) { - wattroff(window, COLOR_PAIR(7)); - } else { - wattroff(window, COLOR_PAIR(5)); - } - wrefresh(window); /* Refresh the window to see the colored border and title */ + write(STDOUT_FILENO, buffer, strlen(buffer)); }