From 4c958dc10a715d8c7441e04fbf4d890da237b2ac Mon Sep 17 00:00:00 2001 From: nova Date: Tue, 8 Jul 2025 01:49:02 +0200 Subject: [PATCH] now parsing file sizes --- backend.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/backend.c b/backend.c index 69af219..7d53599 100644 --- a/backend.c +++ b/backend.c @@ -12,6 +12,7 @@ extern unsigned int settings; extern unsigned int file_modifiers; extern unsigned int color_count; +extern unsigned int terminal_height; extern color *colors; unsigned long file_offset; int (*order_func)() = sort_natural; @@ -145,6 +146,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){ + /* i am not proud of this function */ char *hover_bg = malloc(*line_width+1); memset(hover_bg, ' ', *line_width); @@ -152,15 +154,40 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c unsigned long i = 0; unsigned long j = 0; + float file_size = 0; + float printed_size; + char size_char; char is_selected = 0; + static const char sizes[6] = { 'B', 'K', 'M', 'G', 'T', 'P' }; unsigned long offset_front = 2; if (*dir_file_count > 9) { offset_front = (snprintf(NULL, 0, "%ld", *dir_file_count)) + 1; } - for (i = file_offset; i < *dir_file_count; i++) { + for (i = file_offset; i < *dir_file_count && i < terminal_height; i++) { + unsigned long offset_back; + 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 { + file_size = dir_content[i].file_size; + long passes = 0; + while (file_size > 1 || passes > sizeof(sizes)) { + printed_size = file_size; + file_size /= 1024; + passes++; + } + if (passes) { + size_char = sizes[passes-1]; + } else { + size_char = sizes[0]; + } + 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); + } - unsigned long offset_back = *line_width - (snprintf(NULL,0,"%ld",dir_content[i].file_size) + 1); + } if (dir_content[i].status & FILE_STATUS_SELECTED) { is_selected = 1; @@ -182,7 +209,15 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c } mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]); } - mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size); + 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); + } + } wattroff(win, A_REVERSE); } else { mvwprintw(win, i, 0, "%ld", i); @@ -193,7 +228,15 @@ void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_c } mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]); } - mvwprintw(win, i, offset_back, "%ld", dir_content[i].file_size); + 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); + } + } } if (dir_content[i].status & FILE_STATUS_SELECTED) { wattroff(win, COLOR_PAIR(8));