summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-02-09 00:34:42 -0500
committerJune McEnroe <june@causal.agency>2019-02-09 00:34:42 -0500
commitcb7c9af1b213361ebdec9428f36a78b5b81b5e8e (patch)
tree105bec53f94dd82d9819054a478ae3d55133c476
parentUse X macros for hi classes (diff)
downloadsrc-cb7c9af1b213361ebdec9428f36a78b5b81b5e8e.tar.gz
src-cb7c9af1b213361ebdec9428f36a78b5b81b5e8e.zip
Avoid using SGRReset and IRCReset
-rw-r--r--bin/hi.c43
1 files changed, 22 insertions, 21 deletions
diff --git a/bin/hi.c b/bin/hi.c
index bc560c6f..b545af75 100644
--- a/bin/hi.c
+++ b/bin/hi.c
@@ -312,8 +312,8 @@ typedef void OutputFn(enum Class class, const char *str, size_t len);
 // ANSI format {{{
 
 enum SGR {
-	SGRReset,
-	SGRBold,
+	SGRBoldOn = 1,
+	SGRBoldOff = 22,
 	SGRBlack = 30,
 	SGRRed,
 	SGRGreen,
@@ -322,18 +322,19 @@ enum SGR {
 	SGRMagenta,
 	SGRCyan,
 	SGRWhite,
+	SGRDefault = 39,
 };
 
-static const enum SGR ANSIStyle[ClassLen][2] = {
-	[Normal]  = { SGRReset },
+static const enum SGR ANSIStyle[ClassLen][3] = {
+	[Normal]  = { SGRDefault },
 	[Keyword] = { SGRWhite },
 	[Macro]   = { SGRGreen },
 	[String]  = { SGRCyan },
-	[Escape]  = { SGRReset },
-	[Format]  = { SGRCyan, SGRBold },
+	[Escape]  = { SGRDefault },
+	[Format]  = { SGRCyan, SGRBoldOn, SGRBoldOff },
 	[Interp]  = { SGRGreen },
 	[Comment] = { SGRBlue },
-	[Todo]    = { SGRBlue, SGRBold },
+	[Todo]    = { SGRBlue, SGRBoldOn, SGRBoldOff },
 };
 
 static void ansiOutput(enum Class class, const char *str, size_t len) {
@@ -346,7 +347,7 @@ static void ansiOutput(enum Class class, const char *str, size_t len) {
 				"\x1B[%d;%dm%.*s\x1B[%dm",
 				ANSIStyle[class][0], ANSIStyle[class][1],
 				(int)line, str,
-				SGRReset
+				ANSIStyle[class][2]
 			);
 		} else {
 			printf("\x1B[%dm%.*s", ANSIStyle[class][0], (int)line, str);
@@ -383,12 +384,11 @@ enum IRC {
 	IRCLightGray,
 	IRCBold = 0x02,
 	IRCColor = 0x03,
-	IRCReset = 0x0F,
 };
 
 static const enum IRC SGRIRC[] = {
-	[SGRReset]   = IRCReset,
-	[SGRBold]    = IRCBold,
+	[SGRBoldOn]  = IRCBold,
+	[SGRBoldOff] = IRCBold,
 	[SGRBlack]   = IRCBlack,
 	[SGRRed]     = IRCRed,
 	[SGRGreen]   = IRCGreen,
@@ -397,6 +397,7 @@ static const enum IRC SGRIRC[] = {
 	[SGRMagenta] = IRCMagenta,
 	[SGRCyan]    = IRCCyan,
 	[SGRWhite]   = IRCGray,
+	[SGRDefault] = 0,
 };
 
 static void ircOutput(enum Class class, const char *str, size_t len) {
@@ -404,23 +405,23 @@ static void ircOutput(enum Class class, const char *str, size_t len) {
 	while (len) {
 		size_t line = strcspn(str, "\n");
 		if (line > len) line = len;
+		char cc[3] = "";
+		if (ANSIStyle[class][0] != SGRDefault) {
+			snprintf(cc, sizeof(cc), "%d", SGRIRC[ANSIStyle[class][0]]);
+		}
 		if (ANSIStyle[class][1]) {
 			printf(
-				"%c%d%c%.*s%c",
-				IRCColor, SGRIRC[ANSIStyle[class][0]],
-				SGRIRC[ANSIStyle[class][1]],
+				"%c%s%c%.*s%c",
+				IRCColor, cc, SGRIRC[ANSIStyle[class][1]],
 				(int)line, str,
-				IRCReset
+				SGRIRC[ANSIStyle[class][2]]
 			);
-		} else if (ANSIStyle[class][0]) {
+		} else {
 			// Double-toggle bold to prevent str being interpreted as color.
 			printf(
-				"%c%d%c%c%.*s",
-				IRCColor, SGRIRC[ANSIStyle[class][0]], IRCBold, IRCBold,
-				(int)line, str
+				"%c%s%c%c%.*s",
+				IRCColor, cc, IRCBold, IRCBold, (int)line, str
 			);
-		} else {
-			printf("%c%.*s", IRCReset, (int)line, str);
 		}
 		if (line < len) {
 			printf("\n");