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
commit11a71de2379644a6bbab6575ef8291374c87f374 (patch)
tree3e2e7024246691ef81f642cad79aed6cec424491
parentAdd shotty -c (diff)
downloadsrc-11a71de2379644a6bbab6575ef8291374c87f374.tar.gz
src-11a71de2379644a6bbab6575ef8291374c87f374.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 9d193ca3..60e3a2be 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);
 }