From c9c00f49300db7f9d7b230e1df9f109019fc458e Mon Sep 17 00:00:00 2001 From: nova Date: Sat, 12 Jul 2025 15:00:34 +0200 Subject: [PATCH] added unicode support --- Makefile | 6 +-- backend.c | 107 +++++++++++++++++++++--------------------------------- main.c | 2 + window.c | 2 +- 4 files changed, 48 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 2837c3a..45adea2 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ all: - gcc ./main.c -std=c89 -o th -lncurses -ltinfo -Wall -O2 + gcc ./main.c -std=c89 -o th -lncursesw -ltinfow -Wall d: - gcc -g -std=c89 ./main.c -o th -lncurses -ltinfo -Wall -O2 && gdb --tui ./th + gcc -g -std=c89 ./main.c -o th -lncursesw -ltinfow -Wall && gdb --tui ./th v: gcc -g -std=c89 ./main.c -o th -lncurses -ltinfo -O3 && valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --log-fd=9 9>>valgrind.log ./th rel: - gcc ./main.c -std=c89 -o th -lncurses -ltinfo -Wall -O2 -flto + gcc ./main.c -std=c89 -o th -lncursesw -ltinfow -Wall -O2 -flto diff --git a/backend.c b/backend.c index efec9d9..8c48b2f 100644 --- a/backend.c +++ b/backend.c @@ -148,102 +148,79 @@ 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); - hover_bg[*line_width] = '\0'; + char *bg = malloc(*line_width); + memset(bg, ' ', *line_width); + bg[*line_width] = '\0'; unsigned long i = 0; - unsigned long j = 0; - float file_size = 0; + float file_size; float printed_size = 0; char size_char = ' '; char is_selected = 0; static const char sizes[6] = { 'B', 'K', 'M', 'G', 'T', 'P' }; + unsigned long offset_back; 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 < terminal_height; i++) { - unsigned long offset_back; + 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); + 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 { - 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); - } - + offset_back = *line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1); } if (dir_content[i].status & FILE_STATUS_SELECTED) { is_selected = 1; - wattron(win, COLOR_PAIR(8)); } else { is_selected = 0; + } + + mvwaddnstr(win, i, 0, bg, offset_front+is_selected); + if (dir_content[i].status & FILE_STATUS_SELECTED) { + wattron(win, COLOR_PAIR(8)); + } else { wattron(win, COLOR_PAIR(dir_content[i].color_pair)); } + if (dir_content[i].status & FILE_STATUS_HOVER) { + 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); + } else { + mvwprintw(win, i, offset_back, "%0.2lf %c", printed_size, size_char); + } if (dir_content[i].status & FILE_STATUS_HOVER) { - wattron(win, A_REVERSE); - mvwprintw(win, i, 0, "%s", hover_bg); - mvwprintw(win, i, 0, "%ld", i); - - for (j = 0; j < dir_content[i].file_name_width ;j++) { - if (j >= offset_back-1) { - mvwaddch(win, i, j, '~'); - break; - } - mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]); - } - 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); - for (j = 0; j < dir_content[i].file_name_width ;j++) { - if (j >= offset_back-1) { - mvwaddch(win, i, j, '~'); - break; - } - mvwaddch(win, i, offset_front+j+is_selected, dir_content[i].file_name[j]); - } - 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)); } else { wattroff(win, COLOR_PAIR(dir_content[i].color_pair)); } + } - free(hover_bg); + free(bg); } diff --git a/main.c b/main.c index 80eab9f..16b6922 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "threading.h" #include "window.h" #include "defines.h" @@ -169,6 +170,7 @@ void render_pass(){ /*this function exists for things done at startup (initialization, reading config, etc)*/ void init() { + setlocale(LC_ALL, ""); initscr(); /* start ncurses */ noecho(); /* hide keyboard input */ timeout(0); /* blocking timeout of getch() */ diff --git a/window.c b/window.c index 5108658..f144988 100644 --- a/window.c +++ b/window.c @@ -35,7 +35,7 @@ void window_top(WINDOW *win){ if (pthread_mutex_trylock(&mutex_top) == 0) { wattron(win, COLOR_PAIR(COLOR_PATH)); if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/ - mvwprintw(win, 0, 0, "%s", top_buffer); + mvwaddstr(win, 0, 0, top_buffer); } wattroff(win, COLOR_PAIR(COLOR_PATH)); pthread_mutex_unlock(&mutex_top);