<commit_before>#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>

#include "common.h"

#define C(x) "\x1b[38;5;"#x"m"
#define MAXINPUT 200

void draw_full(void);

struct winsize w;

//extern int chan_count;
//extern int current_chan;
//extern struct channel chan_list[];

extern channel rirc;
extern channel *ccur;
channel *rircp = &rirc;

void
resize()
{
	ioctl(0, TIOCGWINSZ, &w);
	draw_full();
}

void
draw_full()
{
	int i;
	printf("\033[H\033[J");/* Clear */
	draw_chans();
	printf("\033[2;1H\033[2K"C(239));
	for (i = 0; i < w.ws_col; i++) /* Upper separator */
		printf("―");
	draw_chat();
	printf("\033[%d;1H\033[2K", w.ws_row-1);
	for (i = 0; i < w.ws_col; i++) /* Lower separator */
		printf("―");
	printf("\033[%d;1H\033[2K >>> "C(250), w.ws_row); /* bottom bar */
	/* TODO: redraw input bar */
}

int
nick_col(char *nick)
{
	/* temp */
	return 1;
}

void
draw_chat()
{
	printf("\033[s"); /* save cursor location */
	printf("\033[3;1H");

	channel *c = ccur;
	int tw = w.ws_col - c->nick_pad - 15;
	line *l = c->chat;
	while (l->len > 0) {
		int n = l->len;
		printf(C(239)" %02d:%02d  "C(%d)"%*s%s "C(239)"~"C(250)" ",
				l->time_h, l->time_m, nick_col(l->from),
				(int)(c->nick_pad - strlen(l->from)), "", l->from);
		char *end = l->text + l->len - 2;
		if (n > tw) {
			char *ptr1 = l->text;
			for (;;) {
				char *ptr2 = ptr1 + tw;
				if (ptr2 > end)
					ptr2 = end;
				else
					while (*ptr2 != ' ' && ptr2 > ptr1)
						ptr2--;
				if (ptr2 == ptr1)
					ptr2 += tw;
				while (ptr1 <= ptr2)
					putchar(*ptr1++);
				if (ptr2 < end)
					printf("\n                  "C(239)"~"C(250)" ");
				else
					break;
				ptr1 = ptr2 + 1;
				while (*ptr1 == ' ')
					ptr1++;
			}
			printf("\n");
		} else {
			printf("%s\n", l->text);
		}
		l++;
	}
	printf("\033[u"); /* restore cursor location */
}

void
draw_chans()
{
	printf("\033[s"); /* save cursor location */
	printf("\033[H\033[K");
	int len, width = 0;
	channel *c = rircp;
	do {
		len = strlen(c->name);
		if (width + len + 4 < w.ws_col) {
			int color;
			if (c == ccur)
				color = 255;
			else
				color = c->active ? 245 : 239;
			printf("\033[38;5;%dm  %s  ", color, c->name);
			width += len + 4;
			c = c->next;
		}
		else break;
	} while (c != rircp);
	printf("\033[u"); /* restore cursor location */
}

void
print_line(char *text, int ptr1, int ptr2)
{
	int p;
	printf(C(239)"\033[%d;6H\033[K"C(250), w.ws_row);
	for (p = 0; p < ptr1; p++)
		putchar(text[p]);
	for (p = ptr2; p < MAXINPUT-1; p++)
		putchar(text[p]);
	printf("\033[%d;%dH", w.ws_row, ptr1+6);
}<commit_msg>Improved chat drawing<commit_after>#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>

#include "common.h"

#define C(x) "\x1b[38;5;"#x"m"
#define MAXINPUT 200

void draw_full(void);

struct winsize w;

extern channel rirc;
extern channel *ccur;
channel *rircp = &rirc;

void
resize()
{
	ioctl(0, TIOCGWINSZ, &w);

	printf("\033[H\033[J");/* Clear */

	int i;
	printf("\033[2;1H\033[2K"C(239));
	for (i = 0; i < w.ws_col; i++) /* Upper separator */
		printf("―");

	printf("\033[%d;1H\033[2K", w.ws_row-1);
	for (i = 0; i < w.ws_col; i++) /* Lower separator */
		printf("―");
	printf("\033[%d;1H\033[2K >>> "C(250), w.ws_row); /* bottom bar */
	draw_full();
}

void
draw_full()
{
	draw_chans();
	draw_chat();
	/* TODO: redraw input bar */
}

int
nick_col(char *nick)
{
	/* temp */
	return 1;
}

void
draw_chat()
{
	printf("\x1b[s"); /* save cursor location */
	line *l = ccur->chat;
	int i, h, tw = w.ws_col - ccur->nick_pad - 15;
	for (i = 3, h = w.ws_row - 1; i < h; i++)
	{
		printf("\x1b[%d;1H\x1b[2K", i);
		if (l->len > 0) {
			int n = l->len;
			printf(C(239)" %02d:%02d  "C(%d)"%*s%s "C(239)"~"C(250)" ",
					l->time_h, l->time_m, nick_col(l->from),
					(int)(ccur->nick_pad - strlen(l->from)), "", l->from);
			char *end = l->text + l->len - 2;
			if (n > tw) {
				char *ptr1 = l->text;
				for (;;) {
					char *ptr2 = ptr1 + tw;
					if (ptr2 > end)
						ptr2 = end;
					else
						while (*ptr2 != ' ' && ptr2 > ptr1)
							ptr2--;
					if (ptr2 == ptr1)
						ptr2 += tw;
					while (ptr1 <= ptr2)
						putchar(*ptr1++);
					if (ptr2 < end)
						printf("\x1b[%d;%dH\x1b[2K"C(239)"~"C(250)" ", ++i, ccur->nick_pad + 10);
					else
						break;
					ptr1 = ptr2 + 1;
					while (*ptr1 == ' ')
						ptr1++;
				}
			} else {
				printf("%s", l->text);
			}
		}
		l++;
	}
	printf("\x1b[u"); /* restore cursor location */
}

void
draw_chans()
{
	printf("\033[s"); /* save cursor location */
	printf("\033[H\033[K");
	int len, width = 0;
	channel *c = rircp;
	do {
		len = strlen(c->name);
		if (width + len + 4 < w.ws_col) {
			int color;
			if (c == ccur)
				color = 255;
			else
				color = c->active ? 245 : 239;
			printf("\033[38;5;%dm  %s  ", color, c->name);
			width += len + 4;
			c = c->next;
		}
		else break;
	} while (c != rircp);
	printf("\033[u"); /* restore cursor location */
}

void
draw_input(char *text, int ptr1, int ptr2)
{
	int p;
	printf(C(239)"\033[%d;6H\033[K"C(250), w.ws_row);
	for (p = 0; p < ptr1; p++)
		putchar(text[p]);
	for (p = ptr2; p < MAXINPUT-1; p++)
		putchar(text[p]);
	printf("\033[%d;%dH", w.ws_row, ptr1+6);
}
{'fraction_non_alphanumeric': 0.23161978661493696, 'fraction_numerical': 0.06168768186226964, 'mean_word_length': 2.7662527392257124, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 60, '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': '993604', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2292, 'n_words': 522}