diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-30 12:31:23 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-30 12:31:23 +0200 |
commit | 03862852b128748358dcb39ce50600eef0ba1a71 (patch) | |
tree | d5052d02d7bb31a866d5829eb05cfaf9b35bfa59 /client/ui.c | |
parent | 555e5d2c0ce77dd1e031690c98ec9a1366182347 (diff) |
use ncurses instead of poopy ui library
Diffstat (limited to 'client/ui.c')
-rw-r--r-- | client/ui.c | 132 |
1 files changed, 37 insertions, 95 deletions
diff --git a/client/ui.c b/client/ui.c index 1f42db5..c47e1fb 100644 --- a/client/ui.c +++ b/client/ui.c @@ -1,4 +1,5 @@ #include <string.h> +#include <ncurses.h> #include "../shared/bin.h" #include "../shared/util.h" @@ -7,113 +8,54 @@ #include "term.h" #include "ui.h" -w2_s_ui_tty_canvas g_w2_ui_canvas; +WINDOW *g_w2_ui_win; +unsigned int g_w2_ui_width = 0; +unsigned int g_w2_ui_height = 0; void w2_ui_main() { - w2_ui_update(); - w2_ui_paint(); -} - -void w2_ui_update() { w2_term_props(); } - -void w2_ui_paint() { w2_ui_paint_statusbar(); } - -void w2_ui_align(char *text, w2_e_alignment align, unsigned int length) { - unsigned int padding = 0; + g_w2_ui_width = getmaxx(g_w2_ui_win); + g_w2_ui_height = getmaxy(g_w2_ui_win); - switch (align) { - case W2_UI_ALIGN_LEFT: { - break; - } - case W2_UI_ALIGN_CENTER: { - unsigned int middle = length / 2; - unsigned int offset = strlen(text) / 2; - padding = W2_MAX(0, middle - offset); - break; - } - case W2_UI_ALIGN_RIGHT: { - unsigned int right = length; - unsigned int offset = strlen(text); - padding = W2_MAX(0, right - offset); - break; - } - } - - char *temp = calloc(length, 1); - sprintf(temp, "%*s%s", padding, "", text); - memcpy(text, temp, length); - free(temp); -} - -char *w2_ui_pt_sb_con_sts() { - char *connected = - g_w2_state.connected ? W2_UI_CONN_STAT_CONNECTED : W2_UI_CONN_STAT_DISCONNECTED; - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "%s, %ims %s", connected, g_w2_state.ping, W2_UI_CONN_STAT_PING); - w2_ui_align(out, W2_UI_ALIGN_LEFT, g_w2_ui_canvas.width); - return out; -} -char *w2_ui_pt_sb_ver_num() { - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "(%s)", g_w2_state.info.build_str); - w2_ui_align(out, W2_UI_ALIGN_CENTER, g_w2_ui_canvas.width); - return out; -} -char *w2_ui_pt_sb_bat_sts() { - char *out = calloc(g_w2_ui_canvas.width, 1); - sprintf(out, "%s %i%%", W2_UI_BATT_STAT_BATTERY, g_w2_state.battery_level); - w2_ui_align(out, W2_UI_ALIGN_RIGHT, g_w2_ui_canvas.width); - return out; + w2_ui_paint(); } -void w2_ui_nullsub(char *str, unsigned int length) { - for (int i = 0; i < length; i++) - if (str[i] == 0x00) str[i] = ' '; +void w2_ui_paint() { + w2_ui_paint_statusbar(); + refresh(); } -void w2_ui_overlay(char *bottom, char *top, unsigned int length) { - w2_ui_nullsub(bottom, length); - w2_ui_nullsub(top, length); - - unsigned int start = 0; - for (int i = 0; i < length; i++) { - start = i; - if (top[start] != ' ') break; - } - unsigned int stop = 0; - for (int i = 0; i < length; i++) { - stop = length - i; - if (top[stop] != ' ') break; - } - - for (int i = 0; i < length; i++) { - if (i >= start && i <= stop) bottom[i] = top[i]; - } -} +void w2_ui_paint_statusbar() { + char temp[g_w2_ui_width + 1]; + sprintf(temp, "%s, %ims %s", + g_w2_state.connected ? W2_UI_CONN_STAT_CONNECTED : W2_UI_CONN_STAT_DISCONNECTED, + g_w2_state.ping, + W2_UI_CONN_STAT_PING); + mvaddstr(0, 0, temp); -// char* w2_ui_pt_sb_logic_mode() {} -// char* w2_ui_pt_sb_exceptions() {} -// char* w2_ui_pt_sb_tabbar() {} + sprintf(temp, "(%s)", g_w2_state.info.build_str); + mvaddstr(0, g_w2_ui_width / 2 - strlen(temp) / 2, temp); -void w2_ui_paint_statusbar() { - char top_row[g_w2_ui_canvas.width]; + sprintf(temp, "%s %i%%", W2_UI_BATT_STAT_BATTERY, g_w2_state.battery_level); + mvaddstr(0, g_w2_ui_width - strlen(temp), temp); - char *con_sts = w2_ui_pt_sb_con_sts(); - char *ver_num = w2_ui_pt_sb_ver_num(); - char *bat_sts = w2_ui_pt_sb_bat_sts(); + sprintf(temp, "[mode 0x%02x]", g_w2_state.mode); + mvaddstr(1, 0, temp); - w2_ui_overlay(top_row, con_sts, g_w2_ui_canvas.width); - w2_ui_overlay(top_row, ver_num, g_w2_ui_canvas.width); - w2_ui_overlay(top_row, bat_sts, g_w2_ui_canvas.width); + sprintf(temp, "%i %s, %i %s", + 0, W2_UI_EXPT_STAT_WARNINGS, + 0, W2_UI_EXPT_STAT_ERRORS); + mvaddstr(1, g_w2_ui_width - strlen(temp), temp); - printf("%s", top_row); + w2_ui_paint_tabbar(); - free(con_sts); - free(ver_num); - free(bat_sts); + for (unsigned int i = 0; i < g_w2_ui_width; i++) temp[i] = '-'; + temp[g_w2_ui_width] = 0; + mvaddstr(3, 0, temp); +} - // w2_ui_pt_sb_connection_status(); - // w2_ui_pt_sb_connection_status(); - // w2_ui_pt_sb_connection_status(); - printf("\n"); +void w2_ui_paint_tabbar() { + char temp[g_w2_ui_width]; + sprintf(temp, "-- tab bar here --"); + mvaddstr(2, g_w2_ui_width / 2 - strlen(temp) / 2, temp); } + |