about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-09-13 16:16:11 -0400
committerJune McEnroe <june@causal.agency>2018-09-13 16:16:11 -0400
commit0d7854c0d48e3f9f3b0a3ea4b5c71cbd6b332df5 (patch)
treeaf3ee6ca206286f9528c9c0ded2be05a5dfea45b
parentFix len for format->split at end of string (diff)
downloadcatgirl-0d7854c0d48e3f9f3b0a3ea4b5c71cbd6b332df5.tar.gz
catgirl-0d7854c0d48e3f9f3b0a3ea4b5c71cbd6b332df5.zip
Move color selection to format.c
-rw-r--r--chat.h1
-rw-r--r--format.c25
-rw-r--r--handle.c54
3 files changed, 43 insertions, 37 deletions
diff --git a/chat.h b/chat.h
index b72e664..93d3ae9 100644
--- a/chat.h
+++ b/chat.h
@@ -93,6 +93,7 @@ struct Format {
 };
 void formatReset(struct Format *format);
 bool formatParse(struct Format *format, const wchar_t *split);
+enum IRCColor formatColor(const char *str);
 
 void handle(char *line);
 void input(struct Tag tag, char *line);
diff --git a/format.c b/format.c
index 8c2b0ed..58735c5 100644
--- a/format.c
+++ b/format.c
@@ -14,12 +14,33 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <wchar.h>
-#include <stdlib.h>
 #include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <wchar.h>
 
 #include "chat.h"
 
+// Adapted from <https://github.com/cbreeden/fxhash/blob/master/lib.rs>.
+static uint32_t hashChar(uint32_t hash, char ch) {
+	hash = (hash << 5) | (hash >> 27);
+	hash ^= ch;
+	hash *= 0x27220A95;
+	return hash;
+}
+
+enum IRCColor formatColor(const char *str) {
+	if (!str) return IRCDefault;
+	uint32_t hash = 0;
+	for (; str[0]; ++str) {
+		hash = hashChar(hash, str[0]);
+	}
+	while (IRCBlack == (hash & IRCLightGray)) {
+		hash = hashChar(hash, '\0');
+	}
+	return (hash & IRCLightGray);
+}
+
 void formatReset(struct Format *format) {
 	format->bold = false;
 	format->italic = false;
diff --git a/handle.c b/handle.c
index a34c864..938315b 100644
--- a/handle.c
+++ b/handle.c
@@ -17,7 +17,6 @@
 #include <ctype.h>
 #include <err.h>
 #include <stdarg.h>
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -25,25 +24,6 @@
 
 #include "chat.h"
 
-// Adapted from <https://github.com/cbreeden/fxhash/blob/master/lib.rs>.
-static uint32_t hashChar(uint32_t hash, char ch) {
-	hash = (hash << 5) | (hash >> 27);
-	hash ^= ch;
-	hash *= 0x27220A95;
-	return hash;
-}
-static int color(const char *str) {
-	if (!str) return IRCGray;
-	uint32_t hash = 0;
-	for (; str[0]; ++str) {
-		hash = hashChar(hash, str[0]);
-	}
-	while (IRCBlack == (hash & IRCLightGray)) {
-		hash = hashChar(hash, '\0');
-	}
-	return (hash & IRCLightGray);
-}
-
 static char *paramField(char **params) {
 	char *rest = *params;
 	if (rest[0] == ':') {
@@ -163,7 +143,7 @@ static void handleJoin(char *prefix, char *params) {
 	uiFmt(
 		tag, UICold,
 		"\3%d%s\3 arrives in \3%d%s\3",
-		color(user), nick, color(chan), chan
+		formatColor(user), nick, formatColor(chan), chan
 	);
 	logFmt(tag, NULL, "%s arrives in %s", nick, chan);
 }
@@ -184,14 +164,14 @@ static void handlePart(char *prefix, char *params) {
 		uiFmt(
 			tag, UICold,
 			"\3%d%s\3 leaves \3%d%s\3, \"%s\"",
-			color(user), nick, color(chan), chan, dequote(mesg)
+			formatColor(user), nick, formatColor(chan), chan, dequote(mesg)
 		);
 		logFmt(tag, NULL, "%s leaves %s, \"%s\"", nick, chan, dequote(mesg));
 	} else {
 		uiFmt(
 			tag, UICold,
 			"\3%d%s\3 leaves \3%d%s\3",
-			color(user), nick, color(chan), chan
+			formatColor(user), nick, formatColor(chan), chan
 		);
 		logFmt(tag, NULL, "%s leaves %s", nick, chan);
 	}
@@ -214,7 +194,9 @@ static void handleKick(char *prefix, char *params) {
 		uiFmt(
 			tag, (kicked ? UIHot : UICold),
 			"\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3, \"%s\"",
-			color(user), nick, color(kick), kick, color(chan), chan,
+			formatColor(user), nick,
+			formatColor(kick), kick,
+			formatColor(chan), chan,
 			dequote(mesg)
 		);
 		logFmt(
@@ -225,7 +207,9 @@ static void handleKick(char *prefix, char *params) {
 		uiFmt(
 			tag, (kicked ? UIHot : UICold),
 			"\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3",
-			color(user), nick, color(kick), kick, color(chan), chan
+			formatColor(user), nick,
+			formatColor(kick), kick,
+			formatColor(chan), chan
 		);
 		logFmt(tag, NULL, "%s kicks %s out of %s", nick, kick, chan);
 	}
@@ -244,11 +228,11 @@ static void handleQuit(char *prefix, char *params) {
 			uiFmt(
 				tag, UICold,
 				"\3%d%s\3 leaves, \"%s\"",
-				color(user), nick, dequote(mesg)
+				formatColor(user), nick, dequote(mesg)
 			);
 			logFmt(tag, NULL, "%s leaves, \"%s\"", nick, dequote(mesg));
 		} else {
-			uiFmt(tag, UICold, "\3%d%s\3 leaves", color(user), nick);
+			uiFmt(tag, UICold, "\3%d%s\3 leaves", formatColor(user), nick);
 			logFmt(tag, NULL, "%s leaves", nick);
 		}
 	}
@@ -263,7 +247,7 @@ static void handleReplyTopic(char *prefix, char *params) {
 	uiFmt(
 		tag, UICold,
 		"The sign in \3%d%s\3 reads, \"%s\"",
-		color(chan), chan, topic
+		formatColor(chan), chan, topic
 	);
 	logFmt(tag, NULL, "The sign in %s reads, \"%s\"", chan, topic);
 }
@@ -279,7 +263,7 @@ static void handleTopic(char *prefix, char *params) {
 	uiFmt(
 		tag, UICold,
 		"\3%d%s\3 places a new sign in \3%d%s\3, \"%s\"",
-		color(user), nick, color(chan), chan, topic
+		formatColor(user), nick, formatColor(chan), chan, topic
 	);
 	logFmt(tag, NULL, "%s places a new sign in %s, \"%s\"", nick, chan, topic);
 }
@@ -309,7 +293,7 @@ static void handleReplyWho(char *prefix, char *params) {
 	int len = snprintf(
 		&who.buf[who.len], cap,
 		"%s\3%d%s\3",
-		(who.len ? ", " : ""), color(user), nick
+		(who.len ? ", " : ""), formatColor(user), nick
 	);
 	if ((size_t)len < cap) who.len += len;
 }
@@ -322,7 +306,7 @@ static void handleReplyEndOfWho(char *prefix, char *params) {
 	uiFmt(
 		tag, UICold,
 		"In \3%d%s\3 are %s",
-		color(chan), chan, who.buf
+		formatColor(chan), chan, who.buf
 	);
 	who.len = 0;
 }
@@ -340,7 +324,7 @@ static void handleNick(char *prefix, char *params) {
 		uiFmt(
 			tag, UICold,
 			"\3%d%s\3 is now known as \3%d%s\3",
-			color(user), prev, color(user), next
+			formatColor(user), prev, formatColor(user), next
 		);
 		logFmt(tag, NULL, "%s is now known as %s", prev, next);
 	}
@@ -360,7 +344,7 @@ static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
 	uiFmt(
 		tag, (ping ? UIHot : UIWarm),
 		"%c\3%d* %s\17 %s",
-		ping["\17\26"], color(user), nick, params
+		ping["\17\26"], formatColor(user), nick, params
 	);
 	logFmt(tag, NULL, "* %s %s", nick, params);
 }
@@ -384,7 +368,7 @@ static void handlePrivmsg(char *prefix, char *params) {
 	uiFmt(
 		tag, (hot ? UIHot : UIWarm),
 		"%c\3%d%c%s%c\17 %s",
-		ping["\17\26"], color(user), self["<("], nick, self[">)"], mesg
+		ping["\17\26"], formatColor(user), self["<("], nick, self[">)"], mesg
 	);
 	logFmt(tag, NULL, "<%s> %s", nick, mesg);
 }
@@ -403,7 +387,7 @@ static void handleNotice(char *prefix, char *params) {
 	uiFmt(
 		tag, (ping ? UIHot : UIWarm),
 		"%c\3%d-%s-\17 %s",
-		ping["\17\26"], color(user), nick, mesg
+		ping["\17\26"], formatColor(user), nick, mesg
 	);
 	logFmt(tag, NULL, "-%s- %s", nick, mesg);
 }