From d9ae5c79aff1be09403f535c2a45826b0118a5db Mon Sep 17 00:00:00 2001 From: nova Date: Tue, 15 Jul 2025 02:45:45 +0200 Subject: [PATCH] improvenments to the rendered output, trunking of longer file names, no more dir sizes and indexes in lft and rgt --- backend.c | 90 +++++++++++++++++++++++++++++++++++++++---------------- backend.h | 2 +- window.c | 6 ++-- 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/backend.c b/backend.c index 8c48b2f..2f29b2c 100644 --- a/backend.c +++ b/backend.c @@ -145,7 +145,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten } -void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content){ +void print_dir(WINDOW *win, char print_info, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content){ /* i am not proud of this function */ char *bg = malloc(*line_width); @@ -165,23 +165,25 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c offset_front = (snprintf(NULL, 0, "%ld", *dir_file_count)) + 1; } for (i = file_offset; i < *dir_file_count && i < terminal_height; i++) { - file_size = dir_content[i].file_size; - char size_index = 0; - while (file_size > 1) { - printed_size=file_size; - file_size /= 1024; - size_index++; - if (size_index >= 6) { - break; + if (print_info) { + file_size = dir_content[i].file_size; + char size_index = 0; + while (file_size > 1) { + printed_size=file_size; + file_size /= 1024; + size_index++; + if (size_index >= 6) { + break; + } + } + size_char = sizes[size_index-1]; + if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) { + offset_back = *line_width - (snprintf(NULL,0,"%ld", dir_content[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 { + offset_back = *line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1); } - } - size_char = sizes[size_index-1]; - if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) { - offset_back = *line_width - (snprintf(NULL,0,"%ld", dir_content[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 { - offset_back = *line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1); } if (dir_content[i].status & FILE_STATUS_SELECTED) { @@ -200,15 +202,53 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c wattron(win, A_REVERSE); } - mvwaddstr(win, i, 0, bg); - mvwprintw(win, i, 0, "%ld", i); - mvwaddnstr(win, i, offset_front+is_selected, dir_content[i].file_name, *line_width); - if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) { - mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size); - }else if (size_char =='B') { - mvwprintw(win, i, offset_back, "%0.0lf %c", printed_size, size_char); + /* shortens the printed file name if it is too long + * example input: aaaaaaaa.txt + * example output: aaa~.txt + * if no extension is found, the name will truncate */ + char *file_name; + if ((dir_content[i].file_name_width + offset_front + is_selected) > offset_back - 1) { + char *extension = strrchr(dir_content[i].file_name, '.'); + if (extension) { + int char_offset = (dir_content[i].file_name_width + offset_front + is_selected) - (offset_back - 1) ; + if ((dir_content[i].file_name_width - char_offset - strlen(extension) - 1) > 1) { + file_name = malloc(dir_content[i].file_name_width - char_offset); + memcpy(file_name, dir_content[i].file_name, dir_content[i].file_name_width - char_offset); + memcpy(file_name + (dir_content[i].file_name_width - char_offset - strlen(extension)), extension, strlen(extension)); + file_name[dir_content[i].file_name_width - char_offset - strlen(extension) - 1] = '~'; + file_name[dir_content[i].file_name_width - char_offset] = '\0'; + } else { + file_name = malloc(strlen(extension)+1); + file_name[0] = '~'; + memcpy(file_name+1, extension, strlen(extension)); + file_name[strlen(extension)] = '\0'; + } + } else { + file_name = malloc(dir_content[i].file_name_width); + strcpy(file_name, dir_content[i].file_name); + } + } else { - mvwprintw(win, i, offset_back, "%0.2lf %c", printed_size, size_char); + file_name = malloc(dir_content[i].file_name_width); + strcpy(file_name, dir_content[i].file_name); + } + + mvwaddstr(win, i, 0, bg); + if(print_info) { + mvwprintw(win, i, 0, "%ld", i); + mvwaddnstr(win, i, offset_front+is_selected, file_name, *line_width-offset_front-is_selected-2); + free(file_name); + + if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) { + mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size); + }else if (size_char =='B') { + mvwprintw(win, i, offset_back, "%0.0lf %c", printed_size, size_char); + } else { + mvwprintw(win, i, offset_back, "%0.2lf %c", printed_size, size_char); + } + } else { + mvwaddnstr(win, i, 0, file_name, *line_width); + free(file_name); } if (dir_content[i].status & FILE_STATUS_HOVER) { diff --git a/backend.h b/backend.h index b74c6a7..3b3a71c 100644 --- a/backend.h +++ b/backend.h @@ -4,4 +4,4 @@ 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, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content); +void print_dir(WINDOW *win, char print_info, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content); diff --git a/window.c b/window.c index f144988..1ff3b9b 100644 --- a/window.c +++ b/window.c @@ -69,7 +69,7 @@ void window_lft(WINDOW *win){ unsigned long local_height; getmaxyx(win, local_height, local_width); if (pthread_mutex_trylock(&mutex_lft) == 0) { - print_dir(win, &local_width, &lft_file_count, lft_content); + print_dir(win, 0, &local_width, &lft_file_count, lft_content); pthread_mutex_unlock(&mutex_lft); } else { @@ -88,7 +88,7 @@ void window_mid(WINDOW *win){ if (mid_file_count == 0) { mvwprintw(win, 0, 0, "empty"); } else { - print_dir(win, &local_width, &mid_file_count, mid_content); + print_dir(win, 1, &local_width, &mid_file_count, mid_content); } pthread_mutex_unlock(&mutex_mid); } else { @@ -113,7 +113,7 @@ void window_rgt(WINDOW *win){ } else { - print_dir(win, &local_width, &rgt_file_count, rgt_content); + print_dir(win, 0, &local_width, &rgt_file_count, rgt_content); } pthread_mutex_unlock(&mutex_rgt); } else {