From b653fdcc571dfed5f04aa2c3fc042b07716f349b Mon Sep 17 00:00:00 2001 From: rmntgx Date: Sun, 29 Jun 2025 23:32:04 +0500 Subject: [PATCH] feat: buggy resize implementation --- src/ui.c | 78 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/src/ui.c b/src/ui.c index 85ab74c..49c870e 100644 --- a/src/ui.c +++ b/src/ui.c @@ -46,6 +46,7 @@ static void print_session(struct uint_point origin, struct session session, static void print_user(struct uint_point origin, struct user user, bool multiple); static void print_passwd(struct uint_point origin, uint length, bool err); +static void print_ui(); // ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 static struct termios orig_term; @@ -54,6 +55,23 @@ static struct winsize window; #define INNER_BOX_OUT_MARGIN 2 struct config* g_config = NULL; + +void resize_handler(int) { + ioctl(STDOUT_FILENO, TIOCGWINSZ, &window); + + if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 || + window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) { + printf("\033[2J\033[H"); // Clear screen + printf("\x1b[1;31mScreen too small\x1b[0m\n"); + printf("\x1b[%s;%sm\x1b[2J", g_config->colors.bg, g_config->colors.fg); + return; + } + + printf("\033[2J\033[H"); // Clear screen + + print_ui(); +} + void setup(struct config* config) { g_config = config; ioctl(STDOUT_FILENO, TIOCGWINSZ, &window); @@ -81,6 +99,7 @@ void setup(struct config* config) { print_footer(); (void)atexit(restore_all); (void)signal(SIGINT, signal_handler); + (void)signal(SIGWINCH, resize_handler); } static struct uint_point box_start() { @@ -173,12 +192,7 @@ void ui_update_ofield(struct opts_field* NNULLABLE self) { } static char* unknown_str = "unknown"; -// NOLINTNEXTLINE(readability-function-cognitive-complexity) -int load(struct Vector* users, struct Vector* sessions) { - /// SETUP - gusers = users; - gsessions = sessions; - +void print_ui() { // hostnames larger won't render properly const u_char HOSTNAME_SIZE = VALUES_COL - VALUES_SEPR - BOX_HMARGIN; char hostname_buf[HOSTNAME_SIZE]; @@ -194,6 +208,35 @@ int load(struct Vector* users, struct Vector* sessions) { // 1]); // *hidx = '\0'; } + /// PRINTING + const struct uint_point BOXSTART = box_start(); + + // printf box + print_box(); + + // put hostname + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname), + g_config->colors.e_hostname, hostname, g_config->colors.fg); + + // put date + char* fmtd_time = fmt_time(g_config->behavior.timefmt); + printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, + BOXSTART.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time), + g_config->colors.e_date, fmtd_time, g_config->colors.fg); + free(fmtd_time); + + ui_update_field(SESSION); + ui_update_field(USER); + ui_update_field(PASSWD); + ui_update_cursor_focus(); +} + +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +int load(struct Vector* users, struct Vector* sessions) { + /// SETUP + gusers = users; + gsessions = sessions; of_session = ofield_new(sessions->length + g_config->behavior.include_defshell); @@ -228,28 +271,7 @@ int load(struct Vector* users, struct Vector* sessions) { free(initial_state.session_opt); } - /// PRINTING - const struct uint_point BOXSTART = box_start(); - - // printf box - print_box(); - - // put hostname - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, - BOXSTART.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname), - g_config->colors.e_hostname, hostname, g_config->colors.fg); - - // put date - char* fmtd_time = fmt_time(g_config->behavior.timefmt); - printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", BOXSTART.y + HEAD_ROW, - BOXSTART.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time), - g_config->colors.e_date, fmtd_time, g_config->colors.fg); - free(fmtd_time); - - ui_update_field(SESSION); - ui_update_field(USER); - ui_update_field(PASSWD); - ui_update_cursor_focus(); + print_ui(); /// INTERACTIVE u_char len;