handling of empty dirs & refactoring

This commit is contained in:
nova
2025-07-08 20:51:25 +02:00
parent 7fcd148dfe
commit b6f9633677
6 changed files with 98 additions and 41 deletions

View File

@ -50,15 +50,14 @@ void colors_init() {
FILE *dircolors = fopen("/etc/DIR_COLORS", "r");
if (dircolors) {
char *line;
char *line = NULL;
char *token;
char *extension;
int tmp = 0;
size_t size = 0;
short fg;
short bg;
while ((tmp = getline(&line, &size, dircolors)) != -1) {
while (getline(&line, &size, dircolors) != -1) {
fg = 7;
bg = 0;
token = strtok(line, " ");
@ -101,7 +100,7 @@ void colors_init() {
colors = malloc(sizeof(color) * color_count);
unsigned int i = 0;
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
while ((tmp = getline(&line, &size, dircolors)) != -1) {
while (getline(&line, &size, dircolors) != -1) {
fg = 7;
bg = 0;
if (line[0] == '.') {

View File

@ -24,6 +24,7 @@
#define FILE_STATUS_HOVER 1
#define FILE_STATUS_SELECTED 2
#define FILE_STATUS_IS_REGULAR_FILE 4
#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */
#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */
#define COLOR_UNKNOWN 0

View File

@ -59,7 +59,6 @@ void user_interactions() {
terminal_width_empty_line = malloc(terminal_width);
memset(terminal_width_empty_line, ' ', terminal_width);
move(terminal_height, terminal_width);
ch = getch();
if(ch != ERR) {
timeout(1); /* blocking timeout of getch() */

16
main.c
View File

@ -66,13 +66,14 @@ int main(){
temp_width = terminal_width;
temp_heigth = terminal_height;
}
if (status & STATUS_RUN_BACKEND) {
if (threading) {
pthread_cancel(thread_t);
pthread_cancel(thread_l);
pthread_cancel(thread_m);
pthread_cancel(thread_r);
if (status & STATUS_RUN_BACKEND && threading) {
pthread_cancel(thread_b);
pthread_cancel(thread_r);
pthread_cancel(thread_m);
pthread_cancel(thread_l);
pthread_cancel(thread_t);
}
if (threading) {
pthread_join(thread_b, NULL);
pthread_join(thread_r, NULL);
pthread_join(thread_m, NULL);
@ -80,14 +81,15 @@ int main(){
pthread_join(thread_t, NULL);
threading = 0;
}
if (status & STATUS_RUN_BACKEND) {
pthread_create(&thread_t, NULL, thread_top, win_t); /*top bar*/
pthread_create(&thread_l, NULL, thread_lft, win_l); /*current_content slash win_m*/
pthread_create(&thread_m, NULL, thread_mid, win_m); /*parent_content slash win_l*/
pthread_create(&thread_r, NULL, thread_rgt, win_r); /*child_content slash win_r*/
pthread_create(&thread_b, NULL, thread_btm, win_b); /*bottom bar*/
threading = 1;
status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
status |= STATUS_UPDATE_SCREEN_0;
threading = 1;
}
user_interactions();

View File

@ -51,8 +51,8 @@ void *thread_mid(void *data){
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof(file));
mid_content[0].file_name_width = sizeof("cannot open directory");
mid_content[0].file_name = "cannot open directory";
mid_content->file_name_width = sizeof("cannot open directory");
mid_content->file_name = "cannot open directory";
mid_file_count = 1;
} else {
@ -60,9 +60,40 @@ void *thread_mid(void *data){
if (status & STATUS_RELOAD_DIRECTORY) {
free(mid_content);
mid_file_count = (unsigned long)get_dir_size(path);
if (mid_file_count > 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, ' ', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
mid_content = malloc(sizeof(file));
mid_content->status = FILE_STATUS_DIR_EMPTY;
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->permissions = 0;
mid_content->color_pair = 0;
mid_content->file_name_width = sizeof("empty dir");
mid_content->file_name = "empty dir";
file_current->file_name = mid_content->file_name;
file_current->file_name_width = mid_content->file_name_width;
file_current->file_size = mid_content->file_size;
file_current->file_type = mid_content->file_type;
file_current->color_pair = mid_content->color_pair;
file_current->permissions = mid_content->permissions;
file_current->status = mid_content->status;
mid_file_count = 0;
while(wait_count < 2){
/*wait for thread_rgt and thread_btm to lock*/
}
pthread_mutex_lock(&mutex_wait);
pthread_cond_broadcast(&cond_wait);
pthread_mutex_unlock(&mutex_wait);
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0);
}
}
@ -81,7 +112,9 @@ void *thread_mid(void *data){
file_current->file_name_width = mid_content[selected_file_current].file_name_width;
file_current->file_size = mid_content[selected_file_current].file_size;
file_current->file_type = mid_content[selected_file_current].file_type;
file_current->color_pair = mid_content[selected_file_current].color_pair;
file_current->permissions = mid_content[selected_file_current].permissions;
file_current->status = mid_content[selected_file_current].status;
while(wait_count < 2){
/*wait for thread_rgt and thread_btm to lock*/
@ -97,7 +130,7 @@ void *thread_mid(void *data){
}
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(NULL);
pthread_exit(0);
}
void *thread_lft(void *data){
pthread_mutex_lock(&mutex_lft);
@ -124,19 +157,20 @@ void *thread_lft(void *data){
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(NULL);
pthread_exit(0);
}
void *thread_rgt(void *data){
pthread_mutex_lock(&mutex_rgt);
pthread_mutex_lock(&mutex_wait);
wait_count++;
pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
pthread_mutex_lock(&mutex_rgt);
pthread_mutex_lock(&mutex_mid);
free(rgt_content);
rgt_content = malloc(sizeof(file));
rgt_content->file_name = malloc(file_current->file_name_width + 1);
@ -144,10 +178,10 @@ void *thread_rgt(void *data){
rgt_content->file_name_width = file_current->file_name_width;
rgt_content->file_size = file_current->file_size;
rgt_content->file_type = file_current->file_type;
rgt_content->status = file_current->status;
pthread_mutex_unlock(&mutex_mid);
if (rgt_content[0].file_type == FILE_TYPE_DIR || rgt_content[0].file_type == FILE_TYPE_SYMLINK) {
if (rgt_content->file_type == FILE_TYPE_DIR || rgt_content->file_type == FILE_TYPE_SYMLINK) {
char *path = malloc(rgt_content[0].file_name_width + 1);
strcpy(path, rgt_content[0].file_name);
free(rgt_content);
@ -159,17 +193,21 @@ void *thread_rgt(void *data){
free(path);
} else {
rgt_file_count = 1;
rgt_file_count = 0;
if (rgt_content->status & FILE_STATUS_DIR_EMPTY) {
free(rgt_buffer);
rgt_buffer = "empty dir";
} else {
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
free(rgt_buffer);
rgt_buffer = preview_file(rgt_content);
}
}
pthread_mutex_unlock(&mutex_rgt);
pthread_exit(NULL);
pthread_exit(0);
}
void *thread_top(void *data){
pthread_mutex_lock(&mutex_top);
@ -187,7 +225,7 @@ void *thread_top(void *data){
free(path);
pthread_mutex_unlock(&mutex_top);
pthread_exit(NULL);
pthread_exit(0);
}
void *thread_btm(void *data){
pthread_mutex_lock(&mutex_wait);
@ -214,7 +252,7 @@ void *thread_btm(void *data){
pthread_mutex_unlock(&mutex_btm);
pthread_exit(NULL);
pthread_exit(0);
}
void threading_init(){
@ -226,6 +264,13 @@ void threading_init(){
rgt_buffer = malloc(sizeof(char));
btm_buffer = malloc(sizeof(char));
rgt_content[0].file_type = 0;
rgt_content[0].file_size = 1;
rgt_content[0].file_name_width = 1;
rgt_content[0].file_name = malloc(sizeof("a"));
strcpy(rgt_content[0].file_name, "a");
file_current = malloc(sizeof(file));
file_current->file_type = 0;
file_current->file_size = 1;

View File

@ -91,8 +91,12 @@ void window_mid(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0;
pthread_mutex_unlock(&mutex_rgt);
} else {
if (mid_file_count == 0) {
mvwprintw(win, 0, 0, "empty");
} else {
print_dir(win, &local_width, &mid_file_count, mid_content);
}
pthread_mutex_unlock(&mutex_mid);
}
}
@ -109,8 +113,15 @@ void window_rgt(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0;
pthread_mutex_unlock(&mutex_rgt);
} else {
if (rgt_file_count == 0) {
if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) {
mvwprintw(win, 0, 0, "%s", rgt_buffer);
} else {
mvwprintw(win, 0, 0, "empty");
}
} else {
print_dir(win, &local_width, &rgt_file_count, rgt_content);
}