From 412c8968057bac2e7df1a2bd47b6124cc25f21a7 Mon Sep 17 00:00:00 2001 From: A404M Date: Wed, 28 Aug 2024 12:48:10 +0330 Subject: some small changes and clean up --- src/ui/tui.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/ui') diff --git a/src/ui/tui.c b/src/ui/tui.c index 54ae511..664f7a0 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -76,11 +76,10 @@ void tui_refresh(TUI *tui) { ioctl(STDOUT_FILENO, TIOCGWINSZ, &tui->size); - if (width == tui_get_width(tui) && height == tui_get_height(tui)) { - return; + if (width != tui_get_width(tui) || height != tui_get_height(tui)) { + _tui_delete_cells(tui); + _tui_init_cells(tui); } - _tui_delete_cells(tui); - _tui_init_cells(tui); } int tui_get_width(TUI *tui) { return tui->size.ws_col; } @@ -151,7 +150,8 @@ void _tui_set_cell_on_click_callback(TUI *tui, int x, int y, void tui_handle_mouse_action(TUI *tui, MOUSE_ACTION mouse_action) { const ON_CLICK_CALLBACK callback = - tui->cells[_tui_get_cell_index(tui, mouse_action.x, mouse_action.y)].on_click_callback; + tui->cells[_tui_get_cell_index(tui, mouse_action.x, mouse_action.y)] + .on_click_callback; if (callback != NULL) { callback(mouse_action); } @@ -343,6 +343,10 @@ void _tui_draw_widget_to_cells(TUI *tui, const WIDGET *widget, int width_begin, } } +int _tui_move_to_in_str(char *str, int x, int y) { + return sprintf(str, "\033[%d;%dH", y + 1, x + 1); +} + int _tui_get_background_color_ascii(COLOR color) { if (color == COLOR_NO_COLOR) { return 0; @@ -354,9 +358,11 @@ int _tui_get_background_color_ascii(COLOR color) { void _tui_draw_cells_to_terminal(TUI *tui) { const size_t size_of_cell = 5 + 5 + sizeof(char) + 5; - const size_t size = tui->cells_length * (size_of_cell); - char str[(size + 1) * sizeof(char)]; - str[0] = '\0'; + const size_t size = tui->cells_length * size_of_cell; + char str[(size + 2) * sizeof(char) + 1]; + + _tui_move_to_in_str(str, 0, 0); + char cell_str[5]; COLOR last_color = COLOR_NO_COLOR; @@ -369,7 +375,7 @@ void _tui_draw_cells_to_terminal(TUI *tui) { last_background_color != cell.background_color) { sprintf(cell_str, "\033[%dm", COLOR_RESET); strcat(str, cell_str); - last_color = cell.color; // TODO: run to know what to fix + last_color = cell.color; last_background_color = cell.background_color; if (cell.color == COLOR_RESET || cell.color == COLOR_NO_COLOR) { sprintf(cell_str, "\033[%dm", COLOR_RESET); @@ -390,13 +396,11 @@ void _tui_draw_cells_to_terminal(TUI *tui) { } const int len = strlen(str); - tui_move_to(0, 0); write(STDOUT_FILENO, str, len); } void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder) { while (1) { - clock_t start = clock(); tui_refresh(tui); WIDGET *root_widget = widget_builder(tui); tui_save_cursor(); @@ -408,8 +412,6 @@ void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder) { _tui_draw_cells_to_terminal(tui); tui_delete_widget(root_widget); - tui_move_to(30, 30); - printf("%ld",clock()-start); tui_restore_cursor(); if (handle_input(tui)) { return; @@ -557,7 +559,7 @@ WIDGET_ARRAY *tui_make_widget_array(int size, ...) { return widget_array; } -void _tui_delete_widget_array(WIDGET_ARRAY *widget_array){ +void _tui_delete_widget_array(WIDGET_ARRAY *widget_array) { for (size_t i = 0; i < widget_array->size; ++i) { tui_delete_widget(widget_array->widgets[i]); } -- cgit v1.2.3