summary refs log tree commit diff
path: root/ui.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-13 13:49:03 -0400
committerJune McEnroe <june@causal.agency>2018-08-13 13:49:03 -0400
commit2ae5b6b9ab2a528425c1c5265b04bb01a5246e29 (patch)
tree499faf3fd2401818005aed8c3af56206b224830b /ui.c
parentFactor out input param and add tagFind (diff)
downloadcatgirl-2ae5b6b9ab2a528425c1c5265b04bb01a5246e29.tar.gz
catgirl-2ae5b6b9ab2a528425c1c5265b04bb01a5246e29.zip
Add /query, /part and /close
Closing a channel before parting it is a bit weird, but if I send a PART
on /close, it would get reopened again to show the part message.
Diffstat (limited to '')
-rw-r--r--ui.c81
1 files changed, 59 insertions, 22 deletions
diff --git a/ui.c b/ui.c
index 559f7b7..6fcb75e 100644
--- a/ui.c
+++ b/ui.c
@@ -70,11 +70,7 @@ static short pair8(short pair) {
 	return (pair & 0x70) >> 1 | (pair & 0x07);
 }
 
-static const int LOG_LINES   = 256;
-static const int TOPIC_COLS  = 512;
-static const int INPUT_COLS  = 512;
-
-static struct View {
+struct View {
 	struct Tag tag;
 	WINDOW *topic;
 	WINDOW *log;
@@ -82,16 +78,35 @@ static struct View {
 	bool mark;
 	struct View *prev;
 	struct View *next;
-} *viewHead, *viewTail;
+};
+
+static struct {
+	struct View *head;
+	struct View *tail;
+	struct View *tags[TAGS_LEN];
+} views;
 
 static void viewAppend(struct View *view) {
-	if (viewTail) viewTail->next = view;
-	view->prev = viewTail;
+	if (views.tail) views.tail->next = view;
+	view->prev = views.tail;
 	view->next = NULL;
-	viewTail = view;
-	if (!viewHead) viewHead = view;
+	if (!views.head) views.head = view;
+	views.tail = view;
+	views.tags[view->tag.id] = view;
+}
+
+static void viewRemove(struct View *view) {
+	if (view->prev) view->prev->next = view->next;
+	if (view->next) view->next->prev = view->prev;
+	if (views.head == view) views.head = view->next;
+	if (views.tail == view) views.tail = view->prev;
+	views.tags[view->tag.id] = NULL;
 }
 
+static const int LOG_LINES   = 256;
+static const int TOPIC_COLS  = 512;
+static const int INPUT_COLS  = 512;
+
 static int logHeight(const struct View *view) {
 	return LINES - (view->topic ? 2 : 0) - 2;
 }
@@ -105,15 +120,8 @@ static int lastCol(void) {
 	return COLS - 1;
 }
 
-static struct {
-	bool hide;
-	WINDOW *input;
-	struct View *view;
-	struct View *tags[TAGS_LEN];
-} ui;
-
 static struct View *viewTag(struct Tag tag) {
-	struct View *view = ui.tags[tag.id];
+	struct View *view = views.tags[tag.id];
 	if (view) return view;
 
 	view = calloc(1, sizeof(*view));
@@ -127,10 +135,22 @@ static struct View *viewTag(struct Tag tag) {
 	view->scroll = LOG_LINES;
 
 	viewAppend(view);
-	ui.tags[tag.id] = view;
 	return view;
 }
 
+static void viewClose(struct View *view) {
+	viewRemove(view);
+	if (view->topic) delwin(view->topic);
+	delwin(view->log);
+	free(view);
+}
+
+static struct {
+	bool hide;
+	struct View *view;
+	WINDOW *input;
+} ui;
+
 void uiHide(void) {
 	ui.hide = true;
 	termMode(TERM_FOCUS, false);
@@ -170,7 +190,7 @@ void uiExit(void) {
 }
 
 static void uiResize(void) {
-	for (struct View *view = viewHead; view; view = view->next) {
+	for (struct View *view = views.head; view; view = view->next) {
 		wresize(view->log, LOG_LINES, COLS);
 		wmove(view->log, lastLogLine(), lastCol());
 	}
@@ -215,19 +235,36 @@ static void uiView(struct View *view) {
 	ui.view = view;
 }
 
+static void uiClose(struct View *view) {
+	if (ui.view == view) {
+		if (view->next) {
+			uiView(view->next);
+		} else if (view->prev) {
+			uiView(view->prev);
+		} else {
+			return;
+		}
+	}
+	viewClose(view);
+}
+
 void uiViewTag(struct Tag tag) {
 	uiView(viewTag(tag));
 }
 
+void uiCloseTag(struct Tag tag) {
+	uiClose(viewTag(tag));
+}
+
 void uiViewNum(int num) {
 	if (num < 0) {
-		for (struct View *view = viewTail; view; view = view->prev) {
+		for (struct View *view = views.tail; view; view = view->prev) {
 			if (++num) continue;
 			uiView(view);
 			break;
 		}
 	} else {
-		for (struct View *view = viewHead; view; view = view->next) {
+		for (struct View *view = views.head; view; view = view->next) {
 			if (num--) continue;
 			uiView(view);
 			break;