summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-07-14 12:34:02 -0400
committerJune McEnroe <june@causal.agency>2019-07-14 12:34:02 -0400
commitdfd363dd566980ca4ff949e9a53c81f9ce622316 (patch)
tree45e5bbef5fef7288a7fae83c152ba81aac0952f0
parentAdd shotty -c (diff)
downloadsrc-dfd363dd566980ca4ff949e9a53c81f9ce622316.tar.gz
src-dfd363dd566980ca4ff949e9a53c81f9ce622316.zip
Add shotty -d
-rw-r--r--bin/man1/shotty.18
-rw-r--r--bin/shotty.c90
2 files changed, 57 insertions, 41 deletions
diff --git a/bin/man1/shotty.1 b/bin/man1/shotty.1
index 9cc3c336..025ff293 100644
--- a/bin/man1/shotty.1
+++ b/bin/man1/shotty.1
@@ -1,4 +1,4 @@
-.Dd July 13, 2019
+.Dd July 14, 2019
 .Dt SHOTTY 1
 .Os
 .
@@ -8,7 +8,7 @@
 .
 .Sh SYNOPSIS
 .Nm
-.Op Fl Bcs
+.Op Fl Bcds
 .Op Fl b Ar bg
 .Op Fl f Ar fg
 .Op Fl h Ar rows
@@ -40,6 +40,10 @@ The default value is 0 (black).
 Show the position of the cursor
 with reverse video.
 .
+.It Fl d
+Output the terminal state
+for each CSI sequence.
+.
 .It Fl f Ar fg
 Set the default foreground color.
 The default value is 7 (white).
diff --git a/bin/shotty.c b/bin/shotty.c
index 87486990..a39c4f90 100644
--- a/bin/shotty.c
+++ b/bin/shotty.c
@@ -49,6 +49,7 @@ struct Cell {
 
 static struct Style def = { .fg = 7 };
 static uint rows = 24, cols = 80;
+static bool debug;
 
 static uint y, x;
 static bool insert;
@@ -70,6 +71,44 @@ static void move(struct Cell *dst, struct Cell *src, uint len) {
 	memmove(dst, src, sizeof(*dst) * len);
 }
 
+static void span(const struct Style *prev, const struct Cell *cell) {
+	if (!prev || memcmp(&cell->style, prev, sizeof(cell->style))) {
+		if (prev) printf("</span>");
+		uint bg = (cell->style.reverse ? cell->style.fg : cell->style.bg);
+		uint fg = (cell->style.reverse ? cell->style.bg : cell->style.fg);
+		printf(
+			"<span style=\"%s%s%s\" class=\"bg%u fg%u\">",
+			cell->style.bold ? "font-weight:bold;" : "",
+			cell->style.italic ? "font-style:italic;" : "",
+			cell->style.underline ? "text-decoration:underline;" : "",
+			bg, fg
+		);
+	}
+	switch (cell->ch) {
+		break; case '&': printf("&amp;");
+		break; case '<': printf("&lt;");
+		break; case '>': printf("&gt;");
+		break; default:  printf("%lc", cell->ch);
+	}
+}
+
+static void html(bool cursor) {
+	if (cursor) cell(y, x)->style.reverse ^= true;
+	printf(
+		"<pre style=\"width: %uch;\" class=\"bg%u fg%u\">",
+		cols, def.bg, def.fg
+	);
+	for (uint y = 0; y < rows; ++y) {
+		for (uint x = 0; x < cols; ++x) {
+			if (!cell(y, x)->ch) continue;
+			span(x ? &cell(y, x - 1)->style : NULL, cell(y, x));
+		}
+		printf("</span>\n");
+	}
+	printf("</pre>\n");
+	if (cursor) cell(y, x)->style.reverse ^= true;
+}
+
 static char updateNUL(wchar_t ch) {
 	switch (ch) {
 		case BS: if (x) x--; return NUL;
@@ -260,6 +299,15 @@ static char updateCSI(wchar_t ch) {
 		break; default: warnx("unhandled CSI %lc", ch);
 	}
 
+	if (debug) {
+		printf("CSI %s", (dec ? "? " : ""));
+		for (uint i = 0; i < n; ++i) {
+			printf("%s%u ", (i ? "; " : ""), ps[i]);
+		}
+		printf("%lc\n", ch);
+		html(true);
+	}
+
 	dec = false;
 	ps[n = p = 0] = 0;
 	return NUL;
@@ -274,28 +322,6 @@ static void update(wchar_t ch) {
 	}
 }
 
-static void
-html(const struct Style *prev, const struct Cell *cell) {
-	if (!prev || memcmp(&cell->style, prev, sizeof(cell->style))) {
-		if (prev) printf("</span>");
-		uint bg = (cell->style.reverse ? cell->style.fg : cell->style.bg);
-		uint fg = (cell->style.reverse ? cell->style.bg : cell->style.fg);
-		printf(
-			"<span style=\"%s%s%s\" class=\"bg%u fg%u\">",
-			cell->style.bold ? "font-weight:bold;" : "",
-			cell->style.italic ? "font-style:italic;" : "",
-			cell->style.underline ? "text-decoration:underline;" : "",
-			bg, fg
-		);
-	}
-	switch (cell->ch) {
-		break; case '&': printf("&amp;");
-		break; case '<': printf("&lt;");
-		break; case '>': printf("&gt;");
-		break; default:  printf("%lc", cell->ch);
-	}
-}
-
 int main(int argc, char *argv[]) {
 	setlocale(LC_CTYPE, "");
 
@@ -305,11 +331,12 @@ int main(int argc, char *argv[]) {
 	FILE *file = stdin;
 
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "Bb:cf:h:sw:"))) {
+	while (0 < (opt = getopt(argc, argv, "Bb:cdf:h:sw:"))) {
 		switch (opt) {
 			break; case 'B': bright = true;
 			break; case 'b': def.bg = strtoul(optarg, NULL, 0);
 			break; case 'c': cursor = true;
+			break; case 'd': debug = true;
 			break; case 'f': def.fg = strtoul(optarg, NULL, 0);
 			break; case 'h': rows = strtoul(optarg, NULL, 0);
 			break; case 's': size = true;
@@ -351,20 +378,5 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
-	if (cursor) {
-		cell(y, x)->style.reverse ^= true;
-	}
-
-	printf(
-		"<pre style=\"width: %uch;\" class=\"bg%u fg%u\">",
-		cols, def.bg, def.fg
-	);
-	for (uint y = 0; y < rows; ++y) {
-		for (uint x = 0; x < cols; ++x) {
-			if (!cell(y, x)->ch) continue;
-			html(x ? &cell(y, x - 1)->style : NULL, cell(y, x));
-		}
-		printf("</span>\n");
-	}
-	printf("</pre>\n");
+	html(cursor);
 }