Move cache color to an Entry struct
So that more values can be added sensibly.
This commit is contained in:
		
							parent
							
								
									be6052e9a9
								
							
						
					
					
						commit
						93e841b29e
					
				
							
								
								
									
										21
									
								
								cache.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								cache.c
									
									
									
									
									
								
							| @ -36,11 +36,13 @@ | |||||||
| struct Node { | struct Node { | ||||||
| 	uint id; | 	uint id; | ||||||
| 	char *key; | 	char *key; | ||||||
| 	enum Color color; | 	struct Entry entry; | ||||||
| 	struct Node *prev; | 	struct Node *prev; | ||||||
| 	struct Node *next; | 	struct Node *next; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | static const struct Entry DefaultEntry = { .color = Default }; | ||||||
|  | 
 | ||||||
| static uint gen; | static uint gen; | ||||||
| static struct Node *head; | static struct Node *head; | ||||||
| static struct Node *tail; | static struct Node *tail; | ||||||
| @ -50,7 +52,7 @@ static struct Node *alloc(uint id, const char *key) { | |||||||
| 	if (!node) err(EX_OSERR, "calloc"); | 	if (!node) err(EX_OSERR, "calloc"); | ||||||
| 	node->id = id; | 	node->id = id; | ||||||
| 	node->key = strdup(key); | 	node->key = strdup(key); | ||||||
| 	node->color = Default; | 	node->entry = DefaultEntry; | ||||||
| 	if (!node->key) err(EX_OSERR, "strdup"); | 	if (!node->key) err(EX_OSERR, "strdup"); | ||||||
| 	return node; | 	return node; | ||||||
| } | } | ||||||
| @ -105,13 +107,13 @@ static struct Node *insert(bool touch, uint id, const char *key) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void cacheInsert(bool touch, uint id, const char *key) { | struct Entry *cacheInsert(bool touch, uint id, const char *key) { | ||||||
| 	insert(touch, id, key); | 	return &insert(touch, id, key)->entry; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void cacheInsertColor(bool touch, uint id, const char *key, enum Color color) { | const struct Entry *cacheGet(uint id, const char *key) { | ||||||
| 	struct Node *node = insert(touch, id, key); | 	struct Node *node = find(id, key); | ||||||
| 	if (color != Default) node->color = color; | 	return (node ? &node->entry : &DefaultEntry); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void cacheReplace(bool touch, const char *old, const char *new) { | void cacheReplace(bool touch, const char *old, const char *new) { | ||||||
| @ -126,11 +128,6 @@ void cacheReplace(bool touch, const char *old, const char *new) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| enum Color cacheColor(uint id, const char *key) { |  | ||||||
| 	struct Node *node = find(id, key); |  | ||||||
| 	return (node ? node->color : Default); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) { | const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) { | ||||||
| 	size_t len = strlen(prefix); | 	size_t len = strlen(prefix); | ||||||
| 	if (curs->gen != gen) curs->node = NULL; | 	if (curs->gen != gen) curs->node = NULL; | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								chat.h
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								chat.h
									
									
									
									
									
								
							| @ -396,14 +396,16 @@ int bufferReflow( | |||||||
| 	struct Buffer *buffer, int cols, enum Heat thresh, size_t tail | 	struct Buffer *buffer, int cols, enum Heat thresh, size_t tail | ||||||
| ); | ); | ||||||
| 
 | 
 | ||||||
|  | struct Entry { | ||||||
|  | 	enum Color color; | ||||||
|  | }; | ||||||
| struct Cursor { | struct Cursor { | ||||||
| 	uint gen; | 	uint gen; | ||||||
| 	struct Node *node; | 	struct Node *node; | ||||||
| }; | }; | ||||||
| void cacheInsert(bool touch, uint id, const char *key); | struct Entry *cacheInsert(bool touch, uint id, const char *key); | ||||||
| void cacheInsertColor(bool touch, uint id, const char *key, enum Color color); | const struct Entry *cacheGet(uint id, const char *key); | ||||||
| void cacheReplace(bool touch, const char *old, const char *new); | void cacheReplace(bool touch, const char *old, const char *new); | ||||||
| enum Color cacheColor(uint id, const char *key); |  | ||||||
| const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix); | const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix); | ||||||
| const char *cacheSearch(struct Cursor *curs, uint id, const char *substr); | const char *cacheSearch(struct Cursor *curs, uint id, const char *substr); | ||||||
| uint cacheID(struct Cursor *curs, const char *key); | uint cacheID(struct Cursor *curs, const char *key); | ||||||
|  | |||||||
| @ -139,7 +139,7 @@ static void commandMsg(uint id, char *params) { | |||||||
| 	char *nick = strsep(¶ms, " "); | 	char *nick = strsep(¶ms, " "); | ||||||
| 	uint msg = idFor(nick); | 	uint msg = idFor(nick); | ||||||
| 	if (idColors[msg] == Default) { | 	if (idColors[msg] == Default) { | ||||||
| 		idColors[msg] = cacheColor(id, nick); | 		idColors[msg] = cacheGet(id, nick)->color; | ||||||
| 	} | 	} | ||||||
| 	if (params) { | 	if (params) { | ||||||
| 		splitMessage("PRIVMSG", msg, params); | 		splitMessage("PRIVMSG", msg, params); | ||||||
| @ -380,7 +380,7 @@ static void commandQuery(uint id, char *params) { | |||||||
| 	if (!params) return; | 	if (!params) return; | ||||||
| 	uint query = idFor(params); | 	uint query = idFor(params); | ||||||
| 	if (idColors[query] == Default) { | 	if (idColors[query] == Default) { | ||||||
| 		idColors[query] = cacheColor(id, params); | 		idColors[query] = cacheGet(id, params)->color; | ||||||
| 	} | 	} | ||||||
| 	windowShow(windowFor(query)); | 	windowShow(windowFor(query)); | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										39
									
								
								handle.c
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								handle.c
									
									
									
									
									
								
							| @ -372,13 +372,13 @@ static void handleJoin(struct Message *msg) { | |||||||
| 			set(&self.host, msg->host); | 			set(&self.host, msg->host); | ||||||
| 		} | 		} | ||||||
| 		idColors[id] = hash(msg->params[0]); | 		idColors[id] = hash(msg->params[0]); | ||||||
| 		cacheInsertColor(true, None, msg->params[0], idColors[id]); | 		cacheInsert(true, None, msg->params[0])->color = idColors[id]; | ||||||
| 		if (replies[ReplyJoin]) { | 		if (replies[ReplyJoin]) { | ||||||
| 			windowShow(windowFor(id)); | 			windowShow(windowFor(id)); | ||||||
| 			replies[ReplyJoin]--; | 			replies[ReplyJoin]--; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	cacheInsertColor(true, id, msg->nick, hash(msg->user)); | 	cacheInsert(true, id, msg->nick)->color = hash(msg->user); | ||||||
| 	if (msg->params[2] && !strcasecmp(msg->params[2], msg->nick)) { | 	if (msg->params[2] && !strcasecmp(msg->params[2], msg->nick)) { | ||||||
| 		msg->params[2] = NULL; | 		msg->params[2] = NULL; | ||||||
| 	} | 	} | ||||||
| @ -432,14 +432,14 @@ static void handleKick(struct Message *msg) { | |||||||
| 	require(msg, true, 2); | 	require(msg, true, 2); | ||||||
| 	uint id = idFor(msg->params[0]); | 	uint id = idFor(msg->params[0]); | ||||||
| 	bool kicked = !strcmp(msg->params[1], self.nick); | 	bool kicked = !strcmp(msg->params[1], self.nick); | ||||||
| 	cacheInsertColor(true, id, msg->nick, hash(msg->user)); | 	cacheInsert(true, id, msg->nick)->color = hash(msg->user); | ||||||
| 	urlScan(id, msg->nick, msg->params[2]); | 	urlScan(id, msg->nick, msg->params[2]); | ||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		id, (kicked ? Hot : Cold), tagTime(msg), | 		id, (kicked ? Hot : Cold), tagTime(msg), | ||||||
| 		"%s\3%02d%s\17\tkicks \3%02d%s\3 out of \3%02d%s\3%s%s", | 		"%s\3%02d%s\17\tkicks \3%02d%s\3 out of \3%02d%s\3%s%s", | ||||||
| 		(kicked ? "\26" : ""), | 		(kicked ? "\26" : ""), | ||||||
| 		hash(msg->user), msg->nick, | 		hash(msg->user), msg->nick, | ||||||
| 		cacheColor(id, msg->params[1]), msg->params[1], | 		cacheGet(id, msg->params[1])->color, msg->params[1], | ||||||
| 		hash(msg->params[0]), msg->params[0], | 		hash(msg->params[0]), msg->params[0], | ||||||
| 		(msg->params[2] ? ": " : ""), (msg->params[2] ?: "") | 		(msg->params[2] ? ": " : ""), (msg->params[2] ?: "") | ||||||
| 	); | 	); | ||||||
| @ -555,7 +555,7 @@ static void handleErrorUserOnChannel(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		id, Warm, tagTime(msg), | 		id, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3 is already in \3%02d%s\3", | 		"\3%02d%s\3 is already in \3%02d%s\3", | ||||||
| 		cacheColor(id, msg->params[1]), msg->params[1], | 		cacheGet(id, msg->params[1])->color, msg->params[1], | ||||||
| 		hash(msg->params[2]), msg->params[2] | 		hash(msg->params[2]), msg->params[2] | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| @ -571,7 +571,8 @@ static void handleReplyNames(struct Message *msg) { | |||||||
| 		char *nick = &prefixes[strspn(prefixes, network.prefixes)]; | 		char *nick = &prefixes[strspn(prefixes, network.prefixes)]; | ||||||
| 		char *user = strsep(&name, "@"); | 		char *user = strsep(&name, "@"); | ||||||
| 		enum Color color = (user ? hash(user) : Default); | 		enum Color color = (user ? hash(user) : Default); | ||||||
| 		cacheInsertColor(false, id, nick, color); | 		struct Entry *entry = cacheInsert(false, id, nick); | ||||||
|  | 		if (user) entry->color = color; | ||||||
| 		if (!replies[ReplyNames] && !replies[ReplyNamesAuto]) continue; | 		if (!replies[ReplyNames] && !replies[ReplyNamesAuto]) continue; | ||||||
| 		ptr = seprintf( | 		ptr = seprintf( | ||||||
| 			ptr, end, "%s\3%02d%s\3", (ptr > buf ? ", " : ""), color, prefixes | 			ptr, end, "%s\3%02d%s\3", (ptr > buf ? ", " : ""), color, prefixes | ||||||
| @ -897,7 +898,7 @@ static void handleMode(struct Message *msg) { | |||||||
| 				id, Cold, tagTime(msg), | 				id, Cold, tagTime(msg), | ||||||
| 				"\3%02d%s\3\t%s \3%02d%c%s\3 %s%s in \3%02d%s\3", | 				"\3%02d%s\3\t%s \3%02d%c%s\3 %s%s in \3%02d%s\3", | ||||||
| 				hash(msg->user), msg->nick, verb, | 				hash(msg->user), msg->nick, verb, | ||||||
| 				cacheColor(id, nick), prefix, nick, | 				cacheGet(id, nick)->color, prefix, nick, | ||||||
| 				mode, name, hash(msg->params[0]), msg->params[0] | 				mode, name, hash(msg->params[0]), msg->params[0] | ||||||
| 			); | 			); | ||||||
| 			logFormat( | 			logFormat( | ||||||
| @ -1035,7 +1036,7 @@ static void handleReplyBanList(struct Message *msg) { | |||||||
| 			id, Warm, tagTime(msg), | 			id, Warm, tagTime(msg), | ||||||
| 			"Banned from \3%02d%s\3 since %s by \3%02d%s\3: %s", | 			"Banned from \3%02d%s\3 since %s by \3%02d%s\3: %s", | ||||||
| 			hash(msg->params[1]), msg->params[1], | 			hash(msg->params[1]), msg->params[1], | ||||||
| 			since, cacheColor(id, msg->params[3]), msg->params[3], | 			since, cacheGet(id, msg->params[3])->color, msg->params[3], | ||||||
| 			msg->params[2] | 			msg->params[2] | ||||||
| 		); | 		); | ||||||
| 	} else { | 	} else { | ||||||
| @ -1058,7 +1059,7 @@ static void onList(const char *list, struct Message *msg) { | |||||||
| 			id, Warm, tagTime(msg), | 			id, Warm, tagTime(msg), | ||||||
| 			"On the \3%02d%s\3 %s list since %s by \3%02d%s\3: %s", | 			"On the \3%02d%s\3 %s list since %s by \3%02d%s\3: %s", | ||||||
| 			hash(msg->params[1]), msg->params[1], list, | 			hash(msg->params[1]), msg->params[1], list, | ||||||
| 			since, cacheColor(id, msg->params[3]), msg->params[3], | 			since, cacheGet(id, msg->params[3])->color, msg->params[3], | ||||||
| 			msg->params[2] | 			msg->params[2] | ||||||
| 		); | 		); | ||||||
| 	} else { | 	} else { | ||||||
| @ -1091,7 +1092,7 @@ static void handleReplyList(struct Message *msg) { | |||||||
| 
 | 
 | ||||||
| static void handleReplyWhoisUser(struct Message *msg) { | static void handleReplyWhoisUser(struct Message *msg) { | ||||||
| 	require(msg, false, 6); | 	require(msg, false, 6); | ||||||
| 	cacheInsertColor(true, Network, msg->params[1], hash(msg->params[2])); | 	cacheInsert(true, Network, msg->params[1])->color = hash(msg->params[2]); | ||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\tis %s!%s@%s (%s\17)", | 		"\3%02d%s\3\tis %s!%s@%s (%s\17)", | ||||||
| @ -1106,7 +1107,7 @@ static void handleReplyWhoisServer(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\t%s connected to %s (%s)", | 		"\3%02d%s\3\t%s connected to %s (%s)", | ||||||
| 		cacheColor(Network, msg->params[1]), msg->params[1], | 		cacheGet(Network, msg->params[1])->color, msg->params[1], | ||||||
| 		(replies[ReplyWhowas] ? "was" : "is"), msg->params[2], msg->params[3] | 		(replies[ReplyWhowas] ? "was" : "is"), msg->params[2], msg->params[3] | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| @ -1130,7 +1131,7 @@ static void handleReplyWhoisIdle(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\tis idle for %lu %s%s%s%s", | 		"\3%02d%s\3\tis idle for %lu %s%s%s%s", | ||||||
| 		cacheColor(Network, msg->params[1]), msg->params[1], | 		cacheGet(Network, msg->params[1])->color, msg->params[1], | ||||||
| 		idle, unit, (idle != 1 ? "s" : ""), | 		idle, unit, (idle != 1 ? "s" : ""), | ||||||
| 		(msg->params[3] ? ", signed on " : ""), (msg->params[3] ? signon : "") | 		(msg->params[3] ? ", signed on " : ""), (msg->params[3] ? signon : "") | ||||||
| 	); | 	); | ||||||
| @ -1152,7 +1153,7 @@ static void handleReplyWhoisChannels(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\tis in %s", | 		"\3%02d%s\3\tis in %s", | ||||||
| 		cacheColor(Network, msg->params[1]), msg->params[1], buf | 		cacheGet(Network, msg->params[1])->color, msg->params[1], buf | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -1166,7 +1167,7 @@ static void handleReplyWhoisGeneric(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\t%s%s%s", | 		"\3%02d%s\3\t%s%s%s", | ||||||
| 		cacheColor(Network, msg->params[1]), msg->params[1], | 		cacheGet(Network, msg->params[1])->color, msg->params[1], | ||||||
| 		msg->params[2], (msg->params[3] ? " " : ""), (msg->params[3] ?: "") | 		msg->params[2], (msg->params[3] ? " " : ""), (msg->params[3] ?: "") | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| @ -1180,7 +1181,7 @@ static void handleReplyEndOfWhois(struct Message *msg) { | |||||||
| 
 | 
 | ||||||
| static void handleReplyWhowasUser(struct Message *msg) { | static void handleReplyWhowasUser(struct Message *msg) { | ||||||
| 	require(msg, false, 6); | 	require(msg, false, 6); | ||||||
| 	cacheInsertColor(true, Network, msg->params[1], hash(msg->params[2])); | 	cacheInsert(true, Network, msg->params[1])->color = hash(msg->params[2]); | ||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		Network, Warm, tagTime(msg), | 		Network, Warm, tagTime(msg), | ||||||
| 		"\3%02d%s\3\twas %s!%s@%s (%s)", | 		"\3%02d%s\3\twas %s!%s@%s (%s)", | ||||||
| @ -1200,7 +1201,7 @@ static void handleReplyAway(struct Message *msg) { | |||||||
| 	require(msg, false, 3); | 	require(msg, false, 3); | ||||||
| 	// Might be part of a WHOIS response.
 | 	// Might be part of a WHOIS response.
 | ||||||
| 	uint id; | 	uint id; | ||||||
| 	if (cacheColor(Network, msg->params[1]) != Default) { | 	if (cacheGet(Network, msg->params[1])->color != Default) { | ||||||
| 		id = Network; | 		id = Network; | ||||||
| 	} else { | 	} else { | ||||||
| 		id = idFor(msg->params[1]); | 		id = idFor(msg->params[1]); | ||||||
| @ -1208,7 +1209,7 @@ static void handleReplyAway(struct Message *msg) { | |||||||
| 	uiFormat( | 	uiFormat( | ||||||
| 		id, (id == Network ? Warm : Cold), tagTime(msg), | 		id, (id == Network ? Warm : Cold), tagTime(msg), | ||||||
| 		"\3%02d%s\3\tis away: %s", | 		"\3%02d%s\3\tis away: %s", | ||||||
| 		cacheColor(id, msg->params[1]), msg->params[1], msg->params[2] | 		cacheGet(id, msg->params[1])->color, msg->params[1], msg->params[2] | ||||||
| 	); | 	); | ||||||
| 	logFormat( | 	logFormat( | ||||||
| 		id, tagTime(msg), "%s is away: %s", | 		id, tagTime(msg), "%s is away: %s", | ||||||
| @ -1279,7 +1280,7 @@ static char *colorMentions(char *ptr, char *end, uint id, const char *msg) { | |||||||
| 
 | 
 | ||||||
| 		size_t len = strcspn(msg, ",:<> "); | 		size_t len = strcspn(msg, ",:<> "); | ||||||
| 		char *p = seprintf(ptr, end, "%.*s", (int)len, msg); | 		char *p = seprintf(ptr, end, "%.*s", (int)len, msg); | ||||||
| 		enum Color color = cacheColor(id, ptr); | 		enum Color color = cacheGet(id, ptr)->color; | ||||||
| 		if (color != Default) { | 		if (color != Default) { | ||||||
| 			ptr = seprintf(ptr, end, "\3%02d%.*s\3", color, (int)len, msg); | 			ptr = seprintf(ptr, end, "\3%02d%.*s\3", color, (int)len, msg); | ||||||
| 		} else { | 		} else { | ||||||
| @ -1319,7 +1320,7 @@ static void handlePrivmsg(struct Message *msg) { | |||||||
| 	heat = filterCheck(heat, id, msg); | 	heat = filterCheck(heat, id, msg); | ||||||
| 	if (heat > Warm && !mine && !query) highlight = true; | 	if (heat > Warm && !mine && !query) highlight = true; | ||||||
| 	if (!notice && !mine && heat > Ice) { | 	if (!notice && !mine && heat > Ice) { | ||||||
| 		cacheInsertColor(true, id, msg->nick, hash(msg->user)); | 		cacheInsert(true, id, msg->nick)->color = hash(msg->user); | ||||||
| 	} | 	} | ||||||
| 	if (heat > Ice) urlScan(id, msg->nick, msg->params[1]); | 	if (heat > Ice) urlScan(id, msg->nick, msg->params[1]); | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								window.c
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								window.c
									
									
									
									
									
								
							| @ -118,7 +118,7 @@ uint windowFor(uint id) { | |||||||
| 		window->thresh = windowThreshold; | 		window->thresh = windowThreshold; | ||||||
| 	} | 	} | ||||||
| 	window->buffer = bufferAlloc(); | 	window->buffer = bufferAlloc(); | ||||||
| 	cacheInsertColor(false, None, idNames[id], idColors[id]); | 	cacheInsert(false, None, idNames[id])->color = idColors[id]; | ||||||
| 
 | 
 | ||||||
| 	return windowPush(window); | 	return windowPush(window); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user