terminal_getSize

This commit is contained in:
Timerix22 2023-02-18 21:44:39 +06:00
parent 54ba772395
commit 44fb1750ec
8 changed files with 66 additions and 16 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
#include "../kerep/src/Filesystem/filesystem.h"
#include "tui.h"
#include <unistd.h>
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;
}

23
src/terminal.c Normal file
View File

@ -0,0 +1,23 @@
#include "terminal.h"
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
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;
}

21
src/terminal.h Normal file
View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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