#include <iostream>
#include <termios.h>
#include <unistd.h>

constexpr const char* splash = "osint\n";
constexpr const char* divi = "---- ---- ---- ----\n";

termios orig_termios;

void spalsh(){
	std::cout << divi << splash << divi << "\n";
}

void disableRawMode() {
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}

void enableRawMode() {
    tcgetattr(STDIN_FILENO, &orig_termios);
    atexit(disableRawMode);

    termios raw = orig_termios;
    raw.c_lflag &= ~(ICANON | ECHO);   // turn off canonical mode & echo
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}

int main() {
    enableRawMode();

    std::cout << "Press arrow keys (q to quit)\n";

    while (true) {
        char c;
        read(STDIN_FILENO, &c, 1);

        if (c == 'q')
            break;

        if (c == '\x1b') { // ESC
            char seq[2];
            if (read(STDIN_FILENO, &seq[0], 1) == 0) continue;
            if (read(STDIN_FILENO, &seq[1], 1) == 0) continue;

            if (seq[0] == '[') {
                switch (seq[1]) {
                    case 'A': std::cout << "Up\n"; break;
                    case 'B': std::cout << "Down\n"; break;
                    case 'C': std::cout << "Right\n"; break;
                    case 'D': std::cout << "Left\n"; break;
                }
            }
        }
    }
}

