From 7daa4fd0784fd7c0420d46eede052b8f75455f21 Mon Sep 17 00:00:00 2001 From: Jarkko Toivanen Date: Fri, 17 Jan 2025 04:53:27 +0200 Subject: [PATCH] We has arrowy handly --- main.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 8b80427..72e5ede 100644 --- a/main.c +++ b/main.c @@ -1,17 +1,60 @@ #include +#include +#include +#include #define clear() printf("\033[H\033[J") #define gotoxy(x,y) printf("\033[%d;%dH", (y), (x)) -void drawWin(int fontH); +int getch(); +void enableRawMode(); +void disableRawMode(); +void drawWin(int fonth); +int askHeight(); + +struct termios orig_termios; int main() { + int fonth = askHeight(); clear(); - drawWin(12); + drawWin(fonth); + + enableRawMode(); + + char c; + while(1) { + c = getch(); + // Q to exit + if (c == 'q') { + break; + } + // Handle arrows and stuff + if (c == '[') { + c = getch(); + if (c == 'A') { + printf("^"); + continue; + } + if (c == 'B') { + printf("v"); + continue; + } + if (c == 'C') { + printf(">"); + continue; + } + if (c == 'D') { + printf("<"); + continue; + } + } + } + + disableRawMode(); return 0; } -void drawWin(int fontH) { +void drawWin(int fonth) { clear(); printf("Kaari - Binarier font editor\r\n"); printf("+--------+\r\n"); @@ -19,7 +62,7 @@ void drawWin(int fontH) { int cstart=32; int cperline=16; int cmax=126; - for (int y=0; y 16) { + printf("This should be 8 - 16.\r\n"); + return askHeight(); + } + return fonth; +} + +void disableRawMode() { + tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios); +} + +void enableRawMode() { + tcgetattr(STDIN_FILENO, &orig_termios); + atexit(disableRawMode); + + struct termios raw = orig_termios; + raw.c_lflag &= ~(ECHO | ICANON); + + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); +} + +int getch() { + struct termios oldt, newt; + int ch; + + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + ch = getchar(); + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + + return ch; +}