UI: separate windows objects
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
add_library(windows STATIC)
|
||||
|
||||
file(GLOB M_SOURCES "*.c")
|
||||
|
||||
target_link_directories(windows
|
||||
PRIVATE
|
||||
"../widget/"
|
||||
"../widget_core/"
|
||||
"../lib/"
|
||||
"../utils/"
|
||||
)
|
||||
|
||||
target_link_libraries(windows
|
||||
PRIVATE
|
||||
widget
|
||||
widget_core
|
||||
bstring
|
||||
utils
|
||||
)
|
||||
|
||||
target_sources(windows
|
||||
PRIVATE
|
||||
${M_SOURCES}
|
||||
PUBLIC
|
||||
FILE_SET HEADERS
|
||||
FILES
|
||||
windows.h
|
||||
)
|
||||
@@ -0,0 +1,356 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "app.h"
|
||||
#include <bstrlib.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include "main_window.h"
|
||||
#include "file_list.h"
|
||||
#include "../file_processor.h"
|
||||
#include "../fs.h"
|
||||
#include "../server.h"
|
||||
|
||||
void w_fl_reset(w_ui_file_list_t *fl_ui);
|
||||
|
||||
static void w_fl_cb(w_cb_args_t *args) {
|
||||
w_app_t *app = args->app;
|
||||
w_ui_file_list_t *fui = app->query_args->main_ui->ui;
|
||||
int32_t key = *((int32_t *)args->data);
|
||||
char q_prefix[128];
|
||||
if (fui->search_key->slen > 0) {
|
||||
sprintf(q_prefix, "file search name %s", fui->search_key->data);
|
||||
} 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);
|
||||
w_fl_draw(fui);
|
||||
}
|
||||
} else if (key == '\n' || key == '\33') {
|
||||
fui->active_search = false;
|
||||
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");
|
||||
}
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, 1);
|
||||
w_fl_reset(fui);
|
||||
} else {
|
||||
bconchar(fui->search_key, key);
|
||||
w_fl_draw(fui);
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* Default case */
|
||||
switch (key) {
|
||||
case KEY_DOWN:
|
||||
if (fui->current_idx < fui->current_count - 1) {
|
||||
fui->current_idx++;
|
||||
} else if (fui->current_page < fui->pages) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page + 1);
|
||||
w_fl_reset(fui);
|
||||
break;
|
||||
}
|
||||
w_fl_draw(fui);
|
||||
break;
|
||||
case KEY_UP:
|
||||
if (fui->current_idx > 0) {
|
||||
fui->current_idx--;
|
||||
} else if (fui->current_page > 1) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page - 1);
|
||||
w_fl_reset(fui);
|
||||
fui->activate_last = true;
|
||||
break;
|
||||
}
|
||||
w_fl_draw(fui);
|
||||
break;
|
||||
case KEY_NPAGE:
|
||||
if (fui->current_page < fui->pages) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page + 1);
|
||||
w_fl_reset(fui);
|
||||
break;
|
||||
}
|
||||
w_fl_draw(fui);
|
||||
break;
|
||||
case KEY_PPAGE:
|
||||
if (fui->current_page > 1) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page - 1);
|
||||
w_fl_reset(fui);
|
||||
fui->activate_last = true;
|
||||
break;
|
||||
}
|
||||
w_fl_draw(fui);
|
||||
break;
|
||||
case '\33':
|
||||
fui->active_search = false;
|
||||
fui->search_key->data[0] = '\0';
|
||||
fui->search_key->slen = 0;
|
||||
sprintf(q_prefix, "file list");
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix, fui->max_lines,
|
||||
1);
|
||||
w_fl_reset(fui);
|
||||
case '\n':
|
||||
ui_file_select(app->file_args, app->query_args, fui->current_idx + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void w_fl_cb_refresh(void *data) {
|
||||
w_app_t *app = (w_app_t *)data;
|
||||
if (app->main_ui.type != mw_fl_server) {
|
||||
return;
|
||||
}
|
||||
w_ui_file_list_t *fui = app->main_ui.ui;
|
||||
wnoutrefresh(fui->win_list);
|
||||
wnoutrefresh(fui->win_info);
|
||||
}
|
||||
|
||||
static struct action_key action_keys[] = {
|
||||
{.key = "F1", .title = "Help", .code = KEY_F(1)},
|
||||
{.key = "U", .title = "Upload", .code = 'u'},
|
||||
{.key = "S", .title = "Search", .code = 's'},
|
||||
{.key = "F9", .title = "Quit", .code = KEY_F(9)},
|
||||
};
|
||||
|
||||
static int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) {
|
||||
int32_t c;
|
||||
w_ui_file_list_t *fui = (w_ui_file_list_t *)app->query_args->main_ui->ui;
|
||||
c = wgetch(app->win);
|
||||
switch (c) {
|
||||
case KEY_F(9):
|
||||
w_app_destroy(app, 0);
|
||||
return OK;
|
||||
case 'U':
|
||||
case 'u':
|
||||
if (!app->modal.is_initiated && !fui->active_search) {
|
||||
/* app->query_args->state = S_UPLOAD_FILE_SELECT; */
|
||||
main_window_set(app, mw_fl_local);
|
||||
main_window_draw(app);
|
||||
d_args->element = app->main_ui.ui;
|
||||
break;
|
||||
}
|
||||
case 's':
|
||||
case 'S':
|
||||
if (!app->modal.is_initiated && !fui->active_search) {
|
||||
fui->active_search = true;
|
||||
w_fl_draw(fui);
|
||||
return OK;
|
||||
}
|
||||
default:
|
||||
d_args->data = (void *)&c;
|
||||
app->active_callback(d_args);
|
||||
break;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
void *w_fl_init(w_app_t *app) {
|
||||
w_ui_file_list_t *fui = malloc(sizeof(w_ui_file_list_t));
|
||||
app->main_ui.ui = fui;
|
||||
app->main_ui.type = mw_fl_server;
|
||||
app->main_ui.cb_refresh = w_fl_cb_refresh;
|
||||
app->main_ui.b_keys = action_keys;
|
||||
app->main_ui.b_keys_len = 4;
|
||||
app->main_ui.cb_b_press = (w_cb_press_t) process_user_input;
|
||||
app->active_callback = w_fl_cb;
|
||||
w_init(&(fui->w), NULL, &(app->win), "");
|
||||
WINDOW *win_parent = app->win;
|
||||
fui->current_idx = 0;
|
||||
fui->current_page = 1;
|
||||
fui->pages = 1;
|
||||
fui->current_count = 0;
|
||||
fui->full_count = 0;
|
||||
fui->activate_last = false;
|
||||
fui->active_search = false;
|
||||
fui->search_key = bfromcstrrangealloc(12, 64, "");
|
||||
fui->start = NULL;
|
||||
fui->current = NULL;
|
||||
fui->w.sz.x = getmaxx(app->win);
|
||||
fui->w.sz.y = getmaxy(app->win);
|
||||
|
||||
/* * INIT UI * */
|
||||
|
||||
/* define the width for each sub window */
|
||||
int64_t left_w_x = app->coordinates.max_x / 10 * 5;
|
||||
int64_t right_w_x = app->coordinates.max_x - left_w_x - 2;
|
||||
|
||||
/* create the list window */
|
||||
fui->win_list = newwin(app->coordinates.max_y - 4, left_w_x, 2, 1);
|
||||
|
||||
/* create the action window */
|
||||
fui->win_info =
|
||||
newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1);
|
||||
fui->max_lines = getmaxy(fui->win_list) - 3; /* 2+1 (bottom info line) */
|
||||
return fui;
|
||||
}
|
||||
|
||||
void w_fl_destroy(w_ui_file_list_t **fui) {
|
||||
fl_clear((*fui)->start, (*fui)->current);
|
||||
free(*fui);
|
||||
*fui = NULL;
|
||||
}
|
||||
|
||||
void w_fl_reset(w_ui_file_list_t *fl_ui) {
|
||||
fl_clear(fl_ui->start, fl_ui->current);
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_page = 0;
|
||||
fl_ui->pages = 0;
|
||||
fl_ui->current_count = 0;
|
||||
fl_ui->full_count = 0;
|
||||
fl_ui->activate_last = false;
|
||||
fl_ui->active_search = false;
|
||||
}
|
||||
|
||||
void w_fl_draw(w_ui_file_list_t *fui) {
|
||||
int32_t sz_y, sz_x;
|
||||
int32_t p_y, p_x;
|
||||
WINDOW *win = fui->win_list;
|
||||
getmaxyx(win, sz_y, sz_x);
|
||||
|
||||
int32_t sz_y_f = sz_y - 2; /* TODO: можно заменить на поле max_lines */
|
||||
sz_x -= 1; /* do not count the borders */
|
||||
p_y = 1;
|
||||
p_x = 1;
|
||||
fl_item_t *el = NULL;
|
||||
fl_item_t *active_el = NULL;
|
||||
int32_t cur_el_idx = 0;
|
||||
curs_set(false);
|
||||
|
||||
box(fui->win_list, 0, 0);
|
||||
|
||||
if (fui->start != NULL) {
|
||||
el = *(fui->start);
|
||||
active_el = el;
|
||||
}
|
||||
|
||||
if (fui->activate_last) {
|
||||
fui->current_idx = fui->current_count - 1;
|
||||
fui->activate_last = false;
|
||||
}
|
||||
|
||||
if (fui->current_idx + 1 >= sz_y_f) {
|
||||
p_y -= fui->current_idx - sz_y_f + 2;
|
||||
}
|
||||
|
||||
do {
|
||||
if (el == NULL)
|
||||
break;
|
||||
if (p_y < 1) {
|
||||
p_y++;
|
||||
cur_el_idx++;
|
||||
continue;
|
||||
}
|
||||
if (cur_el_idx == fui->current_idx) {
|
||||
wattrset(win, A_BOLD | A_REVERSE);
|
||||
active_el = el;
|
||||
}
|
||||
p_x = 1;
|
||||
p_x += u_utf8_curs_printw(win, p_y, p_x, el->name, sz_x - 1);
|
||||
int pad = sz_x - p_x;
|
||||
if (pad >= 0) {
|
||||
mvwprintw(win, p_y, p_x, "%*s", pad, "");
|
||||
}
|
||||
if (cur_el_idx == fui->current_idx) {
|
||||
wattroff(win, A_BOLD | A_REVERSE);
|
||||
}
|
||||
p_y++;
|
||||
cur_el_idx++;
|
||||
} while ((el = el->next) != NULL && p_y < sz_y_f);
|
||||
|
||||
p_x = 1;
|
||||
int32_t 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(win, p_y, p_x, "%*s", sz_x - 1, "");
|
||||
}
|
||||
p_len = (sz_x - text->slen) / 2;
|
||||
mvwprintw(win, p_y++, p_x, "%*s%s%*s", p_len, "", text->data, p_len, "");
|
||||
for (; p_y < sz_y_f; p_y++) {
|
||||
mvwprintw(win, p_y, p_x, "%*s", sz_x - 1, "");
|
||||
}
|
||||
} else {
|
||||
for (; p_y < sz_y_f; p_y++) {
|
||||
mvwprintw(win, p_y, p_x, "%*s", sz_x - 1, "");
|
||||
}
|
||||
}
|
||||
|
||||
char p_info[64];
|
||||
|
||||
/* Draw file info [right side] */
|
||||
{
|
||||
uint32_t p_y = 1;
|
||||
p_x = 1;
|
||||
WINDOW *i_win = fui->win_info;
|
||||
wclear(i_win);
|
||||
box(fui->win_info, 0, 0);
|
||||
|
||||
/* 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);
|
||||
if (active_el->description != NULL) {
|
||||
mvwprintw(i_win, p_y++, p_x, "Description: ");
|
||||
w_print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x,
|
||||
0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Search bar */
|
||||
p_x = 1;
|
||||
wattrset(win, A_BOLD);
|
||||
if (fui->active_search) {
|
||||
sprintf(p_info, "[search]: %s%n", fui->search_key->data, &p_len);
|
||||
mvwprintw(win, p_y, p_x, "%-*s", sz_x, p_info);
|
||||
curs_set(true);
|
||||
wmove(win, p_y, p_x + p_len);
|
||||
} else {
|
||||
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(win, A_REVERSE);
|
||||
mvwprintw(win, p_y, p_x, "[%s]", fui->search_key->data);
|
||||
wattroff(win, A_REVERSE);
|
||||
l_pad = fui->search_key->slen + 2;
|
||||
mvwprintw(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;
|
||||
if (!(l_pad % 2)) {
|
||||
l_pad--;
|
||||
}
|
||||
mvwprintw(win, p_y, p_x, "%*s%s%*s", l_pad, "", p_info, l_pad, "");
|
||||
}
|
||||
}
|
||||
wattroff(win, A_BOLD);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#ifndef W_FILE_LIST_H
|
||||
#define W_FILE_LIST_H
|
||||
|
||||
#include "app.h"
|
||||
#include <bstrlib.h>
|
||||
#include <stdint.h>
|
||||
#include <widget_core.h>
|
||||
|
||||
typedef struct {
|
||||
w_t w;
|
||||
fl_item_t **current;
|
||||
fl_item_t **start;
|
||||
int32_t current_idx;
|
||||
uint32_t current_count;
|
||||
uint32_t full_count;
|
||||
uint32_t pages;
|
||||
uint32_t current_page;
|
||||
uint32_t max_lines;
|
||||
WINDOW *win_info;
|
||||
WINDOW *win_list;
|
||||
bstring search_key;
|
||||
bool activate_last;
|
||||
bool active_search;
|
||||
} w_ui_file_list_t;
|
||||
|
||||
void w_fl_draw(w_ui_file_list_t *fl_ui);
|
||||
void *w_fl_init(w_app_t *app);
|
||||
void w_fl_reset(w_ui_file_list_t *fl_ui);
|
||||
void w_fl_destroy(w_ui_file_list_t **fui);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,412 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include <dirent.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <uchar.h>
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
|
||||
#include "main_window.h"
|
||||
#include "alert.h"
|
||||
#include "fs_file_list.h"
|
||||
#include "widget_core.h"
|
||||
#include "../server.h"
|
||||
|
||||
// void w_fl_reset(w_lfl_ui_t *fl_ui);
|
||||
|
||||
struct fl_args {
|
||||
char *name;
|
||||
char *path;
|
||||
u_char d_type;
|
||||
};
|
||||
|
||||
static void fl_add(w_lfl_item_t **f_start, w_lfl_item_t **f_current,
|
||||
struct fl_args *fargs) {
|
||||
w_lfl_item_t *c_item = malloc(sizeof(w_lfl_item_t));
|
||||
c_item->name = malloc(strlen(fargs->name) + 1);
|
||||
strcpy(c_item->name, fargs->name);
|
||||
c_item->next = NULL;
|
||||
c_item->d_type = fargs->d_type;
|
||||
if (fargs->path != NULL) {
|
||||
c_item->path = malloc(strlen(fargs->path) + 1);
|
||||
strcpy(c_item->path, fargs->path);
|
||||
}
|
||||
if (*f_start == NULL) {
|
||||
c_item->prev = NULL;
|
||||
*f_start = c_item;
|
||||
*f_current = c_item;
|
||||
} else {
|
||||
c_item->prev = *f_current;
|
||||
(*f_current)->next = c_item;
|
||||
*f_current = c_item;
|
||||
}
|
||||
}
|
||||
|
||||
static void fl_clear(w_lfl_item_t **start, w_lfl_item_t **arg_current) {
|
||||
if (*start == NULL)
|
||||
return;
|
||||
w_lfl_item_t *next, *current;
|
||||
current = *start;
|
||||
|
||||
do {
|
||||
next = current->next;
|
||||
free(current->name);
|
||||
free(current);
|
||||
} while ((current = next) != NULL);
|
||||
*start = NULL;
|
||||
*arg_current = NULL;
|
||||
}
|
||||
|
||||
static int get_files_from_fs(w_lfl_ui_t *fui, char *path) {
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
char name[257];
|
||||
char l_path[INBUFSIZE];
|
||||
struct fl_args f_args;
|
||||
ssize_t r = 0;
|
||||
fui->files_num = 0;
|
||||
|
||||
w_lfl_item_t *d_start = NULL, *d_current = NULL; /* directories */
|
||||
w_lfl_item_t *f_start = NULL, *f_current = NULL; /* files */
|
||||
|
||||
dir = opendir(path);
|
||||
if (!dir)
|
||||
return 1;
|
||||
if (fui->d_path != NULL) {
|
||||
free(fui->d_path);
|
||||
fl_clear(&(fui->start), &(fui->current));
|
||||
}
|
||||
fui->d_path = malloc(strlen(path) + 1);
|
||||
strcpy(fui->d_path, path);
|
||||
while ((dent = readdir(dir)) != NULL) {
|
||||
f_args.path = NULL;
|
||||
if ((f_args.d_type = dent->d_type) == DT_DIR) {
|
||||
if (!strcmp(dent->d_name, "..")) {
|
||||
w_lfl_item_t *t_st = NULL, *t_cur = NULL;
|
||||
sprintf(name, "/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
fl_add(&t_st, &t_cur, &f_args);
|
||||
fui->files_num++;
|
||||
/* Always put .. at the beginning */
|
||||
t_st->next = d_start;
|
||||
d_start = t_st;
|
||||
if (d_current == NULL)
|
||||
d_current = t_cur;
|
||||
continue;
|
||||
}
|
||||
/* Hide directories with dot-beginnings */
|
||||
if (!strncmp(dent->d_name, ".", 1))
|
||||
continue;
|
||||
sprintf(name, "/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
fl_add(&d_start, &d_current, &f_args);
|
||||
fui->files_num++;
|
||||
} else if (f_args.d_type == DT_LNK) {
|
||||
sprintf(name, "%s/%s", fui->d_path, dent->d_name);
|
||||
r = readlink(name, l_path, INBUFSIZE);
|
||||
sprintf(name, "->/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
f_args.path = l_path;
|
||||
fl_add(&d_start, &d_current, &f_args);
|
||||
fui->files_num++;
|
||||
} else {
|
||||
/* Hide files with dot-beginnings */
|
||||
if (!strncmp(dent->d_name, ".", 1))
|
||||
continue;
|
||||
f_args.name = dent->d_name;
|
||||
sprintf(name, "%s", dent->d_name);
|
||||
fl_add(&f_start, &f_current, &f_args);
|
||||
fui->files_num++;
|
||||
}
|
||||
}
|
||||
if (d_current != NULL)
|
||||
d_current->next = f_start;
|
||||
if (f_start != NULL)
|
||||
f_start->prev = d_current;
|
||||
|
||||
fui->start = d_start;
|
||||
fui->page_start = d_start;
|
||||
fui->current = d_start;
|
||||
fui->cur_page = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void open_selected_item(w_lfl_ui_t *fui, w_cbrp_data *resp_data) {
|
||||
char *bash_case = NULL;
|
||||
char *selection = NULL;
|
||||
size_t dp_current;
|
||||
char n_path[INBUFSIZE];
|
||||
if (fui->current->d_type == DT_DIR) {
|
||||
if (!strcmp("/..", fui->current->name)) {
|
||||
strcpy(n_path, fui->d_path);
|
||||
bash_case = strrchr(n_path, '/');
|
||||
if (bash_case != NULL) {
|
||||
*bash_case = '\0';
|
||||
if (get_files_from_fs(fui, n_path)) {
|
||||
w_alert("Can't open the folder!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dp_current = strlen(fui->d_path);
|
||||
selection = fui->current->name + 1;
|
||||
sprintf(n_path, "%s/%s", fui->d_path, selection);
|
||||
if (get_files_from_fs(fui, n_path)) {
|
||||
w_alert("Can't open the folder!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
fui->current = fui->start;
|
||||
fui->current_idx = 0;
|
||||
resp_data->code = cbrc_none;
|
||||
w_lfl_draw(fui);
|
||||
} else if (fui->current->d_type == DT_REG) {
|
||||
resp_data->code = cbrp_val;
|
||||
resp_data->val.type = val_num;
|
||||
resp_data->val.val.num = 1;
|
||||
} else if (fui->current->d_type == DT_LNK) {
|
||||
strcpy(n_path, fui->current->path);
|
||||
if (get_files_from_fs(fui, n_path)) {
|
||||
w_alert("Can't open the link!");
|
||||
return;
|
||||
}
|
||||
fui->current = fui->start;
|
||||
fui->current_idx = 0;
|
||||
resp_data->code = cbrc_none;
|
||||
w_lfl_draw(fui);
|
||||
}
|
||||
}
|
||||
|
||||
/* next page */
|
||||
void page_next(w_lfl_ui_t *fui) {
|
||||
int count = fui->max_lines;
|
||||
w_lfl_item_t *n_page_item = fui->page_start;
|
||||
if (fui->cur_page < fui->pages_num) {
|
||||
for (; count; count--) {
|
||||
n_page_item = n_page_item->next;
|
||||
}
|
||||
fui->cur_page++;
|
||||
fui->page_start = n_page_item;
|
||||
fui->current_idx = 0;
|
||||
fui->current = n_page_item;
|
||||
}
|
||||
w_lfl_draw(fui);
|
||||
}
|
||||
|
||||
/* previous page */
|
||||
void page_previous(w_lfl_ui_t *fui) {
|
||||
int count = fui->max_lines;
|
||||
w_lfl_item_t *n_page_item = fui->page_start;
|
||||
if (fui->cur_page > 1) {
|
||||
for (; count; count--) {
|
||||
n_page_item = n_page_item->prev;
|
||||
}
|
||||
fui->cur_page--;
|
||||
fui->page_start = n_page_item;
|
||||
fui->current_idx = 0;
|
||||
fui->current = n_page_item;
|
||||
}
|
||||
w_lfl_draw(fui);
|
||||
}
|
||||
|
||||
static void w_lfl_cb(w_cb_args_t *args) {
|
||||
w_lfl_ui_t *fui = args->element;
|
||||
int32_t key = *((int32_t *)args->data);
|
||||
switch (key) {
|
||||
case KEY_DOWN:
|
||||
if (fui->current->next != NULL) {
|
||||
if (fui->current_idx < fui->max_lines - 1) {
|
||||
fui->current = fui->current->next;
|
||||
fui->current_idx++;
|
||||
} else {
|
||||
page_next(fui);
|
||||
break;
|
||||
}
|
||||
}
|
||||
w_lfl_draw(fui);
|
||||
break;
|
||||
case KEY_UP:
|
||||
if (fui->current->prev != NULL) {
|
||||
if (fui->current_idx > 0) {
|
||||
fui->current = fui->current->prev;
|
||||
fui->current_idx--;
|
||||
} else {
|
||||
page_previous(fui);
|
||||
break;
|
||||
}
|
||||
}
|
||||
w_lfl_draw(fui);
|
||||
break;
|
||||
case KEY_NPAGE:
|
||||
page_next(fui);
|
||||
break;
|
||||
case KEY_PPAGE:
|
||||
page_previous(fui);
|
||||
break;
|
||||
case '\n':
|
||||
open_selected_item(fui, &(args->resp_data));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
w_lfl_ui_t *w_lfl_init(WINDOW **win, w_t *w_parent) {
|
||||
w_lfl_ui_t *fl_ui = malloc(sizeof(w_lfl_ui_t));
|
||||
WINDOW *win_par = *(w_parent->w_parent->parent_win);
|
||||
w_init(&(fl_ui->w), w_parent, win, "");
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->win_info = NULL;
|
||||
fl_ui->d_path = NULL;
|
||||
char *path = get_current_dir_name();
|
||||
get_files_from_fs(fl_ui, path);
|
||||
fl_ui->current = fl_ui->start;
|
||||
fl_ui->w.callback = w_lfl_cb;
|
||||
fl_ui->w.sz.x = getmaxx(win_par) / 10 * 8;
|
||||
fl_ui->w.sz.y = getmaxy(win_par) / 10 * 8;
|
||||
fl_ui->max_lines = 0; /* detects on the first draw */
|
||||
fl_ui->cur_page = 1;
|
||||
return fl_ui;
|
||||
}
|
||||
|
||||
static void w_lfl_cb_refresh(void *data) {
|
||||
w_app_t *app = (w_app_t *)data;
|
||||
if (app->main_ui.type != mw_fl_local) {
|
||||
return;
|
||||
}
|
||||
w_lfl_ui_t *fui = app->main_ui.ui;
|
||||
wnoutrefresh(fui->win_list);
|
||||
wnoutrefresh(fui->win_info);
|
||||
}
|
||||
|
||||
static struct action_key action_keys[] = {
|
||||
{.key = "Esc", .title = "Back", .code = '\33'},
|
||||
{.key = "F9", .title = "Quit", .code = KEY_F(9)},
|
||||
};
|
||||
|
||||
static int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) {
|
||||
int32_t c;
|
||||
w_lfl_ui_t *fui = (w_lfl_ui_t *)app->query_args->main_ui->ui;
|
||||
c = wgetch(app->win);
|
||||
switch (c) {
|
||||
case KEY_F(9):
|
||||
w_app_destroy(app, 0);
|
||||
return OK;
|
||||
case 'D':
|
||||
case 'd':
|
||||
case '\33': /* ESC key */
|
||||
if (!app->modal.is_initiated) {
|
||||
main_window_set(app, mw_fl_server);
|
||||
main_window_draw(app);
|
||||
server_send_string(app->query_args, "file list %u %u\n", fui->max_lines,
|
||||
1);
|
||||
d_args->element = app->main_ui.ui;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
d_args->data = (void *)&c;
|
||||
app->active_callback(d_args);
|
||||
break;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
w_lfl_ui_t *w_lfl_init_win(w_app_t *app) {
|
||||
w_lfl_ui_t *fui = malloc(sizeof(w_lfl_ui_t));
|
||||
app->main_ui.ui = fui;
|
||||
app->main_ui.type = mw_fl_local;
|
||||
app->main_ui.cb_refresh = w_lfl_cb_refresh;
|
||||
app->main_ui.b_keys = action_keys;
|
||||
app->main_ui.b_keys_len = 2;
|
||||
app->main_ui.cb_b_press = (w_cb_press_t)process_user_input;
|
||||
app->active_callback = w_lfl_cb;
|
||||
w_init(&(fui->w), NULL, &(app->win), "");
|
||||
fui->current_idx = 0;
|
||||
fui->win_info = 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.sz.x = getmaxx(app->win);
|
||||
fui->w.sz.y = getmaxy(app->win);
|
||||
fui->max_lines = 0; /* detects on the first draw */
|
||||
fui->cur_page = 1;
|
||||
fui->w.parent_win = &(app->win);
|
||||
/* * INIT UI * */
|
||||
|
||||
/* define the width for each sub window */
|
||||
int32_t left_w_x = app->coordinates.max_x / 10 * 4;
|
||||
int32_t right_w_x = app->coordinates.max_x - left_w_x - 2;
|
||||
|
||||
/* create the list window */
|
||||
fui->win_list = newwin(app->coordinates.max_y - 4, left_w_x, 2, 1);
|
||||
|
||||
/* create the action window */
|
||||
fui->win_info =
|
||||
newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1);
|
||||
return fui;
|
||||
}
|
||||
|
||||
void w_lfl_reset(w_lfl_ui_t *fl_ui) { fl_ui->current_idx = 0; }
|
||||
|
||||
void w_lfl_draw(w_lfl_ui_t *fui) {
|
||||
int32_t sz_y, sz_x;
|
||||
int32_t p_y, p_x;
|
||||
WINDOW *win = fui->win_list;
|
||||
getmaxyx(win, sz_y, sz_x);
|
||||
int32_t sz_y_f = sz_y - 2; // actual size (without box)
|
||||
int32_t sz_x_f = sz_x - 2; // actual size (without box)
|
||||
fui->max_lines = sz_y_f;
|
||||
fui->pages_num = fui->files_num / fui->max_lines +
|
||||
(fui->files_num % fui->max_lines > 0 ? 1 : 0);
|
||||
p_y = 1;
|
||||
p_x = 1;
|
||||
w_lfl_item_t *el = fui->page_start;
|
||||
|
||||
if (fui->current_idx + 1 >= sz_y_f) {
|
||||
p_y -= fui->current_idx - sz_y_f + 2;
|
||||
}
|
||||
|
||||
box(fui->win_list, 0, 0);
|
||||
box(fui->win_info, 0, 0);
|
||||
|
||||
do {
|
||||
if (p_y < 1) {
|
||||
p_y++;
|
||||
continue;
|
||||
}
|
||||
if (el == fui->current) {
|
||||
wattrset(win, A_BOLD | A_REVERSE);
|
||||
}
|
||||
p_x = 1;
|
||||
p_x += u_utf8_curs_printw(win, p_y, p_x, el->name, sz_x);
|
||||
mvwprintw(win, p_y, p_x, "%*s", sz_x_f - p_x, "");
|
||||
if (el == fui->current) {
|
||||
wattroff(win, A_BOLD | A_REVERSE);
|
||||
}
|
||||
p_y++;
|
||||
} while ((el = el->next) != NULL && p_y < sz_y_f);
|
||||
|
||||
p_x = 1;
|
||||
|
||||
for (; p_y < sz_y_f; p_y++) {
|
||||
mvwprintw(win, p_y, p_x, "%*s", sz_x_f - 1, "");
|
||||
}
|
||||
|
||||
wattroff(win, A_BOLD);
|
||||
curs_set(false);
|
||||
}
|
||||
|
||||
void w_lfl_destroy(w_lfl_ui_t **fui) {
|
||||
w_lfl_ui_t *f = *fui;
|
||||
fl_clear(&(f->start), &(f->current));
|
||||
free(*fui);
|
||||
*fui = NULL;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#ifndef W_LFL_H /* LOCAL FILE LIST */
|
||||
#define W_LFL_H
|
||||
|
||||
#include "app.h"
|
||||
#include <widget_core.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct fs_fl_item {
|
||||
char *name;
|
||||
char *path;
|
||||
struct fs_fl_item *next;
|
||||
struct fs_fl_item *prev;
|
||||
u_char d_type;
|
||||
} w_lfl_item_t;
|
||||
|
||||
typedef struct {
|
||||
w_t w;
|
||||
w_lfl_item_t *current;
|
||||
w_lfl_item_t *start;
|
||||
w_lfl_item_t *page_start;
|
||||
int32_t current_idx;
|
||||
int32_t max_lines;
|
||||
int32_t cur_page;
|
||||
int32_t pages_num;
|
||||
size_t files_num;
|
||||
WINDOW *win_info;
|
||||
WINDOW *win_list;
|
||||
char *d_path;
|
||||
} w_lfl_ui_t;
|
||||
|
||||
void w_lfl_draw(w_lfl_ui_t *fl_ui);
|
||||
void w_lfl_destroy(w_lfl_ui_t **fui);
|
||||
void w_lfl_reset(w_lfl_ui_t *fl_ui);
|
||||
w_lfl_ui_t *w_lfl_init(WINDOW **win, w_t *w_parent);
|
||||
w_lfl_ui_t *w_lfl_init_win(w_app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "main_window.h"
|
||||
#include "fs_file_list.h"
|
||||
#include "file_list.h"
|
||||
|
||||
void main_window_set(w_app_t *app, enum main_window_type type) {
|
||||
if (app->main_ui.ui != NULL) {
|
||||
main_window_destroy(app);
|
||||
}
|
||||
app->main_ui.type = type;
|
||||
switch (type) {
|
||||
case mw_fl_server:
|
||||
w_fl_init(app);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
break;
|
||||
case mw_fl_local:
|
||||
w_lfl_init_win(app);
|
||||
break;
|
||||
case mw_f_desc:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void main_window_draw(w_app_t *app) {
|
||||
switch (app->main_ui.type) {
|
||||
case mw_fl_server:
|
||||
w_fl_draw((w_ui_file_list_t *)app->main_ui.ui);
|
||||
break;
|
||||
case mw_fl_local:
|
||||
w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui);
|
||||
break;
|
||||
case mw_f_desc:
|
||||
break;
|
||||
}
|
||||
w_app_draw_bbar(app);
|
||||
}
|
||||
|
||||
void main_window_destroy(w_app_t *app) {
|
||||
switch (app->main_ui.type) {
|
||||
case mw_fl_server:
|
||||
w_fl_destroy((w_ui_file_list_t **)&(app->main_ui.ui));
|
||||
break;
|
||||
case mw_fl_local:
|
||||
w_lfl_destroy((w_lfl_ui_t **)&(app->main_ui.ui));
|
||||
case mw_f_desc:
|
||||
break;
|
||||
}
|
||||
MAIN_UI_RESET(app);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#ifndef MAIN_WINDOW_H
|
||||
#define MAIN_WINDOW_H
|
||||
|
||||
#include <widget.h>
|
||||
|
||||
void main_window_set(w_app_t *app, enum main_window_type type);
|
||||
void main_window_draw(w_app_t *app);
|
||||
void main_window_destroy(w_app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#ifndef WINDOWS_H
|
||||
#define WINDOWS_H
|
||||
|
||||
#include "file_list.h"
|
||||
#include "fs_file_list.h"
|
||||
#include "main_window.h"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user