1
0
mirror of https://gittea.dev/nova/th.git synced 2025-10-21 18:30:15 -04:00

Compare commits

...

2 Commits

Author SHA1 Message Date
nova
3f90802663 refractoring, fixed enter_shell 2025-08-18 22:01:16 +02:00
nova
f25768d522 linked list to keep track of past directory indexes 2025-08-18 21:07:18 +02:00
8 changed files with 87 additions and 7 deletions

View File

@@ -66,6 +66,7 @@ static const binding key_binding[] = {
{ "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" },
{ "ge", jump_to_dir, "/etc", "jump to /etc" },
{ "gp", jump_to_dir, "/etc/portage", "jump to /etc/portage" },
{ "gm", jump_to_dir, "/mnt", "jump to /mnt" },
{ "go", jump_to_dir, "/opt", "jump to /opt" },
{ "gt", jump_to_dir, "/tmp", "jump to /tmp" },

View File

@@ -91,4 +91,10 @@ typedef struct Yank {
char **list;
unsigned long count;
} yank;
typedef struct Linked_dir {
char *path;
unsigned long selected_file_current;
struct Linked_dir *next;
} linked_dir;
#endif

35
dir.c
View File

@@ -19,6 +19,8 @@ extern volatile unsigned long selected_file_current;
extern volatile unsigned long selected_file_last;
extern color *colors;
int (*order_func)() = sort_natural;
linked_dir *visited_dirs;
linked_dir *current_dir;
unsigned long get_dir_size(char *path);
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
@@ -332,3 +334,36 @@ void update_selected_file(){
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
file_current->status = mid_content[selected_file_current].status;
}
void dir_set_selected_file_current(unsigned long selected_file_current){
current_dir->selected_file_current = selected_file_current;
}
unsigned long dir_get_selected_file_current(){
current_dir = visited_dirs;
char hit = 0;
char *path = getcwd(NULL, 0);
while(current_dir->next != NULL) {
if (strcmp(path, current_dir->path) == 0) {
hit = 1;
break;
}
current_dir = current_dir->next;
}
if (hit == 0) {
current_dir->next = malloc(sizeof(linked_dir));
current_dir->next->next = NULL;
current_dir->next->path = path;
return 0;
} else {
free(path);
return current_dir->selected_file_current;
}
}
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;
}

3
dir.h
View File

@@ -7,3 +7,6 @@ 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 update_selected_file();
void dir_set_selected_file_current(unsigned long selected_file_current);
unsigned long dir_get_selected_file_current();
void dir_init();

View File

@@ -197,6 +197,7 @@ void move_down(int passes){
selected_file_current += passes;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
pthread_mutex_unlock(&mutex_selection);
@@ -213,6 +214,7 @@ void move_up(int passes){
}
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
@@ -226,6 +228,8 @@ void move_left(int passes){
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();
}
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
@@ -234,6 +238,8 @@ void move_right(){
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) {
if (chdir(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;
@@ -291,6 +297,7 @@ void jump_bottom(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0 - 1;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
@@ -298,6 +305,7 @@ void jump_top(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
@@ -515,16 +523,25 @@ void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void enter_shell(int passes, int index){
(void)passes;
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);
}
void not_implemented(int 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");
(void)passes;
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");
}
void jump_to_dir(int passes, int index){
(void)passes;
char *ch = (char*)key_binding[index].black_magic;
char slash = 0;
unsigned int env_len = 0;
@@ -566,6 +583,8 @@ void jump_to_dir(int passes, int index){
}
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*/
@@ -578,11 +597,14 @@ void jump_to_dir(int passes, int index){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void order_by(int passes, int index){
(void)passes;
order_func = key_binding[index].black_magic;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void cmd_on_selected(int passes, int index){
(void)passes;
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
@@ -665,6 +687,8 @@ void cmd_on_selected(int passes, int index){
}
}
void yank_file(int passes, int index){
(void)passes;
unsigned long i;
if (yank_files.status & YANK_IS_USED) {
free(yank_files.path);
@@ -774,8 +798,8 @@ void search(){
for (i = 0; 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();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
render_pass();
break;
}

5
main.c
View File

@@ -117,9 +117,9 @@ int main(){
delwin(win_t);
delwin(win_b);
noraw();
endwin();
curs_set(1);
echo();
curs_set(1);
endwin();
return 0;
}
@@ -191,6 +191,7 @@ void init() {
threading_init(); /* found in threading.c */
colors_init(); /* in colors.c */
ueberzug_init(); /* in file_previews.c */
dir_init(); /*in dir.c */
ESCDELAY = 10;
char *start_path = getcwd(NULL, 0);

View File

@@ -146,6 +146,8 @@ void *thread_lft(void *data){
}
void *thread_rgt(void *data){
unsigned int status = *(unsigned int*)data;
(void)status;
pthread_mutex_lock(&mutex_rgt);
pthread_mutex_lock(&mutex_wait);
wait_count++;
@@ -206,6 +208,8 @@ void *thread_rgt(void *data){
}
void *thread_top(void *data){
unsigned int status = *(unsigned int*)data;
(void)status;
pthread_mutex_lock(&mutex_top);
free(top_buffer);
@@ -225,6 +229,8 @@ void *thread_top(void *data){
}
void *thread_btm(void *data){
unsigned int status = *(unsigned int*)data;
(void)status;
pthread_mutex_lock(&mutex_btm);
pthread_mutex_lock(&mutex_wait);
wait_count++;

View File

@@ -1,5 +1,9 @@
#include <curses.h>
#ifndef THREADING_GUARD
#define THREADING_GUARD
#include "threading.c"
#endif
void *thread_lft(void *data);
void *thread_mid(void *data);