We has arrowy handly

This commit is contained in:
Jarkko Toivanen 2025-01-17 04:53:27 +02:00
parent 01a02c98ba
commit 7daa4fd078
Signed by: jt
GPG key ID: 9151B109B73ECAD5

94
main.c
View file

@ -1,17 +1,60 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<termios.h>
#define clear() printf("\033[H\033[J") #define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x)) #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 main() {
int fonth = askHeight();
clear(); 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; return 0;
} }
void drawWin(int fontH) { void drawWin(int fonth) {
clear(); clear();
printf("Kaari - Binarier font editor\r\n"); printf("Kaari - Binarier font editor\r\n");
printf("+--------+\r\n"); printf("+--------+\r\n");
@ -19,7 +62,7 @@ void drawWin(int fontH) {
int cstart=32; int cstart=32;
int cperline=16; int cperline=16;
int cmax=126; int cmax=126;
for (int y=0; y<fontH; y++) { for (int y=0; y<fonth; y++) {
printf("| | "); printf("| | ");
for (int c=y*cperline+cstart; c<(y+1)*cperline+cstart && c<=cmax; c++) { for (int c=y*cperline+cstart; c<(y+1)*cperline+cstart && c<=cmax; c++) {
printf("%c ", c); printf("%c ", c);
@ -27,7 +70,48 @@ void drawWin(int fontH) {
printf("\r\n"); printf("\r\n");
} }
printf("+--------+\r\n"); printf("+--------+\r\nQ:Quit");
return; return;
} }
int askHeight() {
printf("Enter desired font height: ");
int fonth;
scanf("%d", &fonth);
if (fonth < 8 || fonth > 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;
}