From 44fb1750ec97ff1132ea826cb8b146936681d957 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 18 Feb 2023 21:44:39 +0600 Subject: [PATCH] terminal_getSize --- src/Canvas.c | 8 ++++---- src/TextBlock.c | 10 +++++----- src/main.c | 8 +++++++- src/terminal.c | 23 +++++++++++++++++++++++ src/terminal.h | 21 +++++++++++++++++++++ src/tui.c | 4 ++-- src/tui.h | 6 +++--- tasks/pre_build.sh | 2 +- 8 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/terminal.c create mode 100644 src/terminal.h diff --git a/src/Canvas.c b/src/Canvas.c index d236c43..ffb0b4b 100644 --- a/src/Canvas.c +++ b/src/Canvas.c @@ -7,7 +7,7 @@ void Canvas_freeMembers(void* _self){ Autoarr_freeWithoutMembers(self->children, true); } -void Canvas_draw(UIElement* _self, Rect place){ +void Canvas_draw(UIElement* _self, DrawingArea place){ Canvas* self=(Canvas*)_self; Autoarr_foreach(self->children, ch, ({ UIElement_draw((UIElement*)ch,place); @@ -18,9 +18,9 @@ void Canvas_draw(UIElement* _self, Rect place){ kt_define(Canvas, Canvas_freeMembers, NULL); Canvas* Canvas_create(){ - Canvas* canvas = malloc(sizeof(Canvas)); - canvas->base = __UIElement_createDefault(ktid_name(Canvas), Canvas_draw); - canvas->children = Autoarr_create(Pointer, 32, 64); + Canvas* canvas=malloc(sizeof(Canvas)); + canvas->base=__UIElement_createDefault(ktid_name(Canvas), Canvas_draw); + canvas->children=Autoarr_create(Pointer, 32, 64); return canvas; } diff --git a/src/TextBlock.c b/src/TextBlock.c index b0c15cd..63a01df 100644 --- a/src/TextBlock.c +++ b/src/TextBlock.c @@ -6,7 +6,7 @@ void TextBlock_freeMembers(void* _self){ free(self->text.ptr); } -void TextBlock_draw(UIElement* _self, Rect place){ +void TextBlock_draw(UIElement* _self, DrawingArea place){ TextBlock* self=(TextBlock*)_self; fputs(self->text.ptr, stdout); } @@ -14,8 +14,8 @@ void TextBlock_draw(UIElement* _self, Rect place){ kt_define(TextBlock, TextBlock_freeMembers, NULL); TextBlock* TextBlock_create(string text){ - TextBlock* TextBlock=malloc(sizeof(TextBlock)); - TextBlock->base=__UIElement_createDefault(ktid_name(TextBlock), TextBlock_draw); - TextBlock->text=string_copy(text); - return TextBlock; + TextBlock* textBlock=malloc(sizeof(TextBlock)); + textBlock->base=__UIElement_createDefault(ktid_name(TextBlock), TextBlock_draw); + textBlock->text=string_copy(text); + return textBlock; } diff --git a/src/main.c b/src/main.c index a48cbd7..364a8a8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "../kerep/src/Filesystem/filesystem.h" #include "tui.h" +#include Maybe tryReadFile(char* filePath){ if(!file_exists(filePath)) @@ -31,8 +32,13 @@ i32 main(const i32 argc, const char* const* argv){ Canvas* mainCanvas=Canvas_create(); TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text")); Canvas_addChild(mainCanvas, (UIElement*)testTextBlock); - UIElement_draw(mainCanvas, ((Rect){.x=-1, .y=-1, .h=-1, .w=-1})); + UIElement_draw(mainCanvas, ((DrawingArea){.x=-1, .y=-1, .h=-1, .w=-1})); UIElement_destroy((UIElement*)mainCanvas); + + TerminalSize tsize=terminal_getSize(); + kprintf("rows: %u culumns: %u\n", tsize.rows, tsize.cols); + fflush(stdout); + kt_free(); return 0; } diff --git a/src/terminal.c b/src/terminal.c new file mode 100644 index 0000000..e22035d --- /dev/null +++ b/src/terminal.c @@ -0,0 +1,23 @@ +#include "terminal.h" +#include +#include +#include + +void terminal_moveCursor(u16 row, u16 column){ + printf("\e[%u;%uf",row,column); +} + +void terminal_clear() { + printf("\e[2j"); +} + +TerminalSize terminal_getSize(){ + struct winsize w={0,0,0,0}; + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)!=0) + kprintf("\e[93mterminal_getSize() error\n"); + TerminalSize tsize={ + .cols=w.ws_col, + .rows=w.ws_row + }; + return tsize; +} diff --git a/src/terminal.h b/src/terminal.h new file mode 100644 index 0000000..5e462b8 --- /dev/null +++ b/src/terminal.h @@ -0,0 +1,21 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../kerep/src/base/base.h" + +void terminal_moveCursor(u16 row, u16 column); +void terminal_clear(); + +STRUCT(TerminalSize, + u16 rows; + u16 cols; +) + +TerminalSize terminal_getSize(); + +#if __cplusplus +} +#endif diff --git a/src/tui.c b/src/tui.c index 58c5471..066fde0 100644 --- a/src/tui.c +++ b/src/tui.c @@ -1,10 +1,10 @@ #include "tui.h" -kt_define(Rect, NULL, NULL); +kt_define(DrawingArea, NULL, NULL); kt_define(UIBorderParams, NULL, NULL); void kt_initScolteTypes(){ - kt_register(Rect); + kt_register(DrawingArea); kt_register(UIBorderParams); kt_register(UIElement); kt_register(Canvas); diff --git a/src/tui.h b/src/tui.h index 8f247cb..0def569 100644 --- a/src/tui.h +++ b/src/tui.h @@ -8,6 +8,7 @@ extern "C" { #include "../kerep/src/String/string.h" #include "../kerep/src/kprint/kprint_colors.h" #include "../kerep/src/Array/Array.h" +#include "terminal.h" /// initializes type descriptors for this project's types @@ -42,8 +43,7 @@ PACKED_ENUM(UIBorderType, // Small structs // ////////////////////////////////////// -/// Rectangle -STRUCT(Rect, +STRUCT(DrawingArea, /* right-top corner */ u16 x; u16 y; u16 w; u16 h; @@ -62,7 +62,7 @@ STRUCT(UIBorderParams, typedef struct UIElement UIElement; typedef UIElement* UIElementPtr; -typedef void (*UIElement_draw_t)(UIElement* self,Rect place); +typedef void (*UIElement_draw_t)(UIElement* self,DrawingArea place); #define UIElement_stretch (u16)-1 diff --git a/tasks/pre_build.sh b/tasks/pre_build.sh index 60439dd..126dffe 100644 --- a/tasks/pre_build.sh +++ b/tasks/pre_build.sh @@ -13,5 +13,5 @@ if [ ! -f "$OBJDIR/libs/kerep.a" ] || [ -f .rebuild_kerep.tmp ]; then cp kerep/bin/kerep.a $OBJDIR/libs/ myprint "${GREEN}copied ${CYAN}kerep.a" - rm .rebuild_kerep.tmp + rm -f .rebuild_kerep.tmp fi