summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c6
-rw-r--r--src/ui/tui.c25
-rw-r--r--src/ui/tui.h21
3 files changed, 27 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index 77c5f49..da6dc5c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,9 +12,9 @@ void on_button_click(const MOUSE_ACTION *mouse_action) {
WIDGET *ui_build(TUI *tui) {
if (is_clicked) {
- char frame[20+3+1];
+ char frame[20+4+1];
const uint64_t fps = 1000000000/tui->last_frame;
- sprintf(frame, "%ldfps", fps);
+ sprintf(frame, "%ldfps\n", fps);
return tui_make_box(
-1, -1,
tui_make_column(tui_make_widget_array(
@@ -25,7 +25,7 @@ WIDGET *ui_build(TUI *tui) {
20, 3,
tui_make_column(tui_make_widget_array(
tui_make_text(frame, COLOR_BLUE),
- tui_make_button(tui_make_text(" Back", COLOR_RED),
+ tui_make_button(tui_make_text("Back", COLOR_RED),
on_button_click))),
COLOR_WHITE))))),
COLOR_MAGENTA);
diff --git a/src/ui/tui.c b/src/ui/tui.c
index 5f6acaa..ca87655 100644
--- a/src/ui/tui.c
+++ b/src/ui/tui.c
@@ -282,7 +282,7 @@ void _tui_draw_widget_to_cells(TUI *tui, const WIDGET *widget, int width_begin,
const int y = height;
const char c = metadata->text[inserted_index];
inserted_index += 1;
- if (c == '\n') {
+ if (c == '\n') {// do for other spaces
height += 1;
j = 0;
goto START_OF_HORIZONTAL_LOOP;
@@ -388,9 +388,7 @@ 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);
-}
+void _tui_move_to_start_in_str(char *str) { strcpy(str, "\033[;H"); }
int _tui_get_background_color_ascii(COLOR color) {
if (color == COLOR_NO_COLOR) {
@@ -406,7 +404,7 @@ void _tui_draw_cells_to_terminal(TUI *tui) {
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);
+ _tui_move_to_start_in_str(str);
char cell_str[5];
@@ -452,20 +450,21 @@ int kbhit() {
return select(1, &fds, NULL, NULL, &tv) > 0;
}
-bool widget_array_eqauls(const WIDGET_ARRAY *restrict left,
- const WIDGET_ARRAY *restrict right) {
+bool tui_widget_array_eqauls(const WIDGET_ARRAY *restrict left,
+ const WIDGET_ARRAY *restrict right) {
if (left->size != right->size) {
return false;
}
for (size_t i = 0; i < left->size; ++i) {
- if (!widget_eqauls(left->widgets[i], right->widgets[i])) {
+ if (!tui_widget_eqauls(left->widgets[i], right->widgets[i])) {
return false;
}
}
return true;
}
-bool widget_eqauls(const WIDGET *restrict left, const WIDGET *restrict right) {
+bool tui_widget_eqauls(const WIDGET *restrict left,
+ const WIDGET *restrict right) {
if (left == NULL || right == NULL) {
return left == NULL && right == NULL;
}
@@ -484,17 +483,17 @@ bool widget_eqauls(const WIDGET *restrict left, const WIDGET *restrict right) {
const BUTTON_METADATA *left_data = left->metadata;
const BUTTON_METADATA *right_data = right->metadata;
return left_data->callback == right_data->callback &&
- widget_eqauls(left_data->child, right_data->child);
+ tui_widget_eqauls(left_data->child, right_data->child);
} break;
case WIDGET_TYPE_COLUMN: {
const COLUMN_METADATA *left_data = left->metadata;
const COLUMN_METADATA *right_data = right->metadata;
- return widget_array_eqauls(left_data->children, right_data->children);
+ return tui_widget_array_eqauls(left_data->children, right_data->children);
} break;
case WIDGET_TYPE_ROW: {
const ROW_METADATA *left_data = left->metadata;
const ROW_METADATA *right_data = right->metadata;
- return widget_array_eqauls(left_data->children, right_data->children);
+ return tui_widget_array_eqauls(left_data->children, right_data->children);
} break;
case WIDGET_TYPE_BOX: {
const BOX_METADATA *left_data = left->metadata;
@@ -502,7 +501,7 @@ bool widget_eqauls(const WIDGET *restrict left, const WIDGET *restrict right) {
return left_data->width == right_data->width &&
left_data->height == right_data->height &&
left_data->color == right_data->color &&
- widget_eqauls(left_data->child, right_data->child);
+ tui_widget_eqauls(left_data->child, right_data->child);
} break;
default:
fprintf(stderr, "Type error '%d' in tui_delete_widget\n", left->type);
diff --git a/src/ui/tui.h b/src/ui/tui.h
index baf0c3a..86efa1d 100644
--- a/src/ui/tui.h
+++ b/src/ui/tui.h
@@ -30,11 +30,10 @@ typedef struct MOUSE_ACTION {
typedef void (*ON_CLICK_CALLBACK)(const MOUSE_ACTION *mouse_action);
-
#ifndef __cplusplus
-#if (__STDC_VERSION__ < 202000L)
+ #if (__STDC_VERSION__ < 202000L)
typedef enum bool : uint8_t { false = 0, true = 1 } bool;
-#endif
+ #endif
#endif
typedef enum COLOR {
@@ -62,7 +61,7 @@ typedef struct TUI {
int init_cursor_x, init_cursor_y;
TERMINAL_CELL *cells;
size_t cells_length;
- uint64_t last_frame; // in nanoseconds
+ uint64_t last_frame; // in nanoseconds
} TUI;
typedef enum WIDGET_TYPE {
@@ -128,8 +127,10 @@ extern void _tui_draw_widget_to_cells(TUI *tui, const WIDGET *widget,
int height_begin, int height_end,
int *child_width, int *childHeight);
-extern bool widget_eqauls(const WIDGET *restrict left, const WIDGET *restrict right);
-extern bool widget_array_eqauls(const WIDGET_ARRAY *restrict left, const WIDGET_ARRAY * restrict right);
+extern bool tui_widget_eqauls(const WIDGET *restrict left,
+ const WIDGET *restrict right);
+extern bool tui_widget_array_eqauls(const WIDGET_ARRAY *restrict left,
+ const WIDGET_ARRAY *restrict right);
extern void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder, int fps);
@@ -147,8 +148,8 @@ extern BUTTON_METADATA *_tui_make_button_metadata(WIDGET *restrict child,
extern void _tui_delete_button(WIDGET *restrict button);
extern WIDGET *tui_make_column(WIDGET_ARRAY *restrict children);
-extern COLUMN_METADATA *
-_tui_make_column_metadata(WIDGET_ARRAY *restrict children);
+extern COLUMN_METADATA *_tui_make_column_metadata(
+ WIDGET_ARRAY *restrict children);
extern void _tui_delete_column(WIDGET *restrict column);
extern WIDGET *tui_make_row(WIDGET_ARRAY *restrict children);
@@ -164,6 +165,8 @@ extern void _tui_delete_box(WIDGET *restrict box);
extern WIDGET_ARRAY *tui_make_widget_array_raw(size_t size, ...);
extern void _tui_delete_widget_array(WIDGET_ARRAY *restrict widget_array);
-#define tui_make_widget_array(...) tui_make_widget_array_raw(sizeof((WIDGET* []) {__VA_ARGS__}) / sizeof(WIDGET*), __VA_ARGS__)
+#define tui_make_widget_array(...) \
+ tui_make_widget_array_raw( \
+ sizeof((WIDGET *[]){__VA_ARGS__}) / sizeof(WIDGET *), __VA_ARGS__)
#endif