From f9fc9e016798a5805b056d439afc744cec4aa4fa Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Fri, 13 Feb 2026 14:34:01 +0200 Subject: [PATCH] Improves and fixes file search --- TODO.md | 3 +- src/file_processor.c | 1 + src/widget/app.c | 2 +- src/widget/file_list.c | 100 +++++++++++++++++++++++++++-------------- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/TODO.md b/TODO.md index b9eafa3..228952c 100644 --- a/TODO.md +++ b/TODO.md @@ -2,11 +2,10 @@ ## Features: - [x] Search files -- [ ] Correct spacing in the search bar +- [x] Correct spacing in the search bar - [ ] Privilege settings for files - [ ] Files removal - [ ] Encryption -- [ ] Input: mark cursor position with "_" - [ ] UTF-8 support ## Fixes: - [ ] Check and fix registration and login diff --git a/src/file_processor.c b/src/file_processor.c index 3cc7728..c438816 100644 --- a/src/file_processor.c +++ b/src/file_processor.c @@ -47,6 +47,7 @@ void file_list(file_args_t *f_args, query_args_t *q_args) { d->needs_update = true; } draw_file_list(fui); + free(query); q_args->state = S_FILE_SELECT; break; } diff --git a/src/widget/app.c b/src/widget/app.c index 13f5f95..9150ed1 100644 --- a/src/widget/app.c +++ b/src/widget/app.c @@ -118,8 +118,8 @@ void app_refresh(app_t *app) { draw_borders(app); wnoutrefresh(app->win); - wnoutrefresh(app->left_win); wnoutrefresh(app->right_win); + wnoutrefresh(app->left_win); if (app->modal.win != NULL) { if (app->modal.is_initiated != 0) { wnoutrefresh(app->modal.win); diff --git a/src/widget/file_list.c b/src/widget/file_list.c index 623baf8..3156e18 100644 --- a/src/widget/file_list.c +++ b/src/widget/file_list.c @@ -26,16 +26,22 @@ void file_list_cb(callback_args_t *args) { } else { sprintf(q_prefix, "file list"); } + /* When search is active */ if (fui->active_search) { if (key == KEY_BACKSPACE || key == KEY_DL) { if (blength(fui->search_key)) { btrunc(fui->search_key, blength(fui->search_key) - 1); draw_file_list(fui); } - } else if (key == '\n') { + } else if (key == '\n' || key == '\33') { fui->active_search = false; fl_clear(fui->start, fui->current); app->query_args->state = S_FILE_LIST; + if (key == '\33') { + fui->search_key->data[0] = '\0'; + fui->search_key->slen = 0; + sprintf(q_prefix, "file list"); + } if (fui->search_key->slen > 0) { sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len); } else { @@ -49,6 +55,7 @@ void file_list_cb(callback_args_t *args) { } return; } + /* Default case */ switch (key) { case KEY_DOWN: if (fui->current_idx < fui->current_count - 1) { @@ -77,6 +84,16 @@ void file_list_cb(callback_args_t *args) { fui->activate_last = true; } break; + case '\33': + fui->active_search = false; + fui->search_key->data[0] = '\0'; + fui->search_key->slen = 0; + sprintf(q_prefix, "file list"); + fl_clear(fui->start, fui->current); + app->query_args->state = S_FILE_LIST; + sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len); + write(app->query_args->sd, query, q_len); + reset_file_list(fui); case '\n': ui_file_select(app->file_args, app->query_args, fui->current_idx + 1); break; @@ -155,55 +172,70 @@ void draw_file_list(ui_file_list_t *fui) { } while ((el = el->next) != NULL && p_y < sz_y_f); p_x = 1; - - for (; p_y < sz_y_f; p_y++) { - mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); - } - char p_info[64]; int32_t p_len; - p_x = 1; - if (fui->active_search) { - sprintf(p_info, "[search]: %s_%n", fui->search_key->data, &p_len); + if (!fui->active_search && fui->search_key->slen > 0 && *(fui->start) == NULL) { + bstring text = bfromStatic("[No data to show]"); + for (; p_y < (sz_y_f / 2); p_y++) { + mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); + } + p_len = (sz_x - text->slen) / 2; + mvwprintw(parent_win, p_y++, p_x, "%*s%s%*s", p_len, "", text->data, p_len, ""); + for (; p_y < sz_y_f; p_y++) { + mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); + } } else { - sprintf(p_info, "page: %u/%u files: %u left: %u%n", fui->current_page, - fui->pages, fui->current_count, fui->full_count, &p_len); + for (; p_y < sz_y_f; p_y++) { + mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); + } } - uint32_t l_pad = (sz_x - p_len) / 2; + char p_info[64]; + /* Draw file info */ + { + uint32_t p_y = 1; + p_x = 1; + WINDOW *i_win = *fui->info_win; + wclear(i_win); + + /* Write to the right side the information about the file */ + if (active_el != NULL) { + char size_text[64]; + size_to_text(active_el->size, size_text); + + mvwprintw(i_win, p_y++, p_x, "Size: %s", size_text); + mvwprintw(i_win, p_y++, p_x, "Owner: %s", active_el->owner); + mvwprintw(i_win, p_y++, p_x, "Description: "); + print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0); + } + } + + /* Search bar */ + p_x = 1; wattrset(parent_win, A_BOLD); if (fui->active_search) { - mvwprintw(parent_win, p_y, p_x, "%s%*s", p_info, sz_x - p_len, ""); + sprintf(p_info, "[search]: %s%n", fui->search_key->data, &p_len); + mvwprintw(parent_win, p_y, p_x, "%-*s", sz_x, p_info); + curs_set(true); + wmove(parent_win, p_y, p_x + p_len); } else { - /* TODO: Correct spacing when lack of space (with search keyword) */ + uint32_t l_pad = 0; + sprintf(p_info, "P: %u/%u F: %u L: %u%n", fui->current_page, fui->pages, + fui->current_count, fui->full_count, &p_len); + if (fui->search_key->slen > 0) { wattrset(parent_win, A_REVERSE); mvwprintw(parent_win, p_y, p_x, "[%s]", fui->search_key->data); wattroff(parent_win, A_REVERSE); - mvwprintw(parent_win, p_y, p_x + fui->search_key->slen + 2, " %s", - p_info); + l_pad = fui->search_key->slen + 2; + mvwprintw(parent_win, p_y, p_x + l_pad, " %.*s", sz_x - l_pad, p_info); } else { + sprintf(p_info, "page: %u/%u files: %u left: %u%n", fui->current_page, + fui->pages, fui->current_count, fui->full_count, &p_len); + l_pad = (sz_x - p_len) / 2; mvwprintw(parent_win, p_y, p_x, "%*s%s%*s", l_pad, "", p_info, l_pad, ""); } } wattroff(parent_win, A_BOLD); - - /* draw file info */ - p_y = 1; - p_x = 1; - WINDOW *i_win = *fui->info_win; - wclear(i_win); - - /* Write to the right side the information about the file */ - if (active_el == NULL) - return; - - char size_text[64]; - size_to_text(active_el->size, size_text); - - mvwprintw(i_win, p_y++, p_x, "Size: %s", size_text); - mvwprintw(i_win, p_y++, p_x, "Owner: %s", active_el->owner); - mvwprintw(i_win, p_y++, p_x, "Description: "); - print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0); } \ No newline at end of file