fixes fs file list page up/down

This commit is contained in:
2026-04-27 17:12:05 +03:00
parent 38fcb5dda8
commit 489860210a
+30 -30
View File
@@ -3,6 +3,7 @@
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <math.h>
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@@ -15,12 +16,12 @@
#include <unistd.h> #include <unistd.h>
#include <utils.h> #include <utils.h>
#include "../fs.h"
#include "../server.h" #include "../server.h"
#include "alert.h" #include "alert.h"
#include "fs_file_list.h" #include "fs_file_list.h"
#include "main_window.h" #include "main_window.h"
#include "widget_core.h" #include "widget_core.h"
#include "../fs.h"
// void w_fl_reset(w_lfl_ui_t *fl_ui); // void w_fl_reset(w_lfl_ui_t *fl_ui);
@@ -157,10 +158,14 @@ static int get_files_from_fs(w_lfl_ui_t *fui, char *path) {
fui->page_start = d_start; fui->page_start = d_start;
fui->current = d_start; fui->current = d_start;
fui->cur_page = 1; fui->cur_page = 1;
fui->pages_num = fui->files_num / fui->max_lines;
if (fui->files_num % fui->max_lines) {
fui->pages_num++;
}
return 0; return 0;
} }
void open_selected_item(w_lfl_ui_t *fui, w_cbrp_data *resp_data) { static void open_selected_item(w_lfl_ui_t *fui, w_cbrp_data *resp_data) {
char *bash_case = NULL; char *bash_case = NULL;
char *selection = NULL; char *selection = NULL;
size_t dp_current; size_t dp_current;
@@ -207,11 +212,11 @@ void open_selected_item(w_lfl_ui_t *fui, w_cbrp_data *resp_data) {
} }
/* next page */ /* next page */
void page_next(w_lfl_ui_t *fui) { static void page_next(w_lfl_ui_t *fui) {
int count = fui->max_lines; int count = fui->max_lines;
w_lfl_item_t *n_page_item = fui->page_start; w_lfl_item_t *n_page_item = fui->page_start;
if (fui->cur_page < fui->pages_num) { if (fui->cur_page < fui->pages_num) {
for (; count; count--) { for (; count >= 0; count--) {
n_page_item = n_page_item->next; n_page_item = n_page_item->next;
} }
fui->cur_page++; fui->cur_page++;
@@ -219,39 +224,40 @@ void page_next(w_lfl_ui_t *fui) {
fui->current_idx = 0; fui->current_idx = 0;
fui->current = n_page_item; fui->current = n_page_item;
} }
w_lfl_draw(fui);
} }
/* previous page */ /* previous page */
void page_previous(w_lfl_ui_t *fui) { static void page_previous(w_lfl_ui_t *fui) {
int count = fui->max_lines; int count = fui->max_lines;
w_lfl_item_t *n_page_item = fui->page_start; w_lfl_item_t *n_page_item = fui->page_start;
if (fui->cur_page > 1) { if (fui->cur_page > 1) {
for (; count; count--) { for (; count >= 0; count--) {
n_page_item = n_page_item->prev; n_page_item = n_page_item->prev;
} }
fui->cur_page--; fui->cur_page--;
fui->current = fui->page_start->prev; /* the last element of the previous page */
fui->page_start = n_page_item; fui->page_start = n_page_item;
fui->current_idx = 0; fui->current_idx = fui->max_lines;
fui->current = n_page_item;
} }
w_lfl_draw(fui);
} }
static void w_lfl_cb(w_cb_args_t *args) { static void w_lfl_cb(w_cb_args_t *args) {
w_lfl_ui_t *fui = args->element; w_lfl_ui_t *fui = args->element;
int32_t key = *((int32_t *)args->data); int32_t key = *((int32_t *)args->data);
bool is_cur_p = 0; /* current page */
switch (key) { switch (key) {
case KEY_DOWN: case KEY_DOWN:
is_cur_p = (fui->current_idx % fui->max_lines || fui->current_idx == 0);
if (fui->current->next != NULL) { if (fui->current->next != NULL) {
if (fui->current_idx < fui->max_lines - 1) { if (is_cur_p) {
fui->current = fui->current->next; if (fui->current_idx < fui->max_lines) {
fui->current_idx++; fui->current = fui->current->next;
} else { fui->current_idx++;
}
} else { /* is_cur_p */
page_next(fui); page_next(fui);
break;
} }
} } else break; /* fui->current->next != NULL */
w_lfl_draw(fui); w_lfl_draw(fui);
break; break;
case KEY_UP: case KEY_UP:
@@ -261,9 +267,8 @@ static void w_lfl_cb(w_cb_args_t *args) {
fui->current_idx--; fui->current_idx--;
} else { } else {
page_previous(fui); page_previous(fui);
break;
} }
} } else break;
w_lfl_draw(fui); w_lfl_draw(fui);
break; break;
case KEY_NPAGE: case KEY_NPAGE:
@@ -350,13 +355,9 @@ w_lfl_ui_t *w_lfl_init_win(app_t *app) {
fui->current_idx = 0; fui->current_idx = 0;
fui->win_info = NULL; fui->win_info = NULL;
fui->d_path = NULL; fui->d_path = NULL;
char *path = get_current_dir_name();
get_files_from_fs(fui, path);
fui->current = fui->start;
fui->w.callback = w_lfl_cb; fui->w.callback = w_lfl_cb;
fui->w.sz.x = getmaxx(app->win); fui->w.sz.x = getmaxx(app->win);
fui->w.sz.y = getmaxy(app->win); fui->w.sz.y = getmaxy(app->win);
fui->max_lines = 0; /* detects on the first draw */
fui->cur_page = 1; fui->cur_page = 1;
fui->w.parent_win = &(app->win); fui->w.parent_win = &(app->win);
/* * INIT UI * */ /* * INIT UI * */
@@ -367,10 +368,16 @@ w_lfl_ui_t *w_lfl_init_win(app_t *app) {
/* create the list window */ /* create the list window */
fui->win_list = newwin(app->coordinates.max_y - 4, left_w_x, 2, 1); fui->win_list = newwin(app->coordinates.max_y - 4, left_w_x, 2, 1);
fui->max_lines = fui->max_lines = getmaxy(fui->win_list) - 3;
/* create the action window */ /* create the action window */
fui->win_info = fui->win_info =
newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1); newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1);
/* get files */
char *path = get_current_dir_name();
get_files_from_fs(fui, path);
fui->current = fui->start;
return fui; return fui;
} }
@@ -383,9 +390,7 @@ void w_lfl_draw(w_lfl_ui_t *fui) {
getmaxyx(win, sz_y, sz_x); getmaxyx(win, sz_y, sz_x);
int32_t sz_y_f = sz_y - 1; // actual size (without box) int32_t sz_y_f = sz_y - 1; // actual size (without box)
int32_t sz_x_f = sz_x - 1; // actual size (without box) int32_t sz_x_f = sz_x - 1; // actual size (without box)
fui->max_lines = sz_y_f - 1;
fui->pages_num = fui->files_num / fui->max_lines +
(fui->files_num % fui->max_lines > 0 ? 1 : 0);
p_y = 1; p_y = 1;
p_x = 1; p_x = 1;
w_lfl_item_t *el = fui->page_start; w_lfl_item_t *el = fui->page_start;
@@ -394,11 +399,6 @@ void w_lfl_draw(w_lfl_ui_t *fui) {
box(fui->win_list, 0, 0); box(fui->win_list, 0, 0);
box(fui->win_info, 0, 0); box(fui->win_info, 0, 0);
if (fui->start != NULL) {
el = fui->start;
active_el = el;
}
do { do {
if (p_y < 1) { if (p_y < 1) {
p_y++; p_y++;