<commit_before>/* niii - ncurses interface to ii similar to iii
 * by <NAME>ter <NAME>   >:3
 * see LICENSE for copyright information
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curses.h>
#include <locale.h>

#define NICKLEN 12
#define ESCSYMB "/CLOSE"

#define USAGE "usage: niii [-v] [-h] <path/to/ircdir/network/channel/>"

#ifndef PATH_MAX
#define PATH_MAX 2048
#endif

#ifndef LINE_MAX
#define LINE_MAX 1024
#endif

/* color pair identifiers - up to 8 - see man COLOR_PAIR */
enum { DATETIME, NICK, SEPARATOR, MESG, WINP, };

static WINDOW *winp, *wout;
static FILE *in, *out;
static char ircdir[PATH_MAX];
static int winrows, wincols;
static bool running = true;

/* print the given line formated and colored
 * add the line to the last line of wout
 * older lines should scroll up automatically */
static void printline(const char *date, const char *time, const char *nick, const char *mesg) {
    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(SEPARATOR));
    wprintw(wout, "\n%s %s ", date, time);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(SEPARATOR));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(NICK));
    wprintw(wout, "%*.*s ", NICKLEN, NICKLEN, nick);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(NICK));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(SEPARATOR));
    wprintw(wout, "| ");
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(SEPARATOR));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(MESG));
    wprintw(wout, "%s", mesg);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(MESG));
}

static void readout(void) {
    char *date, *time, *nick, *mesg;
    char *rawline = NULL;
    size_t len = 0;

    while (getline(&rawline, &len, out) != -1) {
        date = strtok(rawline, " \t");
        time = strtok(NULL, " \t");
        nick = strtok(NULL, " \t");
        mesg = strtok(NULL, "\n");

        if (nick[0] == '<') { /* nick[strlen(nick) - 1] == '>' */
            ++nick;
            nick[strlen(nick) - 1] = '\0';
        }

        printline(date, time, nick, mesg);
    }

    wrefresh(wout);
    wrefresh(winp); /* leave cursor on input window */
}

static void updatewout(void) {
    rewind(out);
    delwin(wout);
    wout = newwin(winrows - 1, wincols, 0, 0);
    scrollok(wout, true);
    readout();
}

static void updatewinp(void) {
    char *prompt = NULL;
    prompt = strrchr(ircdir, '/') + 1;
    delwin(winp);
    winp = newwin(1, wincols, winrows - 1, 0);
    if (has_colors() == TRUE) wattron(winp, COLOR_PAIR(WINP)|A_BOLD);
    wprintw(winp, "[%s] ", prompt);
    if (has_colors() == TRUE) wattroff(winp, COLOR_PAIR(WINP)|A_BOLD);
    wrefresh(winp);
}

static void updateall(void) {
    updatewout();
    updatewinp();
}

static void redrawall() {
    getmaxyx(stdscr, winrows, wincols);
    updateall();
    redrawwin(wout);
    redrawwin(winp);
}

static void createwins(void) {
    /* start curses mode - do not buffer input */
    initscr();
    /* start color support and set up color pairs */
    if (has_colors() == TRUE) {
        start_color();
        init_pair(DATETIME,  COLOR_CYAN,  COLOR_BLACK);
        init_pair(NICK,      COLOR_GREEN, COLOR_BLACK);
        init_pair(SEPARATOR, COLOR_CYAN,  COLOR_BLACK);
        init_pair(MESG,      COLOR_WHITE, COLOR_BLACK);
        init_pair(WINP,      COLOR_GREEN, COLOR_BLACK);
    }
    /* create the windows and add contents */
    redrawall();
}

static void sendmesg(const char* mesg) {
    if (mesg == NULL) return;
    fprintf(in, "%s\n", mesg);
    fflush(in);
}

static void readinput(void) {
    char *input;
    if ((input = malloc(LINE_MAX)) == NULL)
        err(EXIT_FAILURE, "failed to allocate space for input");<commit_msg>use calloc inplace of malloc<commit_after>/* niii - ncurses interface to ii similar to iii
 * by <NAME> <NAME>   >:3
 * see LICENSE for copyright information
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curses.h>
#include <locale.h>

#define NICKLEN 12
#define ESCSYMB "/CLOSE"

#define USAGE "usage: niii [-v] [-h] <path/to/ircdir/network/channel/>"

#ifndef PATH_MAX
#define PATH_MAX 2048
#endif

#ifndef LINE_MAX
#define LINE_MAX 1024
#endif

/* color pair identifiers - up to 8 - see man COLOR_PAIR */
enum { DATETIME, NICK, SEPARATOR, MESG, WINP, };

static WINDOW *winp, *wout;
static FILE *in, *out;
static char ircdir[PATH_MAX];
static int winrows, wincols;
static bool running = true;

/* print the given line formated and colored
 * add the line to the last line of wout
 * older lines should scroll up automatically */
static void printline(const char *date, const char *time, const char *nick, const char *mesg) {
    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(SEPARATOR));
    wprintw(wout, "\n%s %s ", date, time);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(SEPARATOR));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(NICK));
    wprintw(wout, "%*.*s ", NICKLEN, NICKLEN, nick);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(NICK));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(SEPARATOR));
    wprintw(wout, "| ");
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(SEPARATOR));

    if (has_colors() == TRUE) wattron(wout, COLOR_PAIR(MESG));
    wprintw(wout, "%s", mesg);
    if (has_colors() == TRUE) wattroff(wout, COLOR_PAIR(MESG));
}

static void readout(void) {
    char *date, *time, *nick, *mesg;
    char *rawline = NULL;
    size_t len = 0;

    while (getline(&rawline, &len, out) != -1) {
        date = strtok(rawline, " \t");
        time = strtok(NULL, " \t");
        nick = strtok(NULL, " \t");
        mesg = strtok(NULL, "\n");

        if (nick[0] == '<') { /* nick[strlen(nick) - 1] == '>' */
            ++nick;
            nick[strlen(nick) - 1] = '\0';
        }
        // FIXME: handle /me|ACTION , cleanup ^AACTIONmsg^A

        printline(date, time, nick, mesg);
    }

    wrefresh(wout);
    wrefresh(winp); /* leave cursor on input window */
}

static void updatewout(void) {
    rewind(out);
    delwin(wout);
    wout = newwin(winrows - 1, wincols, 0, 0);
    scrollok(wout, true);
    readout();
}

static void updatewinp(void) {
    char *prompt = NULL;
    prompt = strrchr(ircdir, '/') + 1;
    delwin(winp);
    winp = newwin(1, wincols, winrows - 1, 0);
    if (has_colors() == TRUE) wattron(winp, COLOR_PAIR(WINP)|A_BOLD);
    wprintw(winp, "[%s] ", prompt);
    if (has_colors() == TRUE) wattroff(winp, COLOR_PAIR(WINP)|A_BOLD);
    wrefresh(winp);
}

static void updateall(void) {
    updatewout();
    updatewinp();
}

static void redrawall() {
    getmaxyx(stdscr, winrows, wincols);
    updateall();
    redrawwin(wout);
    redrawwin(winp);
}

static void createwins(void) {
    /* start curses mode - do not buffer input */
    initscr();
    /* start color support and set up color pairs */
    if (has_colors() == TRUE) {
        start_color();
        init_pair(DATETIME,  COLOR_CYAN,  COLOR_BLACK);
        init_pair(NICK,      COLOR_GREEN, COLOR_BLACK);
        init_pair(SEPARATOR, COLOR_CYAN,  COLOR_BLACK);
        init_pair(MESG,      COLOR_WHITE, COLOR_BLACK);
        init_pair(WINP,      COLOR_GREEN, COLOR_BLACK);
    }
    /* create the windows and add contents */
    redrawall();
}

static void sendmesg(const char* mesg) {
    if (mesg == NULL) return;
    fprintf(in, "%s\n", mesg);
    fflush(in);
}

static void readinput(void) {
    char *input;
    if ((input = calloc(1, LINE_MAX)) == NULL)
        err(EXIT_FAILURE, "failed to allocate space for input");
{'fraction_non_alphanumeric': 0.15666029318036967, 'fraction_numerical': 0.00650095602294455, 'mean_word_length': 3.282751091703057, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '2504753', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2965, 'n_words': 755}