implementation of keeping the index of current file in between dir changes

This commit is contained in:
nova
2026-05-25 17:36:39 +02:00
parent e42d55e66a
commit d33ac88de2
3 changed files with 56 additions and 17 deletions

60
dir.c
View File

@@ -17,10 +17,11 @@ extern unsigned int settings;
extern unsigned int file_modifiers;
extern unsigned int color_count;
extern unsigned int terminal_height;
extern char *global_path;
extern color *colors;
int (*order_func)() = sort_natural;
linked_dir *visited_dirs;
linked_dir *current_dir;
linked_dir *list_beginning;
linked_dir *current_linked_dir;
unsigned long get_dir_size(char *path){
@@ -294,13 +295,58 @@ void print_dir(WINDOW *win, char print_info, dir *dir){
}
}
void dir_changed(){
char *path = getcwd(NULL, 0);
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, path) == 0) {
mid_dir.current_file = &mid_dir.file_list[current_linked_dir->index];
} else {
/*this path should only ever happen on changing the path for the first time
*at least i hope so*/
current_linked_dir->next = malloc(sizeof(linked_dir));
current_linked_dir->next->path = malloc(strlen(global_path)+1);
memcpy(current_linked_dir->next->path, global_path, strlen(global_path)+1);
current_linked_dir->next->next = NULL;
current_linked_dir->next->index = 0;
}
}
void change_dir(){
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, global_path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, global_path) == 0) {
current_linked_dir->index = mid_dir.current_file - mid_dir.file_list;
} else {
current_linked_dir->next = malloc(sizeof(linked_dir));
current_linked_dir->next->path = malloc(strlen(global_path)+1);
memcpy(current_linked_dir->next->path, global_path, strlen(global_path)+1);
current_linked_dir->next->next = NULL;
current_linked_dir->next->index = mid_dir.current_file - mid_dir.file_list;
}
}
void dir_init(){
visited_dirs = malloc(sizeof(linked_dir));
visited_dirs->path = getcwd(NULL, 0);
visited_dirs->selected_file_current = 0;
visited_dirs->next = NULL;
current_dir = visited_dirs;
list_beginning = malloc(sizeof(linked_dir));
list_beginning->path = getcwd(NULL, 0);
list_beginning->index = 0;
list_beginning->next = NULL;
current_linked_dir = list_beginning;
}