#include "mr4th/mr4th_base.h"
#include <ncurses.h>

#include "mr4th/mr4th_base.c"

int main(){
  // initializes ncurses
  initscr();
  
  // settings
  noecho();
  cbreak();
  
  // get size
  int window_x = 0;
  int window_y = 0;
  getmaxyx(stdscr, window_y, window_x);
  
  // program
  int y_counter = 0;
  for (;;){
    char c = getch();
    
    // (27 '^[' ESC)
    if (c == 27){
      printw("ESC ");
      
      // read escape bytes
      nodelay(stdscr, 1);
      int bcount = 0;
      char b[10];
      for (;;){
        char bx = getch();
        if (bx == ERR){
          break;
        }
        if (bcount < 10){
          b[bcount] = bx;
        }
        bcount += 1;
      }
      nodelay(stdscr, 0);
      
      // print escape
      int bcount_clamped = Min(10, bcount);
      for (int i = 0; i < bcount_clamped; i += 1){
        printw("%u ", b[i]);
      }
      if (bcount_clamped < bcount){
        printw("... (%u more), ", bcount - bcount_clamped);
      }
      printw("\n");
    }
    
    else{
      printw("'%c' [%u]\n", c, c);
    }
    
    // go to top
    y_counter += 1;
    if (y_counter >= window_y){
      wmove(stdscr, 0, 0);
      y_counter = 1;
    }
    
    refresh();
  }
  
  // end ncurses
  endwin();
  
  return(0);
}
