summary refs log tree commit diff
path: root/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'ui.c')
-rw-r--r--ui.c68
1 files changed, 51 insertions, 17 deletions
diff --git a/ui.c b/ui.c
index b764f84..12205cc 100644
--- a/ui.c
+++ b/ui.c
@@ -35,6 +35,9 @@
 
 #include "chat.h"
 
+// Annoying stuff from <term.h>:
+#undef lines
+
 #ifndef A_ITALIC
 #define A_ITALIC A_UNDERLINE
 #endif
@@ -43,16 +46,28 @@
 #define RIGHT (COLS - 1)
 #define WINDOW_LINES (LINES - 2)
 
-enum {
-	InputCols = 512,
-	PadLines = 512,
-};
-
 static WINDOW *status;
 static WINDOW *input;
 
+enum { BufferCap = 512 };
+struct Buffer {
+	time_t times[BufferCap];
+	char *lines[BufferCap];
+	size_t len;
+};
+static_assert(!(BufferCap & (BufferCap - 1)), "BufferCap is power of two");
+
+static void bufferPush(struct Buffer *buffer, time_t time, const char *line) {
+	size_t i = buffer->len++ % BufferCap;
+	free(buffer->lines[i]);
+	buffer->times[i] = time;
+	buffer->lines[i] = strdup(line);
+	if (!buffer->lines[i]) err(EX_OSERR, "strdup");
+}
+
 struct Window {
 	size_t id;
+	struct Buffer buffer;
 	WINDOW *pad;
 	enum Heat heat;
 	int unread;
@@ -89,17 +104,15 @@ static struct Window *windowFor(size_t id) {
 	for (window = windows.head; window; window = window->next) {
 		if (window->id == id) return window;
 	}
-	window = malloc(sizeof(*window));
+	window = calloc(1, sizeof(*window));
 	if (!window) err(EX_OSERR, "malloc");
 
 	window->id = id;
-	window->pad = newpad(PadLines, COLS);
-	window->heat = Cold;
-	window->unread = 0;
-	window->scroll = PadLines;
-	window->mark = true;
+	window->pad = newpad(BufferCap, COLS);
 	scrollok(window->pad, true);
-	wmove(window->pad, PadLines - 1, 0);
+	wmove(window->pad, BufferCap - 1, 0);
+	window->scroll = BufferCap;
+	window->mark = true;
 
 	windowAdd(window);
 	return window;
@@ -190,7 +203,7 @@ void uiInit(void) {
 
 	colorInit();
 	status = newwin(1, COLS, 0, 0);
-	input = newpad(1, InputCols);
+	input = newpad(1, 512);
 	keypad(input, true);
 	nodelay(input, true);
 	windows.active = windowFor(Network);
@@ -380,9 +393,11 @@ static void wordWrap(WINDOW *win, const char *str) {
 	}
 }
 
-void uiWrite(size_t id, enum Heat heat, const time_t *time, const char *str) {
-	(void)time;
+void uiWrite(size_t id, enum Heat heat, const time_t *src, const char *str) {
 	struct Window *window = windowFor(id);
+	time_t clock = (src ? *src : time(NULL));
+	bufferPush(&window->buffer, clock, str);
+
 	waddch(window->pad, '\n');
 	if (window->mark && heat > Cold) {
 		if (!window->unread++) {
@@ -406,6 +421,25 @@ void uiFormat(
 	uiWrite(id, heat, time, buf);
 }
 
+static void reflow(struct Window *window) {
+	werase(window->pad);
+	wmove(window->pad, BufferCap - 1, 0);
+	size_t len = window->buffer.len;
+	for (size_t i = (len > BufferCap ? len - BufferCap : 0); i < len; ++i) {
+		waddch(window->pad, '\n');
+		wordWrap(window->pad, window->buffer.lines[i % BufferCap]);
+	}
+}
+
+static void resize(void) {
+	// FIXME: Only reflow when COLS changes.
+	for (struct Window *window = windows.head; window; window = window->next) {
+		wresize(window->pad, BufferCap, COLS);
+		reflow(window);
+	}
+	statusUpdate();
+}
+
 static void inputAdd(struct Style *style, const char *str) {
 	size_t len;
 	while (*str) {
@@ -480,7 +514,7 @@ void uiShowNum(size_t num) {
 
 static void keyCode(int code) {
 	switch (code) {
-		break; case KEY_RESIZE:; // TODO
+		break; case KEY_RESIZE:  resize();
 		break; case KeyFocusIn:  unmark();
 		break; case KeyFocusOut: windows.active->mark = true;
 		break; case KeyPasteOn:; // TODO
@@ -490,7 +524,7 @@ static void keyCode(int code) {
 
 static void keyMeta(wchar_t ch) {
 	switch (ch) {
-		break; case L'm': uiWrite(windows.active->id, Cold, NULL, "");
+		break; case L'm': waddch(windows.active->pad, '\n');
 		break; default: {
 			if (ch >= L'0' && ch <= L'9') uiShowNum(ch - L'0');
 		}
> Such link rot. 2022-08-09Fix all copyright noticesJune McEnroe 2022-08-04Add Conversations With FriendsJune McEnroe The better of the two, but largely the same. I feel like these books are a bit too autobiographical, but I don't know if I'm allowed to accuse an author of that. My real problem is that I read these books as largely uncritical of their characters. They behave in nonsense ways, are mostly uncritical of their own behaviour, and don't really have arcs of growth or change. I suppose this book had a bit of one, but only in the last two chapters. 2022-07-30Add Normal PeopleJune McEnroe Unbearably straight. Eyerolls and sighs per page off the charts. Shout out to Joanna, I guess. I kinda like the lack of quotation marks though to be honest. After half of the Ruth Ozeki novel and now this, I need to get back to some genre fiction. 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe There is not that much distinct stuff here anymore. 2022-07-03Add The Bone Shard EmperorJune McEnroe Suffers a little bit from middle book but I really enjoyed it. Read it faster than the first one too, despite its length. 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe Mixed bag like most collections of short stories. Some of them are pretty good. The author of the worst written story also has the worst written bio. 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe Absolutely indiscriminately. 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe