about summary refs log tree commit diff
path: root/complete.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-02-05 19:47:49 -0500
committerJune McEnroe <june@causal.agency>2023-02-05 19:47:49 -0500
commit1b4297e6095fea7258f99150fb79b49a01b5519b (patch)
tree3feaa576b3e001d243caa49bc20011c8791b25f6 /complete.c
parentFix what went wrong, part 1 (diff)
downloadcatgirl-1b4297e6095fea7258f99150fb79b49a01b5519b.tar.gz
catgirl-1b4297e6095fea7258f99150fb79b49a01b5519b.zip
Fix what went wrong, part 2
Diffstat (limited to 'complete.c')
-rw-r--r--complete.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/complete.c b/complete.c
index 6d7756f..4536401 100644
--- a/complete.c
+++ b/complete.c
@@ -35,6 +35,7 @@
 struct Node {
 	uint id;
 	char *str;
+	enum Color color;
 	struct Node *prev;
 	struct Node *next;
 };
@@ -43,12 +44,13 @@ static uint gen;
 static struct Node *head;
 static struct Node *tail;
 
-static struct Node *alloc(uint id, const char *str) {
+static struct Node *alloc(uint id, const char *str, enum Color color) {
 	struct Node *node = calloc(1, sizeof(*node));
 	if (!node) err(EX_OSERR, "calloc");
 	node->id = id;
 	node->str = strdup(str);
 	if (!node->str) err(EX_OSERR, "strdup");
+	node->color = color;
 	return node;
 }
 
@@ -87,13 +89,23 @@ static struct Node *find(uint id, const char *str) {
 	return NULL;
 }
 
-void completePush(uint id, const char *str) {
-	if (!find(id, str)) append(alloc(id, str));
+void completePush(uint id, const char *str, enum Color color) {
+	struct Node *node = find(id, str);
+	if (node) {
+		if (color != Default) node->color = color;
+	} else {
+		append(alloc(id, str, color));
+	}
 }
 
-void completePull(uint id, const char *str) {
+void completePull(uint id, const char *str, enum Color color) {
 	struct Node *node = find(id, str);
-	prepend(node ? detach(node) : alloc(id, str));
+	if (node) {
+		if (color != Default) node->color = color;
+		prepend(detach(node));
+	} else {
+		prepend(alloc(id, str, color));
+	}
 }
 
 void completeReplace(const char *old, const char *new) {
@@ -121,6 +133,11 @@ void completeRemove(uint id, const char *str) {
 	gen++;
 }
 
+enum Color completeColor(uint id, const char *str) {
+	struct Node *node = find(id, str);
+	return (node ? node->color : Default);
+}
+
 const char *completePrefix(struct Cursor *curs, uint id, const char *prefix) {
 	size_t len = strlen(prefix);
 	if (curs->gen != gen) curs->node = NULL;