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');
 		}
0'>2021-01-25Add protocol coverage to non-features listJune McEnroe I don't feel that 100% protocol extension support should at all be a goal. 2021-01-25Move mention of pledge(2) and unveil(2) to READMEJune McEnroe The manual is the wrong place to document platform support. 2021-01-23Preserve order of filters when removingJune McEnroe Filters should be kept in order since the first one that matches, wins. This lets highlights or ignores take priority over each other, if desired. 2021-01-23Fix /unignore commandJune McEnroe Yikes, copy-paste fail. 2021-01-23Remove use of "%n" to appease de RaadtJune McEnroe Sure, it was completely unnecessary, but I resent being told how to use snprintf. https://cvsweb.openbsd.org/src/lib/libc/stdio/vfprintf.c?rev=1.79&content-type=text/x-cvsweb-markup 2021-01-23Drop filesystem access iff possibleKlemens Nanni Log files and state save/restore both require read/write access to the filesystem, both during start and exit. If neither features are used, catgirl may run with "stdio tty". 2021-01-23Drop exec capability iff restrictedKlemens Nanni Nothing must be executed when running /copy, et al. 2021-01-23Drop network capability after ircConnect()Klemens Nanni catgirl has no reconnect feature and generally must not do anything but read/write from/to the connected socket which does not require "inet" or "dns" promises. 2021-01-23Call pledge(2) after unveil(2)Klemens Nanni Simplify logic, be more idiomatic and finalize by pledging after all unveiling is done by omitting the "unveil" promise and thereby not allowing further calls to it. 2021-01-23Separate kiosk mode from restrict modeJune McEnroe Restrict mode will focus on sandboxing, while kiosk will continue to restrict IRC access through a public kiosk. Kiosk mode without restrict mode allows execution of man 1 catgirl with /help, assuming external sandboxing. The /list and /part commands are also added to the list of disabled commands in kiosk mode, since they are pointless without access to /join. 2021-01-21Simplify windowUpdate loops and factor out windowTopJune McEnroe No idea why I made that more complicated than it had to be looping backwards. 2021-01-21Document that M-l shows timestampsJune McEnroe 2021-01-19Don't lose swapped window when navigating to current bufferJeremy O'Brien 2021-01-19Use Warm heat for manually inserted blank linesJune McEnroe Oops, forgot about these. 2021-01-18Add example tmux(1) configKlemens Nanni Spawn one catgirl per network and handle reconnects through tmux, no extra tool required. 2021-01-17Use Warm heat for blank linesJune McEnroe So they're not hidden by M-+. 2021-01-16Add -I highlight option and /highlightJune McEnroe