kaari/main.c

157 lines
2.6 KiB
C

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<termios.h>
#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int getch();
void enableRawMode();
void disableRawMode();
void drawWin(int fonth);
int askHeight();
void limitCurrChar();
char currchar = 'A';
char xpos=0, ypos=0;
struct termios orig_termios;
int main() {
int fonth = askHeight();
clear();
drawWin(fonth);
enableRawMode();
char c;
limitCurrChar();
while(1) {
c = getch();
// Q to exit
if (c == 'q') {
break;
}
// Handle arrows and stuff
if (c == '[') {
c = getch();
if (c == '5') { // PageUP
currchar++;
limitCurrChar();
continue;
}
if (c == '6') { // PageDOWN
currchar--;
limitCurrChar();
continue;
}
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) {
clear();
printf("Kaari - Binarier font editor\r\n");
printf("+--------+\r\n");
int cstart=32;
int cperline=16;
int cmax=126;
for (int y=0; y<fonth; y++) {
printf("| | ");
for (int c=y*cperline+cstart; c<(y+1)*cperline+cstart && c<=cmax; c++) {
printf("%c ", c);
}
printf("\r\n");
}
printf("+--------+\r\nQ:Quit PgUP/DWN:Switch char\r\n");
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;
}
void limitCurrChar() {
if (currchar < 32) {
currchar = 32;
} else if (currchar > 126) {
currchar = 126;
}
gotoxy(12+currchar%16*2, 3+(currchar-32)/16);
printf("\033[7m%c", currchar);
gotoxy(12+(currchar-1)%16*2, 3+(currchar-1-32)/16);
printf("\033[0m%c", currchar-1);
gotoxy(12+(currchar+1)%16*2, 3+(currchar+1-32)/16);
printf("\033[0m%c", currchar+1);
gotoxy(12, 2);
printf("\033[0mEditing \"%c\" / %d / 0x%X", currchar, currchar, currchar);
gotoxy(0, 21);
return;
}