<commit_before>#include <stdlib.h>
#include <ncurses.h>
#include "io-loop.h"

thrd_t create_io_loop(Rico rico) {
    thrd_t thr;
    thrd_create(&thr, (int(*)(void*))&io_loop, rico);
    return thr;
}

int io_loop(Rico rico) {
    DisplaySize currsz = {0}, cmpsz;
    Key key;

    initscr();
    raw();
    noecho();

    while (rico->running) {
        getmaxyx(stdscr, cmpsz.rows, cmpsz.cols);

        if (currsz.rows != cmpsz.rows || currsz.cols != cmpsz.cols) {
            currsz = cmpsz;
            redraw(currsz);
        }

        if ((key = key_scan()).code > 0) {
            if (key.code == 0x3) {  // ^C
                rico->running = false;
            }

            printw("%016llp:", key.code);
            printw("%s", keyname(key.seq.k0));
            key.code >>= 8;

            while (key.code > 0) {
                printw("%s", keyname(key.seq.k0));
                key.code >>= 8;
            }

            printw("\n ");
        }
    }

    echo();
    noraw();
    endwin();

    return 0;
}

Key key_scan() {
    Key key = {0};
    int ch, i = 0;

    timeout(0);

    while (i < 8 && (ERR != (ch = getch()))) {
        key.code |= (uint64_t)ch << 8*i++;
    }

    timeout(-1);

    return key;
}

void redraw(DisplaySize sz) {
    move(0, 0);
    clrtoeol();
    mvprintw(0, 2, "*keyscan*");
    mvprintw(0, sz.cols-9, "| Rico α");
    move(1, 1);
    refresh();
}<commit_msg>clear screen when redrawing<commit_after>#include <stdlib.h>
#include <ncurses.h>
#include "io-loop.h"

thrd_t create_io_loop(Rico rico) {
    thrd_t thr;
    thrd_create(&thr, (int(*)(void*))&io_loop, rico);
    return thr;
}

int io_loop(Rico rico) {
    DisplaySize currsz = {0}, cmpsz;
    Key key;

    initscr();
    raw();
    noecho();

    while (rico->running) {
        getmaxyx(stdscr, cmpsz.rows, cmpsz.cols);

        if (currsz.rows != cmpsz.rows || currsz.cols != cmpsz.cols) {
            currsz = cmpsz;
            redraw(currsz);
        }

        if ((key = key_scan()).code > 0) {
            if (key.code == 0x3) {  // ^C
                rico->running = false;
            }

            printw("%016llp:", key.code);
            printw("%s", keyname(key.seq.k0));
            key.code >>= 8;

            while (key.code > 0) {
                printw("%s", keyname(key.seq.k0));
                key.code >>= 8;
            }

            printw("\n ");
        }
    }

    echo();
    noraw();
    endwin();

    return 0;
}

Key key_scan() {
    Key key = {0};
    int ch, i = 0;

    timeout(0);

    while (i < 8 && (ERR != (ch = getch()))) {
        key.code |= (uint64_t)ch << 8*i++;
    }

    timeout(-1);

    return key;
}

void redraw(DisplaySize sz) {
    move(0, 0);
    clrtoeol();
    mvprintw(0, 2, "*keyscan*");
    mvprintw(0, sz.cols-9, "| Rico α");

    for (int row = 1; row <= sz.rows; row++) {
        move(row, 0);
        clrtoeol();
    }

    move(1, 1);
    refresh();
}
{'fraction_non_alphanumeric': 0.18296422487223168, 'fraction_numerical': 0.020442930153321975, 'mean_word_length': 1.701011959521619, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7078354', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1119, 'n_words': 229}