kaari/main.c

144 lines
2.4 KiB
C
Raw Permalink Normal View History

2025-01-17 03:02:50 +02:00
#include<stdio.h>
2025-01-17 04:53:27 +02:00
#include<stdlib.h>
#include<unistd.h>
#include<termios.h>
2025-01-17 03:02:50 +02:00
#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
2025-01-17 12:22:40 +02:00
char fonth = 8;
2025-01-17 04:53:27 +02:00
int getch();
void enableRawMode();
void disableRawMode();
2025-01-17 12:22:40 +02:00
void drawWin();
2025-01-17 04:53:27 +02:00
int askHeight();
2025-01-17 12:22:40 +02:00
void resetCurPos();
2025-01-17 06:09:31 +02:00
char currchar = 'A';
char xpos=0, ypos=0;
2025-01-17 04:53:27 +02:00
struct termios orig_termios;
2025-01-17 03:02:50 +02:00
int main() {
2025-01-17 13:10:02 +02:00
clear();
2025-01-17 04:53:27 +02:00
enableRawMode();
char c;
while(1) {
2025-01-17 12:22:40 +02:00
drawWin();
2025-01-17 04:53:27 +02:00
c = getch();
// Q to exit
if (c == 'q') {
break;
}
// Handle arrows and stuff
if (c == '[') {
c = getch();
2025-01-17 12:22:40 +02:00
if (c == '6') { // PageUP
if (currchar < 126) { currchar++; }
2025-01-17 06:09:31 +02:00
continue;
}
2025-01-17 12:22:40 +02:00
if (c == '5') { // PageDOWN
if (currchar >32) { currchar--; }
2025-01-17 06:09:31 +02:00
continue;
}
2025-01-17 04:53:27 +02:00
if (c == 'A') {
2025-01-17 12:22:40 +02:00
if (ypos > 0) { ypos--; }
2025-01-17 04:53:27 +02:00
continue;
}
if (c == 'B') {
2025-01-17 12:22:40 +02:00
if (ypos < fonth-1) { ypos++; }
2025-01-17 04:53:27 +02:00
continue;
}
if (c == 'C') {
2025-01-17 12:22:40 +02:00
if (xpos < 8-1) { xpos++; }
2025-01-17 04:53:27 +02:00
continue;
}
if (c == 'D') {
2025-01-17 12:22:40 +02:00
if (xpos > 0) { xpos--; }
2025-01-17 04:53:27 +02:00
continue;
}
}
}
2025-01-17 12:22:40 +02:00
2025-01-17 04:53:27 +02:00
disableRawMode();
2025-01-17 12:22:40 +02:00
clear();
2025-01-17 03:02:50 +02:00
return 0;
}
2025-01-17 12:22:40 +02:00
void drawWin() {
2025-01-17 13:10:02 +02:00
//clear();
gotoxy(1, 1);
2025-01-17 03:02:50 +02:00
printf("Kaari - Binarier font editor\r\n");
2025-01-17 13:10:02 +02:00
printf("+--------+ Char \"%c\" / %d / 0x%X \r\n", currchar, currchar, currchar);
2025-01-17 03:02:50 +02:00
int cstart=32;
int cperline=16;
int cmax=126;
2025-01-17 04:53:27 +02:00
for (int y=0; y<fonth; y++) {
2025-01-17 03:02:50 +02:00
printf("| | ");
for (int c=y*cperline+cstart; c<(y+1)*cperline+cstart && c<=cmax; c++) {
2025-01-17 12:22:40 +02:00
if (c == currchar) {
printf("\033[7m%c\033[0m ", currchar);
} else {
printf("%c ", c);
}
2025-01-17 03:02:50 +02:00
}
printf("\r\n");
}
2025-01-17 13:10:02 +02:00
printf("+--------+\r\nArrows:Move PgUP/DWN:Char Q:Quit");
2025-01-17 12:22:40 +02:00
resetCurPos();
2025-01-17 03:02:50 +02:00
return;
}
2025-01-17 04:53:27 +02:00
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;
}
2025-01-17 06:09:31 +02:00
2025-01-17 12:22:40 +02:00
void resetCurPos() {
gotoxy(2+xpos, 3+ypos);
2025-01-17 06:09:31 +02:00
return;
}