diff --git a/colors.c b/colors.c index adc4ac9..d96f003 100644 --- a/colors.c +++ b/colors.c @@ -9,9 +9,6 @@ unsigned int color_count; color *colors; -extern file *rgt_content; -extern unsigned long rgt_file_count; - extern unsigned int settings; extern unsigned int status; diff --git a/defines.h b/defines.h index 4a3ac90..0d3d15f 100644 --- a/defines.h +++ b/defines.h @@ -3,10 +3,10 @@ #define STATUS_QUIT_PROGRAM 1 #define STATUS_RUN_BACKEND 2 #define STATUS_RELOAD_DIRECTORY 4 -#define STATUS_UPDATE_SCREEN_MASK 24 /* 11000 */ -#define STATUS_UPDATE_SCREEN_0 8 +#define STATUS_UPDATE_SCREEN_INTERACTION 8 #define STATUS_UPDATE_SCREEN_RESIZE 16 -#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32 +#define STATUS_UPDATE_SCREEN_PRINTED 32 +#define STATUS_UPDATE_SCREEN_MASK (STATUS_UPDATE_SCREEN_INTERACTION | STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_PRINTED) #define STATUS_USER_ROOT 64 #define STATUS_INPUT_MATCH 128 #define STATUS_DELTA_TIME 256 @@ -70,6 +70,11 @@ typedef struct File { unsigned long file_size; /*if its a file, its in bytes, if its a dir, its the count of files within that dir */ char *file_name; } file; +typedef struct Dir { + unsigned long file_count; + file *current_file; + file *file_list; +} dir; typedef struct Color { char *file_extension; short color_pair; diff --git a/dir.c b/dir.c index e6675f3..e72b463 100644 --- a/dir.c +++ b/dir.c @@ -12,8 +12,7 @@ #include "config.h" -extern file *mid_content; -extern unsigned long mid_file_count; +extern dir mid_dir; extern unsigned int settings; extern unsigned int file_modifiers; extern unsigned int color_count; @@ -48,85 +47,87 @@ unsigned long get_dir_size(char *path){ } -void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){ +void get_dir_content(char *path, dir *dir){ struct dirent **entry = NULL; if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */ scandir(path, &entry, skip_dot, NULL); } else { scandir(path, &entry, skip_hidden_files, NULL); } - + char *full_path = NULL; unsigned long i = 0; - for (i = 0; i < *dir_file_count; i++ ) { + for (i = 0; i < dir->file_count; i++ ) { + dir->file_list[i].file_name = NULL; if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) { } else { - dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1); - memcpy(dir_content[i].file_name, entry[i]->d_name, strlen(entry[i]->d_name) + 1); + dir->file_list[i].file_name = malloc(strlen(entry[i]->d_name)+1); + memcpy(dir->file_list[i].file_name, entry[i]->d_name, strlen(entry[i]->d_name) + 1); + dir->file_list[i].status = 0; struct stat *file; file = malloc(sizeof(struct stat)); /* using the full path allows using the same function for all windows */ - char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/")); + full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/")); memcpy(full_path, path, strlen(path)); memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1); full_path[strlen(path)] = '/'; lstat(full_path, file); - dir_content[i].file_size = file->st_size; - dir_content[i].permissions = file->st_mode; + dir->file_list[i].file_size = file->st_size; + dir->file_list[i].permissions = file->st_mode; if (S_ISLNK(file->st_mode)) { stat(full_path, file); if (S_ISDIR(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK; - dir_content[i].color_pair = COLOR_SYMLINK; - dir_content[i].file_size = get_dir_size(full_path); + dir->file_list[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK; + dir->file_list[i].color_pair = COLOR_SYMLINK; + dir->file_list[i].file_size = get_dir_size(full_path); } else { - dir_content[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK; - dir_content[i].color_pair = COLOR_SYMLINK; + dir->file_list[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK; + dir->file_list[i].color_pair = COLOR_SYMLINK; } } else if (S_ISDIR(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_DIR; - dir_content[i].color_pair = COLOR_DIR; - dir_content[i].file_size = get_dir_size(full_path); + dir->file_list[i].file_type = FILE_TYPE_DIR; + dir->file_list[i].color_pair = COLOR_DIR; + dir->file_list[i].file_size = get_dir_size(full_path); } else if (file->st_mode & S_IXUSR) { - dir_content[i].file_type = FILE_TYPE_EXEC; - dir_content[i].color_pair = COLOR_EXEC; + dir->file_list[i].file_type = FILE_TYPE_EXEC; + dir->file_list[i].color_pair = COLOR_EXEC; } else if (S_ISREG(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_REGULAR; - dir_content[i].color_pair = COLOR_REGULAR; + dir->file_list[i].file_type = FILE_TYPE_REGULAR; + dir->file_list[i].color_pair = COLOR_REGULAR; unsigned long j = 0; char *extension = strrchr(entry[i]->d_name, '.'); if (extension) { for (j = 0; j < color_count; j++) { if (!strcmp(colors[j].file_extension, extension)) { - dir_content[i].color_pair = colors[j].color_pair; + dir->file_list[i].color_pair = colors[j].color_pair; } } } } else if (S_ISBLK(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_BLOCK; - dir_content[i].color_pair = COLOR_BLOCK; + dir->file_list[i].file_type = FILE_TYPE_BLOCK; + dir->file_list[i].color_pair = COLOR_BLOCK; } else if (S_ISCHR(file->st_mode)) { - dir_content[i].file_type = COLOR_CHARDEV; + dir->file_list[i].file_type = COLOR_CHARDEV; } else if (S_ISFIFO(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_FIFO; - dir_content[i].color_pair = COLOR_FIFO; + dir->file_list[i].file_type = FILE_TYPE_FIFO; + dir->file_list[i].color_pair = COLOR_FIFO; } else if (S_ISSOCK(file->st_mode)) { - dir_content[i].file_type = FILE_TYPE_SOCK; - dir_content[i].color_pair = COLOR_SOCK; + dir->file_list[i].file_type = FILE_TYPE_SOCK; + dir->file_list[i].color_pair = COLOR_SOCK; } else { - dir_content[i].file_type = COLOR_REGULAR; - dir_content[i].color_pair = COLOR_REGULAR; + dir->file_list[i].file_type = COLOR_REGULAR; + dir->file_list[i].color_pair = COLOR_REGULAR; unsigned long j = 0; char *extension = strrchr(entry[i]->d_name, '.'); if (extension) { for (j = 0; j < color_count; j++) { if (!strcmp(colors[j].file_extension, extension)) { - dir_content[i].color_pair = colors[j].color_pair; + dir->file_list[i].color_pair = colors[j].color_pair; } } } else { @@ -135,16 +136,19 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten free(full_path); free(file); free(entry[i]); + full_path = NULL; + file = NULL; + entry[i] = NULL; } } - qsort(dir_content, *dir_file_count, sizeof(file), order_func); + qsort(dir->file_list, dir->file_count, sizeof(file), order_func); free(entry); } -void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content){ +void print_dir(WINDOW *win, char print_info, dir *dir){ /* i am not proud of this function */ unsigned long line_width = getmaxx(win); @@ -161,22 +165,23 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file #else long offset_front = 2; #endif + long selected_file_current = dir->current_file - dir->file_list; long offset_index = 2; /* only used for the index of the file itself */ if (print_info) { - if (*dir_file_count > 9) { + if (dir->file_count > 9) { #if SETTINGS_LINE_NUMBERS != 0 - unsigned long dir_file_count_ = *dir_file_count; + unsigned long dir_file_count_ = dir->file_count; while(dir_file_count_ > 9) { offset_front++; dir_file_count_ /= 10; } #endif } - if (selected_file_current > (terminal_height/3)*2 && *dir_file_count > terminal_height - 2) { - if (selected_file_current + (terminal_height/3) >= *dir_file_count) { - offset_vertical = *dir_file_count - terminal_height+2; + if (selected_file_current > (terminal_height/3)*2 && dir->file_count > terminal_height - 2) { + if (selected_file_current + (terminal_height/3) >= dir->file_count) { + offset_vertical = dir->file_count - terminal_height+2; } else { offset_vertical = selected_file_current - (terminal_height/3)*2; } @@ -184,12 +189,12 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file } else { offset_front = 0; } - for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) { + for (i = offset_vertical; i < dir->file_count && i < (terminal_height + offset_vertical); i++) { if (print_info) { - file_size = dir_content[i].file_size; + file_size = dir->file_list[i].file_size; char size_index = -1; do { @@ -199,8 +204,8 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file } while (file_size > 1 && size_index < size_unit_count); size_char = size_unit[(unsigned)size_index]; - if (dir_content[i].file_type &= FILE_TYPE_DIR) { - offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1); + if (dir->file_list[i].file_type &= FILE_TYPE_DIR) { + offset_back = line_width - (snprintf(NULL,0,"%ld", dir->file_list[i].file_size) + 1); } else if (size_char =='B') { offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1); } else { @@ -210,17 +215,17 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file offset_back = line_width; } - if (dir_content[i].status & FILE_STATUS_SELECTED) { + if (dir->file_list[i].status & FILE_STATUS_SELECTED) { is_selected = 1; } else { is_selected = 0; } - if (dir_content[i].status & FILE_STATUS_SELECTED) { + if (dir->file_list[i].status & FILE_STATUS_SELECTED) { wattron(win, COLOR_PAIR(8)); } else { - wattron(win, COLOR_PAIR(dir_content[i].color_pair)); + wattron(win, COLOR_PAIR(dir->file_list[i].color_pair)); } - if (dir_content[i].status & FILE_STATUS_HOVER) { + if (&dir->file_list[i] == dir->current_file) { wattron(win, A_REVERSE); unsigned long bg = 0; for (bg = 0; bg < line_width; bg++) { @@ -265,29 +270,29 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file #endif - if (dir_content[i].file_type &= FILE_TYPE_DIR) { - mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size); + if (dir->file_list[i].file_type &= FILE_TYPE_DIR) { + mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir->file_list[i].file_size); }else if (size_char =='B') { mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char); } else { mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char); } } - char *extension = strrchr(dir_content[i].file_name, '.'); + char *extension = strrchr(dir->file_list[i].file_name, '.'); unsigned long printable_size = offset_back-offset_front-is_selected-2; - mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir_content[i].file_name, printable_size); - if (extension && printable_size <= strlen(dir_content[i].file_name)) { + mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir->file_list[i].file_name, printable_size); + if (extension && printable_size <= strlen(dir->file_list[i].file_name)) { mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-1, extension, strlen(extension)); mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-2, "~", 1); } - if (dir_content[i].status & FILE_STATUS_SELECTED) { + if (dir->file_list[i].status & FILE_STATUS_SELECTED) { wattroff(win, COLOR_PAIR(8)); } else { - wattroff(win, COLOR_PAIR(dir_content[i].color_pair)); + wattroff(win, COLOR_PAIR(dir->file_list[i].color_pair)); } } @@ -296,6 +301,7 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file char update_selected_file(){ char ret = -1; /* -1 on empty or inaccessible file, 0 on unchanged file, 1 on changed file */ + /* if (selected_file_current >= mid_file_count) { selected_file_current = mid_file_count-1; } @@ -305,18 +311,22 @@ char update_selected_file(){ } else { ret = 0; } - mid_content[selected_file_current].status |= FILE_STATUS_HOVER; + mid_dir.current_dir.status |= FILE_STATUS_HOVER; selected_file_last = selected_file_current; + */ return ret; } void dir_set_selected_file_current(unsigned long selected_file_current){ + /* if (mid_content->file_name) { current_dir->selected_file_current = selected_file_current; } + */ } unsigned long dir_get_selected_file_current(){ + /* current_dir = visited_dirs; if (mid_content->file_name[0] == '\0') { return 0; @@ -339,6 +349,7 @@ unsigned long dir_get_selected_file_current(){ free(path); return current_dir->selected_file_current; } + */ } void dir_init(){ visited_dirs = malloc(sizeof(linked_dir)); @@ -350,6 +361,7 @@ void dir_init(){ } void recursive_delete(file current_file){ + /* if (S_ISLNK(current_file.permissions)) { remove(current_file.file_name); } else if (current_file.file_type & FILE_TYPE_DIR ) { @@ -379,4 +391,5 @@ void recursive_delete(file current_file){ } else { remove(current_file.file_name); } + */ } diff --git a/dir.h b/dir.h index 2bdbf5f..fa9ef33 100644 --- a/dir.h +++ b/dir.h @@ -4,8 +4,8 @@ #endif unsigned long get_dir_size(char *path); -void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content); -void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content); +void get_dir_content(char *path, dir *dir); +void print_dir(WINDOW *win, char print_info, dir *dir); char update_selected_file(); void dir_set_selected_file_current(unsigned long selected_file_current); unsigned long dir_get_selected_file_current(); diff --git a/file_previews.c b/file_previews.c index 4f23421..ff1c24b 100644 --- a/file_previews.c +++ b/file_previews.c @@ -40,7 +40,6 @@ char* preview_file(file *f){ char *file_buffer; - char *mime = get_mimetype(f); #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 @@ -81,7 +80,7 @@ char* text(file *f){ } } char* generic(file *f){ - static const char *cmd_str = "file "; + static const char *cmd_str = "file"; char *cmd = parse_cmd(cmd_str, f); FILE *cmd_open = popen(cmd, "r"); diff --git a/interactions.c b/interactions.c index 9bcd1f5..a6bcfa8 100644 --- a/interactions.c +++ b/interactions.c @@ -21,9 +21,7 @@ extern pthread_mutex_t mutex_rgt; extern pthread_mutex_t mutex_mid; extern pthread_mutex_t mutex_btm; extern pthread_cond_t cond_rgt; -extern file *mid_content; -extern file *lft_content; -extern file *rgt_content; +extern dir mid_dir; extern unsigned int terminal_height; extern unsigned int terminal_width; @@ -32,7 +30,6 @@ extern WINDOW *win_b; extern char *rgt_buffer; extern char *btm_buffer; -extern unsigned long mid_file_count; extern unsigned int status; extern char *start_path; @@ -46,16 +43,17 @@ unsigned long parsed_input_number; yank yank_files = { 0 }; extern void render_pass(); -extern void window_btm(WINDOW *win, char force_render); extern int (*order_func)(); +#define TODO noraw(); \ + endwin();\ + curs_set(1);\ + echo();\ + printf("TODO: %s at %d in %s \n", __func__, __LINE__, __FILE__);\ + exit(1); void FAIL(char *function, char *str){ - noraw(); - endwin(); - curs_set(1); - echo(); printf("ERROR in function %s: %s", function, str); } void user_interactions() { @@ -74,7 +72,7 @@ void user_interactions() { memset(input, 0, INPUT_BUFFER_SIZE); input_pass = 0; } - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_INTERACTION; } @@ -87,6 +85,9 @@ void user_interactions() { input++; number_length++; } + if (parsed_input_number == 0) { + parsed_input_number = 1; + } input -= number_length; char cmp_len = strlen(input); @@ -137,9 +138,9 @@ int read_string(WINDOW *win, int y, int x, char *str){ err = 0; break; } else if (ch == '\t') { /* tab */ - memcpy(str + pass, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)); - mvwaddstr(win, y, x +pass, mid_content[selected_file_current].file_name); - pass += strlen(mid_content[selected_file_current].file_name); + memcpy(str + pass, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)); + mvwaddstr(win, y, x +pass, mid_dir.current_file->file_name); + pass += strlen(mid_dir.current_file->file_name); } else if (ch == 127) { /* backspace */ if (pass > 0) { pass--; @@ -163,385 +164,71 @@ int read_string(WINDOW *win, int y, int x, char *str){ void quit_program(){ status = STATUS_QUIT_PROGRAM; } +void update(){ + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_MASK); +} void select_all(){ - pthread_mutex_lock(&mutex_selection); - pthread_mutex_lock(&mutex_mid); - unsigned long i; - for(i = 0; i < mid_file_count; i++) { - mid_content[i].status ^= FILE_STATUS_SELECTED; - } - pthread_mutex_unlock(&mutex_mid); - pthread_mutex_unlock(&mutex_selection); + TODO; } void move_down(unsigned long passes){ - pthread_mutex_lock(&mutex_selection); - if (passes == 0) { - passes++; + mid_dir.current_file = mid_dir.current_file + passes; + if (mid_dir.current_file >= mid_dir.file_list + mid_dir.file_count - 1) { + mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1; } - selected_file_current += passes; - - update_selected_file(); - current_dir->selected_file_current = selected_file_current; - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - pthread_mutex_unlock(&mutex_selection); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_INTERACTION); } void move_up(unsigned long passes){ - pthread_mutex_lock(&mutex_selection); - if (passes == 0) { - passes++; + mid_dir.current_file = mid_dir.current_file - passes; + if (mid_dir.current_file <= mid_dir.file_list) { + mid_dir.current_file = mid_dir.file_list; } - unsigned long tmp = selected_file_current; - selected_file_current -= passes; - if (tmp < selected_file_current) { - selected_file_current = 0; - } - - update_selected_file(); - current_dir->selected_file_current = selected_file_current; - - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - pthread_mutex_unlock(&mutex_selection); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_INTERACTION); } void move_left(unsigned long passes){ - if (passes == 0) { - passes++; - } unsigned long i; for (i = 0; i < passes; i++) { if (chdir("..") != 0) { /* TODO(2025-07-09T00:30:05) fix */ FAIL("move_left", "unhandled error of chdir"); } else { - selected_file_current = dir_get_selected_file_current(); } } - update_selected_file(); - current_dir->selected_file_current = selected_file_current; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RELOAD_FULL | STATUS_RELOAD_DIRECTORY); + update(); + } void move_right(){ - if (mid_content->file_name[0] == '\0') { - return; - } - if ((mid_content[selected_file_current].file_type & FILE_TYPE_DIR) == FILE_TYPE_DIR) { - if (chdir(mid_content[selected_file_current].file_name) != 0) { - FAIL("move_right", "unhandled error of chdir"); - } else { - selected_file_current = dir_get_selected_file_current(); - } - } else { - unsigned long i = 0; - char match = 0; - char *mime = get_mimetype(&mid_content[selected_file_current]); - char *extension = strrchr(mid_content[selected_file_current].file_name, '.'); - if (extension != NULL) { - for (i = 0; i < file_extension_default_count; i++) { - if (strstr(extension, file_extension_default_cmd[i].file_extension)) { - char *cmd; - cmd = parse_cmd(file_extension_default_cmd[i].command, &mid_content[selected_file_current]); - printf("\n%s\n",cmd); - - if (system(cmd) == -1) { - /*do nothing*/ - } - free(cmd); - curs_set(1); /*for some reason, 1 here turns it invisible once again */ - match = 1; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RELOAD_FULL | STATUS_RELOAD_DIRECTORY); - break; - } - } - } - if (match == 0) { - for (i = 0; i < mimetype_default_count; i++) { - if (strstr(mime, mimetype_default_cmd[i].mimetype)) { - - char *cmd = parse_cmd(mimetype_default_cmd[i].command, &mid_content[selected_file_current]); - if (system(cmd) == -1) { - /*do nothing*/ - } - curs_set(1); /*for some reason, 1 here turns it invisible once again */ - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RELOAD_FULL | STATUS_RELOAD_DIRECTORY); - break; - - } - } - } - free(mime); - } - update_selected_file(); - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RELOAD_FULL | STATUS_RELOAD_DIRECTORY); + TODO; } void toggle_hidden_files(){ file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RELOAD_FULL | STATUS_RELOAD_DIRECTORY); + status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY); } void toggle_selection(){ - pthread_mutex_lock(&mutex_selection); - pthread_mutex_lock(&mutex_mid); - mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED; - status |= (STATUS_UPDATE_SCREEN_MASK); - pthread_mutex_unlock(&mutex_mid); - pthread_mutex_unlock(&mutex_selection); - move_down(1); + TODO; } void jump_bottom(){ - pthread_mutex_lock(&mutex_selection); - selected_file_current = 0 - 1; - update_selected_file(); - current_dir->selected_file_current = selected_file_current; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - pthread_mutex_unlock(&mutex_selection); + mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1; } void jump_top(){ - pthread_mutex_lock(&mutex_selection); - selected_file_current = 0; - update_selected_file(); - current_dir->selected_file_current = selected_file_current; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); - pthread_mutex_unlock(&mutex_selection); + mid_dir.current_file = mid_dir.file_list; } - void open_with(){ - pthread_mutex_lock(&mutex_btm); - - - char *btm_buffer_tmp = btm_buffer; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - - concat(btm_buffer, ui_open_with_text_0, " \'", 0); - concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\' ", 1); - concat(btm_buffer, btm_buffer, ui_open_with_text_1, 1); - - - window_btm(win_b, 1); - - - char *str = malloc(INPUT_BUFFER_SIZE); - memset(str, ' ', INPUT_BUFFER_SIZE); - str[INPUT_BUFFER_SIZE-1] = '\0'; - int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0 , str); - - - if (err == 0) { - char *cmd; - concat(cmd, str, " ./\'", 0); - concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\'", 1); - - #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 - images_clear(); - #endif - - endwin(); - if (system(cmd) == -1) { - FAIL("open_with", "creating subcommand failed unhandled"); - } - initscr(); - } - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - pthread_mutex_unlock(&mutex_btm); - - free(str); + TODO; } - void rename_hovered(){ - pthread_mutex_lock(&mutex_btm); - - char *btm_buffer_tmp = btm_buffer; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - - concat(btm_buffer, ui_rename_text_0, " \'", 0); - concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\' ", 1); - concat(btm_buffer, btm_buffer, ui_rename_text_1, 1); - - window_btm(win_b, 1); - - char *str = malloc(INPUT_BUFFER_SIZE); - memset(str, ' ', INPUT_BUFFER_SIZE); - str[INPUT_BUFFER_SIZE-1] = '\0'; - int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); - - - if (!err) { - char *cmd; - concat(cmd, "mv ./\'", mid_content[selected_file_current].file_name, 0); - concat(cmd, cmd, "\' ./\'", 1); - concat(cmd, cmd, str, 1); - concat(cmd, cmd, "\'", 1); - - if (system(cmd) == -1) { - FAIL("rename_hovered", "mv or creating subcommand failed"); - }; - btm_buffer = cmd; - } - free(str); - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - - pthread_mutex_unlock(&mutex_btm); - + TODO; } - void delete(){ - pthread_mutex_lock(&mutex_btm); - - char *btm_buffer_tmp = btm_buffer; - - unsigned int i = 0; - unsigned int hits = 0; - char *file_str = ""; - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - concat(file_str, file_str, "\'", 0); - concat(file_str, file_str, mid_content[i].file_name, 1); - concat(file_str, file_str, "\' ", 1); - hits++; - } - } - - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width); - btm_buffer = malloc(BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width); - memset(btm_buffer, ' ', BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width); - - memcpy(btm_buffer, ui_delete_text ,strlen(ui_delete_text)); - - /*this horrendous check tries to copy everything until the last line, while keeping said last line unwritten*/ - /*lets hope im never gonna need to format something like this ever again*/ - memcpy(btm_buffer + strlen(ui_delete_text)+1, file_str, - (strlen(file_str) > ((BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width) - terminal_width) ? - ((BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width) - terminal_width) : - strlen(file_str))); - - memcpy(btm_buffer + (BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width - terminal_width) , "(y/N)", strlen("(y/N)")); - btm_buffer[BTM_WINDOW_HEIGHT_ON_STR_INTERACTION * terminal_width-1] = '\0'; - - - - window_btm(win_b, 1); - - timeout(-1); /* negative numbers block until enter is pressed */ - char ch = wgetch(win_b); - - if (ch == 'y' || ch == 'Y') { - if (hits) { - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - recursive_delete(mid_content[i]); - } - } - } else { - if (mid_content[selected_file_current].file_type & FILE_TYPE_DIR) { - recursive_delete(mid_content[selected_file_current]); - } - remove(mid_content[selected_file_current].file_name); - - } - } - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - if (hits) { - free(file_str); - } - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - - pthread_mutex_unlock(&mutex_btm); + TODO; } - void makedir(){ - pthread_mutex_lock(&mutex_btm); - - - char *btm_buffer_tmp = btm_buffer; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - - btm_buffer = "create dir: "; - - window_btm(win_b, 1); - - char *str = malloc(INPUT_BUFFER_SIZE); - memset(str, ' ', INPUT_BUFFER_SIZE); - str[INPUT_BUFFER_SIZE-1] = '\0'; - int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); - - if (!err) { - concat(btm_buffer, btm_buffer, str, 0); - mode_t mask = umask(0); - mkdir(str, 0755); /*magic number from default permissions as created by mkdir*/ - umask(mask); - } - free(str); - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - pthread_mutex_unlock(&mutex_btm); + TODO; } void makefile(){ - pthread_mutex_lock(&mutex_btm); - - char *btm_buffer_tmp = btm_buffer; - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - - btm_buffer = "create file: "; - - window_btm(win_b, 1); - - char *str = malloc(INPUT_BUFFER_SIZE); - memset(str, ' ', INPUT_BUFFER_SIZE); - str[INPUT_BUFFER_SIZE-1] = '\0'; - int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); - - if (!err) { - concat(btm_buffer, btm_buffer, str, 0); - FILE *fp; - fp = fopen(str, "w"); - fclose(fp); - } - free(str); - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - pthread_mutex_unlock(&mutex_btm); -} - -void update(){ - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + TODO; } void enter_shell(unsigned long passes, int index){ - (void)passes; - - #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 - images_clear(); - #endif - endwin(); - if (system(key_binding[index].black_magic) != 0) { - /*do nothing*/ - } - initscr(); - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + TODO; } void not_implemented(unsigned long passes, int index){ (void)passes; @@ -549,63 +236,10 @@ void not_implemented(unsigned long passes, int index){ mvaddstr(terminal_height-1, 0, key_binding[index].comment); mvaddstr(terminal_height-1, strlen(key_binding[index].comment), "\t"); mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented"); + TODO; } void jump_to_dir(unsigned long passes, int index){ - (void)passes; - - char *ch = (char*)key_binding[index].black_magic; - char slash = 0; - unsigned int env_len = 0; - while (*ch != '\0') { - if (*ch == '/') { - slash = 1; - break; - } - env_len++; - ch++; - } - char *env_str = NULL; - char *env_parsed = NULL; - char *path = NULL; - ch = (char*)key_binding[index].black_magic; - if (*ch == '/') { - path = malloc(strlen((char*)key_binding[index].black_magic)); - memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1); - } else if (slash) { - env_str = malloc(env_len * sizeof(char)); - memcpy(env_str, (char*)key_binding[index].black_magic +1, env_len); - env_str[env_len-1] = '\0'; - env_parsed = getenv(env_str); - if (env_parsed) { - concat(path, env_parsed, (char*)key_binding[index].black_magic + env_len, 0); - } else { - path = malloc(strlen((char*)key_binding[index].black_magic)); - memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1); - } - } else { - env_parsed = getenv((char*)key_binding[index].black_magic +1); - if (env_parsed) { - path = malloc(strlen(env_parsed)+1); - memcpy(path, env_parsed, strlen(env_parsed)+1); - } else { - path = malloc(strlen((char*)key_binding[index].black_magic)); - memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1); - } - } - if (chdir(path) != 0) { - FAIL("jump_to_dir", "jumping to black_magic in config.h failed"); - } else { - selected_file_current = dir_get_selected_file_current(); - } - - /*env_parsed shall not be modified (read: free'd) - the man page*/ - if (env_str) { - free(env_str); - } - if(path) { - free(path); - } - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); + TODO; } void order_by(unsigned long passes, int index){ (void)passes; @@ -613,314 +247,29 @@ void order_by(unsigned long passes, int index){ seed = time(NULL); order_func = key_binding[index].black_magic; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); + status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_INTERACTION | STATUS_RELOAD_DIRECTORY); } void cmd_on_selected(unsigned long passes, int index){ - (void)passes; - pthread_mutex_lock(&mutex_btm); - - char *btm_buffer_tmp = btm_buffer; - btm_buffer = ""; - unsigned int i = 0; - unsigned int hits = 0; - char *file_str = ""; - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - concat(file_str, file_str, "\'", 0); - concat(file_str, file_str, mid_content[i].file_name, 1); - concat(file_str, file_str, "\' ", 1); - hits++; - } - } - - if (hits) { - concat(btm_buffer, key_binding[index].black_magic, file_str, 2); - } else { - concat(btm_buffer, key_binding[index].black_magic, "\'", 0); - concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); - concat(btm_buffer, btm_buffer, "\'", 1); - } - - concat(btm_buffer, btm_buffer, "?", 1); - concat(btm_buffer, btm_buffer, "\n\n", 1); - concat(btm_buffer, btm_buffer, "(y/N)", 1); - - werase(win_b); - mvwin(win_b, terminal_height-6, 0); - wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - - window_btm(win_b, 1); - - timeout(-1); /* negative numbers block until enter is pressed */ - /* TODO(2025-07-06T07:22:49) fix fixed buffer size */ - char ch = wgetch(win_b); - - if (ch == 'y' || ch == 'Y') { - /* the second loop is used to add "./", wich is not being printed" */ - char *cmd = malloc(sizeof(char)); - /* TODO(2025-07-06T07:23:05) IMPORTANT: this really fucks up when the file has a quotation mark in its name */ - endwin(); - if (hits) { - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - free(cmd); - concat(cmd, (char*)key_binding[index].black_magic, " \'", 0); - concat(cmd, cmd, mid_content[i].file_name, 1); - concat(cmd, cmd, "\'", 1); - if (system(cmd) != 0) { - /*do nothing*/ - } - } - } - } else { - free(cmd); - concat(cmd, (char*)key_binding[index].black_magic, " \'", 0); - concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\'", 1); - if (system(cmd) != 0) { - /*do nothing*/ - } - mvaddstr(10,10, cmd); - - } - /*system(cmd);*/ - free(cmd); - initscr(); - - } - free(btm_buffer); - btm_buffer = btm_buffer_tmp; - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - - pthread_mutex_unlock(&mutex_btm); + TODO; } void yank_text(unsigned long passes, int index){ - (void)passes; - char *cmd; - if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) { - char *path=getcwd(NULL, 0); - concat(cmd, "echo \'", path, 0); - concat(cmd, cmd, "/", 1); - concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); - concat(cmd, cmd, "\' | ", 1); - concat(cmd, cmd, clipboard_cmd, 1); - free(path); - } else { - concat(cmd, "echo \'", mid_content[selected_file_current].file_name, 0); - concat(cmd, cmd, "\' | ", 1); - concat(cmd, cmd, clipboard_cmd, 1); - } - if (system(cmd) == -1) { - /*do nothing*/ - } - free(cmd); + TODO; } void yank_file(unsigned long passes, int index){ - (void)passes; - - unsigned long i; - if (yank_files.status & YANK_IS_USED) { - for (i = 0; i < yank_files.count; i++) { - free(yank_files.list[i]); - } - free(yank_files.list); - free(yank_files.path); - yank_files.count = 0; - yank_files.status = 0; - } - yank_files.path=getcwd(NULL, 0); - yank_files.count = 0; - - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - yank_files.count++; - } - } - if (yank_files.count == 0) { - yank_files.count = 1; - yank_files.list = (char**)malloc(yank_files.count * sizeof(char*)); - *yank_files.list = malloc(strlen(mid_content[selected_file_current].file_name)+1); - memcpy(*yank_files.list, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)+1); - } else { - yank_files.list = malloc(yank_files.count * sizeof(char*)); - for (i = 0; i < mid_file_count; i++) { - if (mid_content[i].status & FILE_STATUS_SELECTED) { - *yank_files.list = malloc(strlen(mid_content[i].file_name)+1); - memcpy(*yank_files.list, mid_content[i].file_name, strlen(mid_content[i].file_name)+1); - yank_files.list += 1; - } - } - yank_files.list -= yank_files.count; - } - yank_files.status |= YANK_IS_USED; - if (strncmp((char*)key_binding[index].black_magic, "cut", 3) == 0) { - yank_files.status |= YANK_CUT; - yank_files.status &= ~YANK_COPY; - } else { - yank_files.status |= YANK_COPY; - yank_files.status &= ~YANK_CUT; - } + TODO; } void paste(){ - unsigned long i; - for (i = 0; i < yank_files.count; i++) { - char *cmd; - if (yank_files.status & YANK_COPY) { - concat(cmd, "false | cp -ri \'", yank_files.path, 0); - } else { - concat(cmd, "mv \'", yank_files.path, 0); - } - concat(cmd, cmd, "/", 1); - concat(cmd, cmd, *yank_files.list, 1); - concat(cmd, cmd, "\' ./", 1); - concat(cmd, cmd, " 2>&1", 1); - char *line = malloc(INPUT_BUFFER_SIZE); - FILE *cmd_open; - while (1) { - cmd_open = popen(cmd, "r"); - if (fgets(line, INPUT_BUFFER_SIZE, cmd_open) == 0) { - break; - } - if (strstr(line, "are the same file")) { - cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; - concat(cmd, cmd, "_", 1); - concat(cmd, cmd, " 2>&1", 1); - } else if ((strstr(line, "overwrite"))) { - cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; - concat(cmd, cmd, "_", 1); - concat(cmd, cmd, " 2>&1", 1); - } else if ((strstr(line, "No such file or directory"))) { - pclose(cmd_open); - break; - } else if ((strstr(line, "into itself"))) { - pclose(cmd_open); - break; - } - if (pclose(cmd_open) == 0) { - break; - } - } - - free(cmd); - - yank_files.list++; - } - yank_files.list -= yank_files.count; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); + TODO; } void search(){ - pthread_mutex_lock(&mutex_btm); - - unsigned long local_height; - local_height = getmaxy(win_b); - memset(search_buffer, '\0', INPUT_BUFFER_SIZE); - - window_btm(win_b, 1); - - curs_set(1); - - timeout(-1); /* negative numbers block until enter is pressed */ - - unsigned int pass = 0; - char ch; - - wmove(win_b, local_height-1, 1); - while(1) { - /*ch = mvwgetch(win, y, x + pass);*/ - werase(win_b); - mvwaddch(win_b, local_height-1, 0, '/'); - mvwaddstr(win_b, local_height-1, 1, search_buffer); - ch = wgetch(win_b); - if (ch == '\n') { - break; - } else if (ch == '\t') { /* tab */ - memcpy(search_buffer, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)); - mvwaddstr(win_b, local_height-1, pass, mid_content[selected_file_current].file_name); - pass = strlen(mid_content[selected_file_current].file_name); - } else if (ch == 127) { /* backspace */ - mvwdelch(win_b, local_height-1, 1); - wdelch(win_b); - if (pass != 0) { - search_buffer[pass-1] = '\0'; - pass--; - } - } else if (ch == 27) { /* esc key */ - break; - } else { - search_buffer[pass] = ch; - pass++; - unsigned long i; - for (i = 0; i < mid_file_count; i++) { - if (smartstrcasestr(mid_content[i].file_name, search_buffer)) { - selected_file_current = i; - if (update_selected_file()) { - pthread_cond_signal(&cond_rgt); - } - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - break; - } - render_pass(); - } - } - } - search_buffer[pass] = '\0'; - - curs_set(0); - - current_dir->selected_file_current = selected_file_current; - - update_selected_file(); - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - pthread_mutex_unlock(&mutex_btm); + TODO; } void search_next(){ - unsigned long i; - for (i = selected_file_current+1; i < mid_file_count; i++) { - if (smartstrcasestr(mid_content[i].file_name, search_buffer)) { - selected_file_current = i; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - update_selected_file(); - render_pass(); - break; - } - } + TODO; } void search_previous(){ - unsigned long i; - for (i = selected_file_current-1;; i--) { - if(i > selected_file_current) { - break; - } - if (smartstrcasestr(mid_content[i].file_name, search_buffer)) { - selected_file_current = i; - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - update_selected_file(); - render_pass(); - break; - } - } + TODO; } void jmp_file_index(){ - char *index = malloc(INPUT_BUFFER_SIZE); - memset(index, ' ', INPUT_BUFFER_SIZE); - index[INPUT_BUFFER_SIZE-1] = '\0'; - unsigned long local_height; - local_height = getmaxy(win_b); - read_string(win_b, local_height - 1, 0, index); - - unsigned long new_index = 0; - while((*index >= '0') && (*index <= '9')) { - new_index = (new_index * 10) + (*index - '0'); - index++; - } - if (new_index > mid_file_count) { - selected_file_current = mid_file_count; - } else { - selected_file_current = new_index; - } - - status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0); - update_selected_file(); + TODO; } diff --git a/main.c b/main.c index 4e20845..0d3296f 100644 --- a/main.c +++ b/main.c @@ -97,7 +97,7 @@ int main(){ pthread_cond_signal(&cond_mid); pthread_cond_signal(&cond_lft); status &= ~(STATUS_RUN_BACKEND); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } else { status &= ~(STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME); } @@ -136,10 +136,6 @@ int main(){ void render_pass(){ if (status & STATUS_UPDATE_SCREEN_RESIZE) { - if (status & STATUS_UPDATE_SCREEN_RELOAD_FULL) { - clear(); - status &= ~STATUS_UPDATE_SCREEN_RELOAD_FULL; - } wresize(win_t, 1, terminal_width); wresize(win_l, terminal_height-2, terminal_width/8); @@ -154,22 +150,20 @@ void render_pass(){ mvwin(win_r, 1, ((terminal_width/2))); mvwin(win_b, terminal_height-1, 0); - - status |= STATUS_UPDATE_SCREEN_0; } if (status & STATUS_UPDATE_SCREEN_MASK) { - status &= ~(STATUS_UPDATE_SCREEN_MASK); window_top(win_t); window_lft(win_l); window_mid(win_m); window_rgt(win_r); - window_btm(win_b, 0); + window_btm(win_b); wrefresh(win_t); wrefresh(win_l); wrefresh(win_m); wrefresh(win_r); wrefresh(win_b); + status &= ~(STATUS_UPDATE_SCREEN_MASK); } } /*this function exists for things done at startup (initialization, reading config, etc)*/ diff --git a/sorting.c b/sorting.c index b8973e9..58a1b23 100644 --- a/sorting.c +++ b/sorting.c @@ -117,18 +117,21 @@ int sort_random(const void *file0, const void *file1){ if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) { return 1; } - time_t num = ((time_t)&sort_random) + (time_t)seed + getpid(); + /* seed is the only value that actually changes whenever this funcion is called. + * seed only updates when the order_by binding is executed. + * this means that the random output only changes on the command of the user, and not SETTINGS_RELOAD_DIR_DELTA, this is intentional. + * well the other stuff like file sizes and file count in a dir may also change, but this needs user intervention too. + * that is unless the user decides to sort random in /var/log or something, which begs the question: why would you do that? + */ + time_t num = (((time_t)&sort_random) + (time_t)seed) ^ (((file*)file0)->file_size + ((file*)file1)->file_size); int i; - for (i = 1; i < (((time_t)&seed)%10); i++) { - num += getpid() / seed; - num ^= num << ((((time_t)seed)%11)+i); - num += ((file*)file0)->file_size; - num ^= num >> ((((time_t)seed)%7)+i); - num -= ((file*)file1)->file_size; + for (i = 1; i < (((time_t)&getpid)%3); i++) { + num ^= num << ((((time_t)&file0)%27)+i); + num ^= num >> ((((time_t)&file1)%18)+i); + num ^= ((time_t)&getpid) ^ (((time_t)&getpid) >> ((time_t)getpid())%10); } - /*printf("%ld\n", ((num & (3 << seed%16)) >> seed%16) - 1);*/ - return (((num & (3 << seed%32)) >> seed%32) - 1); + return (((num & (3 << seed%16)) >> seed%16) - 1); /*return ((num%3) - 1);*/ } diff --git a/string.h b/string.h new file mode 100644 index 0000000..d96667d --- /dev/null +++ b/string.h @@ -0,0 +1,119 @@ +#include +#ifndef BACKEND_GUARD +#define BACKEND_GUARD +#include "defines.h" + +#define SETTINGS_COMMAND_REPLACE_STR "%" + +typedef struct String { + char *data; + size_t size; +} string; + +#define s(str) { \ + return (string) { \ + .data = str; \ + .size = strlen(str); \ + } \ +} + +#define concat(out, s1, s2, _free) { \ + concat## _free(out, s1, s2); \ +} + +#define concat0(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + out = result; + + +#define concat1(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s1); \ + out = result; + +#define concat2(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s2); \ + out = result; + +#define concat3(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s1); \ + free(s2); \ + out = result; + +/* +int s_strcmp(const char *s0, char string *s1) { + size_t size = s1->size - s0->size; + if (size) { + return size; + } + size = s0.size; + while (*s0->data == *s0->data && size) { + s0->data += 1; + s1->data += 1; + size--; + } + return size; +} +*/ +char* smartstrcasestr(const char *haystack, const char *needle){ + char smart = 0; + char *ret; + char passes = 0; + while (*needle) { + if (*needle >= 'A' && *needle <= 'Z') { + smart = 1; + break; + } + passes++; + needle++; + } + needle -= passes; + if (smart == 0) { + char *needle_case = malloc(strlen(needle)+1); + memcpy(needle_case, needle, strlen(needle)+1); + passes = 0; + while (*needle_case) { + *needle_case = *needle_case | ' '; + needle_case++; + passes++; + } + needle_case -= passes; + + char *haystack_case = malloc(strlen(haystack)+1); + memcpy(haystack_case, haystack, strlen(haystack)+1); + passes = 0; + while (*haystack_case) { + *haystack_case = *haystack_case | ' '; + haystack_case++; + passes++; + } + haystack_case -= passes; + + ret = strstr(haystack_case, needle_case); + free(needle_case); + free(haystack_case); + } else { + ret = strstr(haystack, needle); + } + return ret; +} + + + + + +#endif + + +/*char* concat(const char *s1, const char *s2);*/ +char* smartstrcasestr(const char *haystack, const char *needle); diff --git a/threading.c b/threading.c index 1945b33..767197f 100644 --- a/threading.c +++ b/threading.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -27,24 +26,19 @@ pthread_cond_t cond_top; pthread_cond_t cond_btm; -file *rgt_content; -file *mid_content; -file *lft_content; +dir rgt_dir; +dir mid_dir; +dir lft_dir; char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */ char *btm_buffer; char *top_buffer; /* current path */ -unsigned long rgt_file_count = 0; -unsigned long mid_file_count = 0; -unsigned long lft_file_count; unsigned long top_width; -volatile unsigned long selected_file_current = 0; -volatile unsigned long selected_file_last = 0; extern unsigned int terminal_width; extern unsigned int status; extern char *global_path; @@ -60,38 +54,34 @@ void *thread_mid(){ unsigned int local_status = status; if (global_path == NULL) { - mid_content = malloc(sizeof(file)); - mid_content->file_name = "cannot open directory"; - mid_file_count = 1; - pthread_mutex_unlock(&mutex_mid); + mid_dir.current_file = NULL; + mid_dir.file_count = 0; continue; } char *path = malloc(strlen(global_path)+1); memcpy(path, global_path, strlen(global_path)+1); if (local_status & STATUS_RELOAD_DIRECTORY) { - unsigned long i = 0; - for (i = 0; i < mid_file_count; i++) { - free(mid_content[i].file_name); - } - free(mid_content); - mid_file_count = get_dir_size(path); - if (mid_file_count != 0) { - mid_content = malloc(mid_file_count * sizeof(file)); - memset(mid_content, '\0', mid_file_count * sizeof(file)); - get_dir_content(path, &mid_file_count, mid_content); - } else { - selected_file_current = 0; - mid_content = malloc(sizeof(file)); - mid_content->file_type = 0; - mid_content->file_size = 0; - mid_content->permissions = 0; - mid_content->color_pair = 0; - mid_content->file_name = ""; + long index = (long)mid_dir.current_file; - mid_file_count = 0; + dir tmp; + tmp.file_count = get_dir_size(global_path); + tmp.file_list = malloc(tmp.file_count * sizeof(file)); + if (tmp.file_count) { /* fails if dir empty */ + get_dir_content(global_path, &tmp); + } else { /* the hovered dir is empty */ + tmp.current_file = NULL; } + long i; + for (i = 0; i < mid_dir.file_count; i++) { + free(mid_dir.file_list[i].file_name); + } + free(mid_dir.file_list); + mid_dir.file_list = tmp.file_list; + mid_dir.current_file = tmp.file_list; + mid_dir.file_count = tmp.file_count; + update_selected_file(); } @@ -112,9 +102,8 @@ void *thread_lft(){ unsigned int local_status = status; if (global_path == NULL) { - lft_content = malloc(sizeof(file)); - lft_content[0].file_name = "cannot open directory"; - lft_file_count = 1; + lft_dir.current_file = NULL; + lft_dir.file_count = 0; pthread_mutex_unlock(&mutex_lft); continue; } @@ -127,14 +116,23 @@ void *thread_lft(){ path[0] = '/'; if (local_status & STATUS_RELOAD_DIRECTORY) { - lft_file_count = get_dir_size(path); - free(lft_content); - lft_content = malloc(lft_file_count * sizeof(file)); - memset(lft_content, '\0', lft_file_count * sizeof(file)); - get_dir_content(path, &lft_file_count, lft_content); + unsigned long i = 0; + for (i = 0; i < lft_dir.file_count; i++) { + free(lft_dir.file_list[i].file_name); + } + free(lft_dir.file_list); + lft_dir.file_count = get_dir_size(path); + if (lft_dir.file_count != 0) { + lft_dir.file_list = malloc(lft_dir.file_count * sizeof(file)); + get_dir_content(path, &lft_dir); + } else { + lft_dir.current_file = NULL; + lft_dir.file_count = 0; + + } } } else { - lft_file_count = 0; + lft_dir.file_count = 0; } @@ -147,84 +145,86 @@ void *thread_lft(){ } void *thread_rgt(){ - file file_current; - file_current.file_name = NULL; while(!(status & STATUS_QUIT_PROGRAM)){ pthread_mutex_lock(&mutex_rgt); pthread_cond_wait(&cond_rgt, &mutex_rgt); pthread_mutex_lock(&mutex_mid); - free(file_current.file_name); - memcpy(&file_current, &mid_content[selected_file_current], sizeof(file)); - file_current.file_name = malloc(strlen(mid_content[selected_file_current].file_name)); - memcpy(file_current.file_name, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name)+1); + + rgt_dir.current_file = mid_dir.current_file; + rgt_dir.file_count = 1; + rgt_dir.file_list = malloc(sizeof(file)); + + memcpy(rgt_dir.file_list, rgt_dir.current_file, sizeof(file)); + rgt_dir.file_list->file_name = malloc(strlen(rgt_dir.current_file->file_name)+1); + memcpy(rgt_dir.file_list->file_name, rgt_dir.current_file->file_name, strlen(rgt_dir.current_file->file_name)+1); + rgt_dir.current_file = rgt_dir.file_list; + pthread_mutex_unlock(&mutex_mid); - if (file_current.permissions & S_IRUSR) { - if (file_current.file_type &= FILE_TYPE_DIR) { + if (rgt_dir.current_file->permissions & S_IRUSR) { + if (rgt_dir.current_file->file_type &= FILE_TYPE_DIR) { #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 images_clear(); #endif - unsigned long i = 0; - for (i = 0; i < rgt_file_count; i++) { - if (rgt_content[i].file_name) { - free(rgt_content[i].file_name); - } - } - free(rgt_content); - - rgt_file_count = get_dir_size(file_current.file_name); - if (rgt_file_count) { /* fails if dir empty */ - rgt_content = malloc(rgt_file_count * sizeof(file)); - memset(rgt_content, '\0', rgt_file_count * sizeof(file)); - get_dir_content(file_current.file_name, &rgt_file_count, rgt_content); - rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN; - - free(rgt_buffer); - rgt_buffer = malloc(sizeof(char)); - rgt_buffer[0] = '\0'; + dir tmp; + tmp.file_count = get_dir_size(rgt_dir.current_file->file_name); + tmp.file_list = malloc(tmp.file_count * sizeof(file)); + if (tmp.file_count) { /* fails if dir empty */ + get_dir_content(rgt_dir.current_file->file_name, &tmp); } else { /* the hovered dir is empty */ - rgt_content = malloc(sizeof(file)); - rgt_content->file_type = 0; - rgt_content->permissions = mid_content[selected_file_current].permissions; + tmp.current_file = NULL; } - } else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME && mid_file_count > 0) { + long i; + for (i = 0; i < rgt_dir.file_count; i++) { + free(rgt_dir.file_list[i].file_name); + } + free(rgt_dir.file_list); + rgt_dir.file_list = tmp.file_list; + rgt_dir.current_file = tmp.file_list; + rgt_dir.file_count = tmp.file_count; + + } else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME && mid_dir.file_count > 0) { - unsigned long i = 0; - for (i = 0; i < rgt_file_count; i++) { - if (rgt_content[i].file_name) { - free(rgt_content[i].file_name); - } - } - free(rgt_content); - rgt_file_count = 0; - rgt_content = malloc(sizeof(file)); free(rgt_buffer); - rgt_content->file_type = FILE_TYPE_OPEN_FILE; - rgt_content->status = FILE_STATUS_HOVER; - rgt_buffer = preview_file(&file_current); + rgt_dir.current_file->status = FILE_STATUS_HOVER; + rgt_buffer = preview_file(rgt_dir.current_file); + rgt_dir.current_file->file_type = FILE_TYPE_OPEN_FILE; + + + /* + unsigned long i = 0; + for (i = 0; i < rgt_dir.file_count; i++) { + if (rgt_dir.file_list[i].file_name) { + free(rgt_dir.file_list[i].file_name); + } + } + free(rgt_dir.file_list); + rgt_dir.file_count = 0; + rgt_dir.file_list = malloc(sizeof(file)); + rgt_dir.current_file = rgt_dir.file_list; + */ + } } else { unsigned long i = 0; - for (i = 0; i < rgt_file_count; i++) { - if (rgt_content[i].file_name) { - free(rgt_content[i].file_name); + for (i = 0; i < rgt_dir.file_count; i++) { + if (rgt_dir.current_file[i].file_name) { + free(rgt_dir.current_file[i].file_name); } } - free(rgt_content); - rgt_file_count = 0; - rgt_content = malloc(sizeof(file)); + free(rgt_dir.file_list); + rgt_dir.file_count = 0; + rgt_dir.file_list = malloc(sizeof(file)); + rgt_dir.current_file = rgt_dir.file_list; free(rgt_buffer); rgt_buffer = malloc(sizeof(char)); rgt_buffer[0] = '\0'; } - /* - free(path); - */ pthread_mutex_unlock(&mutex_rgt); } @@ -248,7 +248,7 @@ void *thread_top(){ top_buffer = malloc(strlen(global_path)+1); memcpy(top_buffer, global_path, strlen(global_path)+1); top_width = strlen(top_buffer); - /*printing of mid_content[selected_file_current].file_name is done directly in window.c window_top()*/ + /*printing of mid_dir.current_file->file_name is done directly in window.c window_top()*/ pthread_mutex_unlock(&mutex_top); } @@ -271,9 +271,9 @@ void *thread_btm(){ pthread_mutex_lock(&mutex_mid); unsigned long i; float total_dir_size = 0; - for(i = 0; i < mid_file_count; i++) { - if ((mid_content[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) { - total_dir_size += mid_content[i].file_size; + for(i = 0; i < mid_dir.file_count; i++) { + if ((mid_dir.file_list[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) { + total_dir_size += mid_dir.file_list[i].file_size; } } pthread_mutex_unlock(&mutex_mid); @@ -347,17 +347,17 @@ void *thread_btm(){ memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size); - btm_buffer[0] = (S_ISLNK(mid_content[selected_file_current].permissions)) ? 'l': - (S_ISDIR(mid_content[selected_file_current].permissions) ? 'd': '-'); - btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-'; - btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-'; - btm_buffer[3] = (mid_content[selected_file_current].permissions & S_IXUSR) ? 'x' : '-'; - btm_buffer[4] = (mid_content[selected_file_current].permissions & S_IRGRP) ? 'r' : '-'; - btm_buffer[5] = (mid_content[selected_file_current].permissions & S_IWGRP) ? 'w' : '-'; - btm_buffer[6] = (mid_content[selected_file_current].permissions & S_IXGRP) ? 'x' : '-'; - btm_buffer[7] = (mid_content[selected_file_current].permissions & S_IROTH) ? 'r' : '-'; - btm_buffer[8] = (mid_content[selected_file_current].permissions & S_IWOTH) ? 'w' : '-'; - btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-'; + btm_buffer[0] = (S_ISLNK(mid_dir.current_file->permissions)) ? 'l': + (S_ISDIR(mid_dir.current_file->permissions) ? 'd': '-'); + btm_buffer[1] = (mid_dir.current_file->permissions & S_IRUSR) ? 'r' : '-'; + btm_buffer[2] = (mid_dir.current_file->permissions & S_IWUSR) ? 'w' : '-'; + btm_buffer[3] = (mid_dir.current_file->permissions & S_IXUSR) ? 'x' : '-'; + btm_buffer[4] = (mid_dir.current_file->permissions & S_IRGRP) ? 'r' : '-'; + btm_buffer[5] = (mid_dir.current_file->permissions & S_IWGRP) ? 'w' : '-'; + btm_buffer[6] = (mid_dir.current_file->permissions & S_IXGRP) ? 'x' : '-'; + btm_buffer[7] = (mid_dir.current_file->permissions & S_IROTH) ? 'r' : '-'; + btm_buffer[8] = (mid_dir.current_file->permissions & S_IWOTH) ? 'w' : '-'; + btm_buffer[9] = (mid_dir.current_file->permissions & S_IXOTH) ? 'x' : '-'; @@ -367,9 +367,6 @@ void *thread_btm(){ } void threading_init(){ - rgt_content = malloc(sizeof(file)); - mid_content = malloc(sizeof(file)); - lft_content = malloc(sizeof(file)); top_buffer = malloc(sizeof(char)); rgt_buffer = malloc(sizeof(char)); @@ -378,13 +375,6 @@ void threading_init(){ memset(rgt_buffer, '\0', sizeof(char)); memset(btm_buffer, '\0', sizeof(char)); - mid_content->file_type = 0; - mid_content->file_size = 0; - mid_content->file_name = ""; - - rgt_content->file_type = 0; - rgt_content->file_size = 0; - rgt_content->file_name = ""; volatile char vol; /* needed to make sure higher optimization steps dont move these around */ @@ -400,13 +390,8 @@ void threading_init(){ vol = pthread_cond_init(&cond_top, NULL); vol = pthread_cond_init(&cond_btm, NULL); vol; - selected_file_current = 0; - selected_file_last = 0; } void threading_free(){ - free(rgt_content); - free(mid_content); - free(lft_content); free(top_buffer); pthread_mutex_destroy(&mutex_top); diff --git a/window.c b/window.c index 20ca1fc..eeeaabd 100644 --- a/window.c +++ b/window.c @@ -14,17 +14,14 @@ extern unsigned int timeout_time; extern unsigned int color_count; extern color *colors; -extern file *mid_content; -extern file *lft_content; -extern file *rgt_content; +extern dir rgt_dir; +extern dir mid_dir; +extern dir lft_dir; extern char *top_buffer; extern char *rgt_buffer; extern char *btm_buffer; -extern unsigned long lft_file_count; -extern unsigned long mid_file_count; -extern unsigned long rgt_file_count; extern unsigned long top_width; extern pthread_mutex_t mutex_top; @@ -42,17 +39,17 @@ void window_top(WINDOW *win){ mvwaddstr(win, 0, 0, top_buffer); mvwaddch(win, 0, strlen(top_buffer), '/'); wattroff(win, COLOR_PAIR(COLOR_PATH)); - if (mid_file_count != 0) { - mvwaddstr(win, 0, strlen(top_buffer)+1, mid_content[selected_file_current].file_name); + if (mid_dir.file_count != 0) { + mvwaddstr(win, 0, strlen(top_buffer)+1, mid_dir.current_file->file_name); } } pthread_mutex_unlock(&mutex_top); } else { mvwaddstr(win, 0, terminal_width/2, "LOADING"); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } } -void window_btm(WINDOW *win, char force_render){ +void window_btm(WINDOW *win){ werase(win); if (pthread_mutex_trylock(&mutex_btm) == 0) { @@ -62,62 +59,55 @@ void window_btm(WINDOW *win, char force_render){ pthread_mutex_unlock(&mutex_btm); /*the printing of the input char is done in user_interactions*/ /*the printing of all possible inputs are done in user_interactions */ - } else if (force_render) { - /*force_render is used in stuff like open_with, search, and other functions that take over win_b, - * while needing to avoid possible conflicts like thread_btm itself*/ - mvwprintw(win, 0, 0, "%s", btm_buffer); - } else { + } else { mvwaddstr(win, 0, terminal_width/2, "LOADING"); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } } void window_lft(WINDOW *win){ werase(win); if (pthread_mutex_trylock(&mutex_lft) == 0) { - print_dir(win, 0, &lft_file_count, lft_content); + print_dir(win, 0, &lft_dir); pthread_mutex_unlock(&mutex_lft); } else { mvwaddstr(win, terminal_height/2, terminal_width/8, "LOADING"); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } } void window_mid(WINDOW *win){ werase(win); if (pthread_mutex_trylock(&mutex_mid) == 0) { - if (mid_file_count == 0) { + if (mid_dir.file_count == 0) { mvwaddstr(win, 0, 0, "empty"); } else { - print_dir(win, 1, &mid_file_count, mid_content); + print_dir(win, 1, &mid_dir); } pthread_mutex_unlock(&mutex_mid); } else { mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING"); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } } void window_rgt(WINDOW *win){ werase(win); if (pthread_mutex_trylock(&mutex_rgt) == 0) { - if (rgt_file_count == 0) { - if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) { - mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width); - } else if (rgt_content->permissions & S_IRUSR) { - mvwaddstr(win, 0, 0, "not accessible"); - } else { - mvwaddstr(win, 0, 0, "empty"); - } - - + if (!rgt_dir.current_file) { + mvwaddstr(win, 0, 0, "not accessible"); + }else if (rgt_dir.current_file->file_type == FILE_TYPE_OPEN_FILE) { + mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width); + } else if ((rgt_dir.current_file->permissions & S_IRUSR) == 0) { + mvwaddstr(win, 0, 0, "not accessible"); } else { - print_dir(win, 0, &rgt_file_count, rgt_content); + print_dir(win, 0, &rgt_dir); } + pthread_mutex_unlock(&mutex_rgt); } else { mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING"); - status |= STATUS_UPDATE_SCREEN_0; + status |= STATUS_UPDATE_SCREEN_PRINTED; } } diff --git a/window.h b/window.h index 7abda83..f1ce2df 100644 --- a/window.h +++ b/window.h @@ -1,7 +1,7 @@ #include "window.c" void window_top(WINDOW *win); -void window_btm(WINDOW *win, char force_render); +void window_btm(WINDOW *win); void window_lft(WINDOW *win); void window_mid(WINDOW *win); void window_rgt(WINDOW *win);