diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-28 12:48:10 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-28 12:51:53 +0330 |
commit | 412c8968057bac2e7df1a2bd47b6124cc25f21a7 (patch) | |
tree | 60237e47a32c9255a2131535500eb10a5f719776 | |
parent | 4cc1b51a5e532f14ece5b11c66cd56091eb97a97 (diff) |
some small changes and clean up
-rw-r--r-- | src/main.c | 9 | ||||
-rw-r--r-- | src/ui/tui.c | 30 |
2 files changed, 23 insertions, 16 deletions
@@ -1,6 +1,6 @@ #include "ui/tui.h" -#include <unistd.h> #include <stdio.h> +#include <unistd.h> void on_button_click(MOUSE_ACTION mouse_action) { printf("hello"); @@ -11,12 +11,17 @@ WIDGET *ui_build(TUI *tui) { return tui_make_box( -1, -1, tui_make_row(tui_make_widget_array( - 2, + 3, tui_make_box( 8, 2, tui_make_button(tui_make_text("Hello, World!", COLOR_BLUE), on_button_click), COLOR_YELLOW), + tui_make_box( + 8, 2, + tui_make_button(tui_make_text("Hello, World!", COLOR_RED), + on_button_click), + COLOR_BLUE), tui_make_text("Hello, World!", COLOR_RED), 1)), COLOR_MAGENTA); } 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]); } |