ui: file list

This commit is contained in:
2025-12-26 17:34:51 +02:00
parent e3fbe1b40a
commit b669b4be8a
16 changed files with 164 additions and 49 deletions
+20 -6
View File
@@ -7,16 +7,19 @@
// #include "modals/server_message.h"
// #include "action.h"
#include <ncurses.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
void ac_file(WINDOW *win, int is_action_w);
app_t *init_app() {
/* get values from terminal size */
int y_max, x_max;
int32_t y_max, x_max;
getmaxyx(stdscr, y_max, x_max);
/* allocate app struct memory */
@@ -29,7 +32,7 @@ app_t *init_app() {
_app->win = newwin(_app->coordinates.cur_y, _app->coordinates.cur_x, 0, 0);
/* define the width for each sub window */
int32_t left_w_x = x_max / 3;
int32_t left_w_x = x_max / 10 * 4;
int32_t right_w_x = x_max - left_w_x - 2;
/* create the menu window */
@@ -43,7 +46,7 @@ app_t *init_app() {
_app->modal.is_initiated = 0;
/* default win */
_app->active_win = aw_left;
_app->active_win_type = aw_left;
/* print decorative bars */
print_bars(_app);
@@ -93,6 +96,8 @@ void draw_borders(app_t *app) {
}
void print_bars(app_t *app) {
char top_text[64] = "Hello!";
wattrset(app->win, A_REVERSE);
/* print top and bottom bars */
@@ -102,9 +107,14 @@ void print_bars(app_t *app) {
}
/* add content to the top bar */
mvwprintw(app->win, 1, 2, "example cprint_barsurses app | ");
mvwprintw(app->win, 1, sizeof "example cprint_barsurses app | " + 1,
"size %d, %d", app->coordinates.cur_x, app->coordinates.cur_y);
if (app->params && app->params->is_connected) {
uint32_t ip_address = app->params->addr;
u_char ip_addr[4];
mempcpy(ip_addr, &ip_address, 4);
sprintf(top_text, "Connected to %u.%u.%u.%u, user: %s", ip_addr[0],
ip_addr[1], ip_addr[2], ip_addr[3], app->params->uname);
}
mvwprintw(app->win, 1, 2, "%s", top_text);
/* add content to the bottom bar */
mvwprintw(app->win, app->coordinates.cur_y - 2, 2, "F1 - Help | F9 - Quit");
@@ -160,6 +170,10 @@ void app_draw_modal(app_t *app) {
case S_NEXT_ACTION:
return;
}
app->active_callback = app->modal.w.callback;
app->active_win = app->modal.win;
app->active_win_type = aw_modal;
app->active_widget = &(app->modal);
}
draw_dialogue(&(app->modal));
}
+5 -1
View File
@@ -2,6 +2,7 @@
#define N_APP_H
#include "../common.h"
#include "widget.h"
#include "widget/dialogue.h"
#include <ncurses.h>
@@ -26,7 +27,10 @@ typedef struct {
query_args_t *query_args;
file_args_t *file_args;
int32_t read_fd;
active_win_t active_win;
active_win_t active_win_type;
void (*active_callback)(callback_args_t *args);
WINDOW *active_win;
void *active_widget;
} app_t;
app_t *init_app();
+3 -2
View File
@@ -18,10 +18,11 @@ void asa_modal_dialogue_callback(callback_args_t *args) {
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
dialogue_t *d = (dialogue_t *)args->widget;
dialogue_t *d = (dialogue_t *)app->active_widget;
memcpy(&d_args, args, sizeof(callback_args_t));
d_args.app = NULL;
d_args.resp_data = &response;
d_args.widget = app->active_widget;
input_t *in_ip = d->g_content->elements[0].element;
input_t *in_port = d->g_content->elements[1].element;
dialogue_default_callback(&d_args);
@@ -30,7 +31,7 @@ void asa_modal_dialogue_callback(callback_args_t *args) {
case 0:
get_ip_port(app->params, in_ip->value, in_port->value);
connect_to_server(app);
destroy_dialogue(d);
destroy_dialogue(d, app);
app->query_args->state = S_WAIT_SERVER;
break;
case 1:
+4 -2
View File
@@ -18,10 +18,11 @@ void init_login_credentials_modal_cb(callback_args_t *args) {
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
dialogue_t *d = (dialogue_t *)args->widget;
dialogue_t *d = (dialogue_t *)app->active_widget;
memcpy(&d_args, args, sizeof(callback_args_t));
d_args.app = NULL;
d_args.resp_data = &response;
d_args.widget = app->active_widget;
input_t *in_name = d->g_content->elements[0].element;
input_t *in_pass = d->g_content->elements[1].element;
dialogue_default_callback(&d_args);
@@ -36,7 +37,8 @@ void init_login_credentials_modal_cb(callback_args_t *args) {
app->params->pass[in_pass->value_len] = 0;
write(app->params->sd, app->params->uname, in_name->value_len);
app->query_args->state = S_WAIT_SERVER;
destroy_dialogue(d);
destroy_dialogue(d,app);
print_bars(app);
break;
case 1:
destroy_app(app);
+3 -2
View File
@@ -15,17 +15,18 @@ void login_modal_option_cb(callback_args_t *args) {
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
dialogue_t *d = (dialogue_t *)args->widget;
dialogue_t *d = (dialogue_t *)app->active_widget;
memcpy(&d_args, args, sizeof(callback_args_t));
d_args.app = NULL;
d_args.resp_data = &response;
d_args.widget = app->active_widget;
dialogue_default_callback(&d_args);
if (response > -1) {
switch (response) {
case 0:
break;
case 1:
destroy_dialogue(d);
destroy_dialogue(d, app);
app->query_args->state = S_ASK_LOGIN_USER;
break;
}
+3 -2
View File
@@ -16,15 +16,16 @@ void server_message_modal_cb(callback_args_t *args) {
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
dialogue_t *d = (dialogue_t *)args->widget;
dialogue_t *d = (dialogue_t *)app->active_widget;
memcpy(&d_args, args, sizeof(callback_args_t));
d_args.app = NULL;
d_args.resp_data = &response;
d_args.widget = app->active_widget;
dialogue_default_callback(&d_args);
if (response > -1) {
switch (response) {
case 0:
destroy_dialogue(d);
destroy_dialogue(d, app);
/* TODO: macros */
free(app->query_args->server_message.text);
app->query_args->server_message.capacity = 0;
-1
View File
@@ -14,7 +14,6 @@ enum w_type {
};
typedef struct {
WINDOW *win;
void *app;
void *widget;
void *data;
+6 -1
View File
@@ -5,6 +5,7 @@
#include "../app.h"
#include "dialogue.h"
#include "file_list.h"
#include "group.h"
#include "input.h"
@@ -251,7 +252,8 @@ int32_t draw_dialogue(dialogue_t *d) {
return 0;
};
void destroy_dialogue(dialogue_t *d) {
void destroy_dialogue(dialogue_t *d, void *_app) {
app_t *app = (app_t *)_app;
if (d->g_content) {
destroy_group(d->g_content);
}
@@ -261,4 +263,7 @@ void destroy_dialogue(dialogue_t *d) {
delwin(d->win);
d->win = NULL;
d->is_initiated = 0;
app->active_win_type = aw_left;
app->active_win = app->left_win;
app->active_callback = file_list_cb;
}
+1 -1
View File
@@ -27,7 +27,7 @@ typedef struct {
void init_dialogue(dialogue_t *d, const char title[], const char text[],
coordinates_t *p_coordinates);
int32_t draw_dialogue(dialogue_t *d);
void destroy_dialogue(dialogue_t *d);
void destroy_dialogue(dialogue_t *d, void *app);
void dialogue_default_callback(callback_args_t *args);
void dialogue_init_active_id(dialogue_t *dialogue);
+87 -4
View File
@@ -1,31 +1,114 @@
#include "file_list.h"
#include "../app.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
void file_list_cb(callback_args_t *args) {
app_t *app = args->app;
file_list_t *fui = app->query_args->file_list_ui;
int32_t key = *((int32_t *)args->data);
switch (key) {
case KEY_DOWN:
if (fui->current_idx < fui->count - 1) {
fui->current_idx++;
redraw_file_list(fui);
}
break;
case KEY_UP:
if (fui->current_idx > 0) {
fui->current_idx--;
redraw_file_list(fui);
}
break;
}
}
file_list_t *init_file_list(WINDOW **win) {
file_list_t *fl_ui = malloc(sizeof(file_list_t));
init_widget(&(fl_ui->w), NULL, win,"");
init_widget(&(fl_ui->w), NULL, win, "");
fl_ui->current_idx = 0;
fl_ui->current_page = 0;
fl_ui->pages = 0;
fl_ui->count = 0;
return fl_ui;
}
void draw_file_list(file_list_t *fl_ui, fl_item_t **fl_start) {
void reset_file_list(file_list_t *fl_ui) {
fl_ui->current_idx = 0;
fl_ui->current_page = 0;
fl_ui->pages = 0;
fl_ui->count = 0;
}
void draw_file_list(file_list_t *fl_ui) {
int32_t sz_y, sz_x;
int32_t p_y, p_x;
getmaxyx(*(fl_ui->w.parent_win), sz_y, sz_x);
p_y = 1;
p_x = 1;
fl_item_t *el = *fl_start;
fl_item_t *el = fl_ui->start;
int32_t cur_el_idx = 0;
curs_set(0);
do {
if (cur_el_idx == fl_ui->current_idx) {
wattrset(*(fl_ui->w.parent_win), A_BOLD | A_REVERSE);
}
p_x = 1;
mvwprintw(*(fl_ui->w.parent_win), p_y, p_x, "%s%n", el->name, &p_x);
for (uint i = 1; p_x < sz_x; i++) {
mvwprintw(*(fl_ui->w.parent_win), p_y, ++p_x, " ");
mvwprintw(*(fl_ui->w.parent_win), p_y, ++p_x, " ");
}
if (cur_el_idx == fl_ui->current_idx) {
wattroff(*(fl_ui->w.parent_win), A_BOLD | A_REVERSE);
}
p_y++;
cur_el_idx++;
} while ((el = el->next) != NULL && p_y < sz_y);
}
void redraw_file_list(file_list_t *fl_ui) {
static uint32_t active_idx = 0;
int32_t sz_y, sz_x;
int32_t p_y, p_x;
getmaxyx(*(fl_ui->w.parent_win), sz_y, sz_x);
int32_t sz_y_f = sz_y - 2; // factual size (without box)
p_y = 1;
p_x = 1;
fl_item_t *el = fl_ui->start;
int32_t cur_el_idx = 0;
bool sm_size = sz_y_f < fl_ui->count;
bool print_all = false;
int32_t diff = 0;
if (fl_ui->current_idx >= sz_y_f) {
print_all = true;
diff = fl_ui->current_idx - sz_y_f + 1;
} else if (active_idx - fl_ui->current_idx == 1) {
print_all = true;
}
do {
if (cur_el_idx == fl_ui->current_idx) {
wattrset(*(fl_ui->w.parent_win), A_BOLD | A_REVERSE);
} else if (cur_el_idx != active_idx && !print_all) {
p_y++;
cur_el_idx++;
continue;
}
p_x = 1;
mvwprintw(*(fl_ui->w.parent_win), p_y-diff, p_x, "%s%n", el->name, &p_x);
for (uint i = 1; p_x < sz_x; i++) {
mvwprintw(*(fl_ui->w.parent_win), p_y-diff, ++p_x, " ");
}
if (cur_el_idx == fl_ui->current_idx) {
wattroff(*(fl_ui->w.parent_win), A_BOLD | A_REVERSE);
}
p_y++;
cur_el_idx++;
} while ((el = el->next) != NULL && p_y-diff < sz_y);
active_idx = fl_ui->current_idx;
}
+6 -1
View File
@@ -7,8 +7,13 @@ typedef struct {
fl_item_t *current;
fl_item_t *start;
int32_t current_idx;
uint32_t count;
uint32_t pages;
uint32_t current_page;
} file_list_t;
void draw_file_list(file_list_t *fl_ui, fl_item_t **fl_start);
void draw_file_list(file_list_t *fl_ui);
void redraw_file_list(file_list_t *fl_ui);
file_list_t *init_file_list(WINDOW **win);
void file_list_cb(callback_args_t *args);
#endif