ui: brings back text centering in modals | fixes error on exit | small clean-up

This commit is contained in:
2026-01-05 16:09:23 +02:00
parent 23fda5232d
commit bdb194679e
21 changed files with 96 additions and 61 deletions
+1 -2
View File
@@ -20,8 +20,7 @@ void connect_to_server(app_t *app) {
if (-1 ==
connect(app->params->sd, (struct sockaddr *)&server, sizeof(server))) {
perror("connect");
destroy_app(app);
exit(2);
destroy_app(app, 2);
}
app->params->is_connected = TRUE;
print_bars(app);
+1 -2
View File
@@ -25,5 +25,4 @@ query.o: src/query.c src/client.h src/main.h src/types.h src/connection.h \
src/ui/widget/../../common.h src/ui/widget/button.h \
src/ui/widget/../../main.h src/ui/widget/group.h src/file_processor.h \
src/ui/widget/file_list.h src/server.h
server.o: src/server.c src/main.h src/types.h src/ui.h
ui.o: src/ui.c
server.o: src/server.c src/main.h src/types.h
+7 -3
View File
@@ -57,7 +57,7 @@ int main(int argc, char **argv) {
query_loop(app);
clear_params(&params);
destroy_app(app);
destroy_app(app, 0);
return OK;
}
@@ -67,8 +67,12 @@ int32_t process_user_input(app_t *app, callback_args_t *d_args) {
c = wgetch(app->win);
switch (c) {
case KEY_F(9):
destroy_app(app);
return ERR;
destroy_app(app, 0);
return OK;
case 'U':
case 'u':
app->query_args->state = S_UPLOAD_FILE;
break;
default:
d_args->data = (void *)&c;
app->active_callback(d_args);
-1
View File
@@ -45,7 +45,6 @@ enum state {
WAIT_REGISTER,
WAIT_REGISTER_CONFIRMATION,
WAIT_CLIENT,
UPLOAD_FILE,
};
typedef struct p_file {
-1
View File
@@ -144,7 +144,6 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
case WAIT_REGISTER_CONFIRMATION:
wait_side(query_args);
break;
case UPLOAD_FILE:
case S_ERR:
break;
case S_FILE_LIST:
-1
View File
@@ -1,5 +1,4 @@
#include "main.h"
#include "ui.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
-25
View File
@@ -1,25 +0,0 @@
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
uint32_t print_ask_list(char title[], const char *options[]) {
char c;
uint32_t i, answer;
printf("%s\n", title);
for (i = 0; options[i] != NULL; i++) {
printf("[%u] %s\n", i + 1, options[i]);
}
write(STDOUT_FILENO, "> ", sizeof "> ");
while (!scanf("%u", &answer) || answer > i) {
printf("Input format is not correct! You need to specify between 1 and "
"%u\n > ",
i);
}
while ((c = getchar()) != '\n' && c != EOF);
return answer;
}
-3
View File
@@ -1,3 +0,0 @@
#include <stdint.h>
uint32_t print_ask_list(char *title, const char *options[]);
+9 -10
View File
@@ -5,6 +5,7 @@
#include "modals/login_option.h"
#include "modals/notification.h"
#include "modals/server_message.h"
#include "modals/upload_dialogue.h"
#include "widget/dialogue.h"
#include <ncurses.h>
#include <netinet/in.h>
@@ -90,12 +91,6 @@ void draw_borders(app_t *app) {
box(app->win, 0, 0);
box(app->left_win, 0, 0);
box(app->right_win, 0, 0);
/* refresh the windows */
wnoutrefresh(app->win);
wnoutrefresh(app->left_win);
wnoutrefresh(app->right_win);
doupdate();
}
void print_bars(app_t *app) {
@@ -120,7 +115,8 @@ void print_bars(app_t *app) {
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");
mvwprintw(app->win, app->coordinates.cur_y - 2, 2,
"F1 - Help | U - upload | F9 - Quit");
wattroff(app->win, A_REVERSE);
}
@@ -161,16 +157,17 @@ void app_draw_modal(app_t *app) {
case S_FILE_DOWNLOAD:
init_dwn_pr_modal(app);
break;
case S_UPLOAD_FILE:
init_upload_dialogue_modal(app);
break;
case WAIT_SERVER_INIT:
case WAIT_SERVER:
case WAIT_REGISTER:
case WAIT_REGISTER_CONFIRMATION:
case WAIT_CLIENT:
case UPLOAD_FILE:
case S_FILE_LIST:
case S_FILE_SELECT:
case S_UPLOAD_PARAMS:
case S_UPLOAD_FILE:
case S_UPLOAD_REQUESTED:
case S_ASK_USER_BEFORE_LOGIN:
case S_ERR:
@@ -191,7 +188,9 @@ void app_draw_modal(app_t *app) {
draw_dialogue(&(app->modal));
}
void destroy_app(app_t *app) {
void destroy_app(app_t *app, int32_t exit_code) {
delwin(app->win);
endwin();
app->win = NULL;
exit(exit_code);
}
+1 -1
View File
@@ -35,7 +35,7 @@ typedef struct {
app_t *init_app();
void init_nc();
void destroy_app(app_t *app);
void destroy_app(app_t *app, int32_t exit_code);
void draw_borders(app_t *app);
void print_bars(app_t *app);
void app_refresh(app_t *app);
+1 -2
View File
@@ -35,8 +35,7 @@ void asa_modal_dialogue_callback(callback_args_t *args) {
app->query_args->state = S_WAIT_SERVER;
break;
case 1:
destroy_app(app);
exit(0);
destroy_app(app, 0);
}
}
}
+2 -2
View File
@@ -5,7 +5,7 @@
#include <stdint.h>
#include <stdio.h>
void init_dwn_pr_modal_cb(callback_args_t *args) {
void dwn_pr_modal_cb(callback_args_t *args) {
}
dialogue_t *init_dwn_pr_modal(app_t *app) {
@@ -23,7 +23,7 @@ dialogue_t *init_dwn_pr_modal(app_t *app) {
init_dialogue(&(app->modal), "Download", text, &(app->coordinates));
dialogue_t *d = &(app->modal);
d->w.callback = init_dwn_pr_modal_cb;
d->w.callback = dwn_pr_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
+1 -2
View File
@@ -41,8 +41,7 @@ void init_login_credentials_modal_cb(callback_args_t *args) {
print_bars(app);
break;
case 1:
destroy_app(app);
exit(0);
destroy_app(app, 0);
}
}
}
+57
View File
@@ -0,0 +1,57 @@
#include "../app.h"
#include "../widget/dialogue.h"
#include "../widget/group.h"
#include <ncurses.h>
#include <stdint.h>
#include <string.h>
void upload_dialogue_modal_cb(callback_args_t *args) {
int32_t response;
callback_args_t d_args;
app_t *app = args->app;
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:
app->query_args->state = S_WAIT_SERVER;
break;
case 1:
break;
}
}
}
dialogue_t *init_upload_dialogue_modal(app_t *app) {
if (app == NULL)
return NULL;
group_el_init_t content[] = {{.type = w_input,
.label = "File description",
.length = 30}, /* length is ignored */
{.type = w_end}};
group_el_init_t actions[] = {
{.type = w_button, .label = "Upload", .is_default = 1},
{.type = w_button, .label = "Cancel", .is_default = 0},
{.type = w_end}};
app->modal.w.parent_win = &app->win;
init_dialogue(&(app->modal), "Upload new file", "Enter essential data",
&(app->coordinates));
dialogue_t *d = &(app->modal);
d->w.callback = upload_dialogue_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
app->query_args->progress_bar = d->g_content->elements[0].element;
app->query_args->active_dialogue = d;
dialogue_init_active_id(d);
return NULL;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef UPLOAD_DIALOGUE_H
#define UPLOAD_DIALOGUE_H
#include "../widget/dialogue.h"
#include "../app.h"
dialogue_t *init_upload_dialogue_modal(app_t *app);
#endif
+2 -2
View File
@@ -58,14 +58,14 @@ int32_t get_max_line_len(const char *text, uint32_t *line_count) {
mvwprintw(win, line_v_pos, x, "%s", l_buf); \
}
uint32_t print_multiline_text(WINDOW *win, const char *_text, const uint32_t y,
uint32_t print_multiline_text(WINDOW *win, const char *_text,
const uint32_t win_width, const uint32_t y,
const uint32_t x, const uint16_t attrs) {
uint16_t line_v_pos = y;
int32_t c_line_len = 0;
uint32_t m_line_len = 0;
uint32_t i = 0;
uint32_t win_width = getmaxx(win) - 2; /* -2: borders */
char l_buf[DIALOGUE_TEXT];
char *text = malloc(strlen(_text) + 1);
strcpy(text, _text);
+1 -1
View File
@@ -58,7 +58,7 @@ enum pmt_attrs {
void init_widget(widget_t *w, widget_t *w_parent, WINDOW **win, char *title);
int32_t get_max_line_len(const char *text, uint32_t *line_count);
uint32_t print_multiline_text(WINDOW *win, const char *text, const uint32_t y,
uint32_t print_multiline_text(WINDOW *win, const char *text, const uint32_t win_width, const uint32_t y,
const uint32_t x, const uint16_t attrs);
#endif
+1 -1
View File
@@ -239,7 +239,7 @@ int32_t draw_dialogue(dialogue_t *d) {
/* text */
wattroff(d->win, A_BOLD);
/* mvwhline(d->win, d->w.y - 3, 1, 0, d->w.x - 2); */
print_multiline_text(d->win, d->text, 2, 1, PMT_ALIGN_CENTER);
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
wattroff(d->win, A_REVERSE);
if (d->g_content != NULL) {
+1 -1
View File
@@ -144,5 +144,5 @@ void draw_file_list(ui_file_list_t *fl_ui) {
mvwprintw(i_win, p_y++, p_x, "Size: %zu", active_el->size);
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, p_y, p_x, 0);
print_multiline_text(i_win, active_el->description, sz_x, p_y, p_x, 0);
}
+1
View File
@@ -2,6 +2,7 @@
#define FILE_LIST_H
#include "../widget.h"
#include <stdint.h>
typedef struct {
widget_t w;
fl_item_t **current;
+1 -1
View File
@@ -107,7 +107,7 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
w = &(((ui_progress_bar_t *)elements[i].element)->w);
break;
}
// set dimensions
/* set dimensions */
elements[i].id = w->id;
if (direction == horizontal) {
w->m_x = group->w.m_x + 1 + group->w.x;