terminal functions

This commit is contained in:
Timerix22 2023-05-19 23:45:56 +06:00
parent 9febbe1de0
commit bb66d841f3
6 changed files with 56 additions and 24 deletions

2
cbuild

@ -1 +1 @@
Subproject commit 9711d8fbb1bf947101f475c8cf918009a4d39110 Subproject commit 5e23ef8156a85a981d60152990bde53d6e8eefe9

View File

@ -42,8 +42,7 @@ case "$TASK" in
# -flto=auto is needed to multithreaded copilation # -flto=auto is needed to multithreaded copilation
# -fuse-linker-plugin is required to use static libs with lto, it strips away all # -fuse-linker-plugin is required to use static libs with lto, it strips away all
# -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code # -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects \ C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
-fdata-sections -ffunction-sections -Wl,--gc-sections"
CPP_ARGS="$C_ARGS" CPP_ARGS="$C_ARGS"
LINKER_ARGS="$CPP_ARGS" LINKER_ARGS="$CPP_ARGS"
PRE_TASK_SCRIPT=tasks/pre_build.sh PRE_TASK_SCRIPT=tasks/pre_build.sh
@ -65,7 +64,7 @@ case "$TASK" in
;; ;;
# executes $EXEC_FILE with valgrind memory checker # executes $EXEC_FILE with valgrind memory checker
valgrind) valgrind)
VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" VALGRIND_ARGS="-s --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all"
TASK_SCRIPT=cbuild/default_tasks/valgrind.sh TASK_SCRIPT=cbuild/default_tasks/valgrind.sh
;; ;;
# generates profiling info # generates profiling info

2
kerep

@ -1 +1 @@
Subproject commit ededa875d6d1b5947e4516588489b1c8c64e940f Subproject commit d7136055d92e90070b947f1662b3fa6981eecdc0

View File

@ -20,39 +20,54 @@ Maybe tryReadFile(char* filePath){
i32 main(const i32 argc, const char* const* argv){ i32 main(const i32 argc, const char* const* argv){
#if _WIN32 || _WIN64 #if _WIN32 || _WIN64
if(!SetConsoleOutputCP(CP_UTF8)){ if(!SetConsoleOutputCP(CP_UTF8)){
kprintf("\e[93mcan't set console codepage to utf8"); kprintf("\e[91mcan't set console codepage to utf8");
return 1;
} }
#endif #endif
if(setlocale(LC_ALL, "C.UTF8")==0){ // doesn't work on windows if(setlocale(LC_ALL, "C.UTF8")==0){ // doesn't work on windows
kprintf("\e[93msetlocale failed! (%i)\n", errno); kprintf("\e[91msetlocale failed! (%i)\n", errno);
return 1;
} }
// term_cursorHide(true);
// term_clear();
// kerep type system
kt_beginInit(); kt_beginInit();
kt_initKerepTypes(); kt_initKerepTypes();
kt_initScolteTypes(); kt_initScolteTypes();
kt_endInit(); kt_endInit();
// print command line arguments
kprintf("\e[37margs:"); kprintf("\e[37margs:");
for(i32 i=0; i<argc; i++) for(i32 i=0; i<argc; i++)
kprintf(" %s", argv[i]); kprintf(" %s", argv[i]);
kprintf("\n"); kprintf("\n");
// create file
/* char* filePath= argc>1 ? argv[argc-1] : "new_file.txt"; /* char* filePath= argc>1 ? argv[argc-1] : "new_file.txt";
tryLast(tryReadFile(filePath), _m_text); tryLast(tryReadFile(filePath), _m_text);
char* text=(char*)_m_text.value.VoidPtr; char* text=(char*)_m_text.value.VoidPtr;
fputs(text,stdout); */ fputs(text,stdout); */
// render ui
Renderer* renderer=Renderer_create(); Renderer* renderer=Renderer_create();
TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text")); TextBlock* testTextBlock=TextBlock_create(string_fromCptr("some example text"));
Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64); Autoarr(UIElement_Ptr)* grid_content=Autoarr_create(UIElement_Ptr, 1, 64);
Autoarr_add(grid_content, (UIElement_Ptr)testTextBlock); Autoarr_add(grid_content, (UIElement_Ptr)testTextBlock);
Grid* mainGrid=Grid_create(1,1, Autoarr_toArray(grid_content)); Grid* mainGrid=Grid_create(1,1, Autoarr_toArray(grid_content));
Autoarr_free(grid_content, true);
tryLast(UIElement_draw(renderer, (UIElement_Ptr)mainGrid, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_); tryLast(UIElement_draw(renderer, (UIElement_Ptr)mainGrid, ((DrawingArea){.x=10, .y=4, .h=7, .w=24})),_);
tryLast(Renderer_drawFrame(renderer),_2); tryLast(Renderer_drawFrame(renderer),_2);
// free ui memory
UIElement_destroy((UIElement_Ptr)mainGrid); UIElement_destroy((UIElement_Ptr)mainGrid);
Renderer_destroy(renderer); Renderer_destroy(renderer);
// TODO signal( SIGWINCH, redraw );
// exit
kt_free(); kt_free();
term_resetColors();
term_cursorHide(false);
return 0; return 0;
} }

View File

@ -15,6 +15,13 @@ kt_define(TerminalSize, NULL, __TerminalSize_toString);
TerminalSize term_default_size={.cols=80, .rows=16}; TerminalSize term_default_size={.cols=80, .rows=16};
int getenv_int(const char* var_name){
char* str=getenv(var_name);
if(str==NULL)
return -1;
return strtol(str, NULL, 0);
}
bool term_getSize(TerminalSize* out) { bool term_getSize(TerminalSize* out) {
#if defined(_WIN64) || defined(_WIN32) #if defined(_WIN64) || defined(_WIN32)
// helps when STD_OUT is redirected to a file // helps when STD_OUT is redirected to a file
@ -27,22 +34,30 @@ bool term_getSize(TerminalSize* out) {
.rows=consoleInfo.srWindow.Bottom-consoleInfo.srWindow.Top+1 .rows=consoleInfo.srWindow.Bottom-consoleInfo.srWindow.Top+1
}; };
#else #else
struct winsize w={0,0,0,0}; struct winsize ws={0,0,0,0};
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)!=0) // tries to get terminal size from stdin, stdout, stderr
return false; if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)==0 ||
*out=(TerminalSize){ ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)==0 ||
.cols=w.ws_col, ioctl(STDERR_FILENO, TIOCGWINSZ, &ws)==0 ){
.rows=w.ws_row out->cols=ws.ws_col;
}; out->rows=ws.ws_row;
}
// tries to get size from environtent variables
else{
out->cols=getenv_int("COLUMNS");
out->rows=getenv_int("LINES");
}
#endif #endif
return true; return out->cols > 0 && out->cols < 720 && out->rows > 0 && out->rows < 480;
} }
/*
Most of escape sequences can be found at:
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
*/
void term_moveCursor(u16 row, u16 column){ void term_resetCursor() { printf("\e[H"); }
printf("\e[%u;%uf",row,column); void term_resetColors() { printf("\e[0m"); }
} void term_clear() { printf("\e[0m\e[H\e[2J"); }
void term_cursorMove(u16 row, u16 column) { printf("\e[%u;%uH",row,column); }
void term_clear() { void term_cursorHide(bool hide) { printf( hide ? "\e[?25l" : "\e[?25h"); }
printf("\e[2j");
}

View File

@ -8,8 +8,11 @@ extern "C" {
#include "../../kerep/src/kprint/kprint_format.h" #include "../../kerep/src/kprint/kprint_format.h"
#include "../encoding/encoding.h" #include "../encoding/encoding.h"
void term_moveCursor(u16 row, u16 column); void term_resetCursor();
void term_resetColors();
void term_clear(); void term_clear();
void term_cursorMove(u16 row, u16 column);
void term_cursorHide(bool hide);
STRUCT(TerminalSize, STRUCT(TerminalSize,
u16 cols; u16 cols;