From 0d764d57b979be72a9a420474851a616c3b1d514 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 18 Oct 2021 13:31:08 -0400 Subject: Add shotty -i to output inline colors --- bin/man1/shotty.1 | 67 +++++++++++++++++++++++++++++-------------------------- bin/shotty.l | 23 ++++++++++++++----- 2 files changed, 53 insertions(+), 37 deletions(-) (limited to 'bin') diff --git a/bin/man1/shotty.1 b/bin/man1/shotty.1 index 5b9b7a69..0a3bd127 100644 --- a/bin/man1/shotty.1 +++ b/bin/man1/shotty.1 @@ -1,14 +1,14 @@ -.Dd November 25, 2019 +.Dd October 18, 2021 .Dt SHOTTY 1 .Os . .Sh NAME .Nm shotty -.Nd terminal capture +.Nd HTML terminal renderer . .Sh SYNOPSIS .Nm -.Op Fl Bdns +.Op Fl Bdins .Op Fl b Ar bg .Op Fl f Ar fg .Op Fl h Ar rows @@ -17,37 +17,37 @@ . .Sh DESCRIPTION .Nm -interprets terminal output from +renders a terminal session +captured with +.Xr ptee 1 +or +.Xr script 1 +from .Ar file or standard input -and produces HTML -.Sy
-on standard output.
-.
-.Pp
-Terminal output
-can be captured with
-.Xr ptee 1 .
+and renders one or more HTML snapshots.
+One snapshot is rendered
+for each media copy sequence,
+or a single snapshot is rendered
+at the end of the session.
 .Nm
 targets compatibility with
-.Ev TERM Ns = Ns Cm xterm
+.Ev TERM=xterm
 and
-.Ev TERM Ns = Ns Cm xterm-256color
+.Ev TERM=xterm-256color
 as used by
 .Xr ncurses 3 .
-A snapshot of the terminal
-is output each time
-a media copy sequence occurs,
-or once at the end of the capture.
 .
 .Pp
-HTML output uses the classes
+HTML output uses
 .Sy bg Ns Va n
 and
-.Sy fg Ns Va n ,
+.Sy fg Ns Va n
+classes for colors,
 and inline styles for
 bold, italic and underline.
-CSS for colors can be generated with
+CSS for colors
+can be generated by
 .Xr scheme 1 .
 .
 .Pp
@@ -58,38 +58,41 @@ Replace bold with bright colors.
 .
 .It Fl b Ar bg
 Set the default background color.
-The default value is 0 (black).
+The default is 0 (black).
 .
 .It Fl d
-Output the terminal state
-following each control sequence.
+Render a snapshot
+after each control sequence.
 .
 .It Fl f Ar fg
 Set the default foreground color.
-The default value is 7 (white).
+The default is 7 (white).
 .
 .It Fl h Ar rows
 Set the terminal height.
-The default value is 24.
+The default is 24.
+.
+.It Fl i
+Output inline color attributes.
 .
 .It Fl n
-Do not show the cursor.
+Hide the cursor.
 .
 .It Fl s
-Set the terminal size
-from the current terminal size.
+Copy the terminal size
+from the current terminal.
 .
 .It Fl w Ar cols
 Set the terminal width.
-The default value is 80.
+The default is 80.
 .El
 .
 .Sh EXAMPLES
-.Dl ptee htop | shotty -s > htop.html
+.Dl $ ptee htop | shotty -Bis >htop.html
 .
 .Sh SEE ALSO
 .Xr ptee 1 ,
-.Xr scheme 1
+.Xr script 1
 .
 .Sh STANDARDS
 .Bl -item
diff --git a/bin/shotty.l b/bin/shotty.l
index 67b5d422..3d1d50c0 100644
--- a/bin/shotty.l
+++ b/bin/shotty.l
@@ -431,9 +431,17 @@ static void update(enum Code cc) {
 }
 
 static bool bright;
+static bool colors;
 static int defaultBg = 0;
 static int defaultFg = 7;
 
+static const char *Inline[16] = {
+	"#000000;", "#CD0000;", "#00CD00;", "#CDCD00;",
+	"#0000EE;", "#CD00CD;", "#00CDCD;", "#E5E5E5;",
+	"#7F7F7F;", "#FF0000;", "#00FF00;", "#FFFF00;",
+	"#5C5CFF;", "#FF00FF;", "#00FFFF;", "#FFFFFF;",
+};
+
 static void span(const struct Cell *prev, const struct Cell *cell) {
 	if (
 		!prev ||
@@ -443,18 +451,22 @@ static void span(const struct Cell *prev, const struct Cell *cell) {
 	) {
 		if (prev) printf("");
 		int attr = cell->attr;
-		int bg = (cell->bg < 0 ? defaultBg : cell->bg);
-		int fg = (cell->fg < 0 ? defaultFg : cell->fg);
+		int bg = (attr & Reverse ? cell->fg : cell->bg);
+		int fg = (attr & Reverse ? cell->bg : cell->fg);
+		if (bg < 0) bg = defaultBg;
+		if (fg < 0) fg = defaultFg;
 		if (bright && cell->attr & Bold) {
 			if (fg < 8) fg += 8;
 			attr &= ~Bold;
 		}
 		printf(
-			Q(),
+			Q(),
+			bg, fg,
 			(attr & Bold ? "font-weight:bold;" : ""),
 			(attr & Italic ? "font-style:italic;" : ""),
 			(attr & Underline ? "text-decoration:underline;" : ""),
-			(attr & Reverse ? fg : bg), (attr & Reverse ? bg : fg)
+			(colors ? "background-color:" : ""), (colors ? Inline[bg] : ""),
+			(colors ? "color:" : ""), (colors ? Inline[fg] : "")
 		);
 	}
 	switch (cell->ch) {
@@ -496,13 +508,14 @@ int main(int argc, char *argv[]) {
 	bool size = false;
 	bool hide = false;
 
-	for (int opt; 0 < (opt = getopt(argc, argv, "Bb:df:h:nsw:"));) {
+	for (int opt; 0 < (opt = getopt(argc, argv, "Bb:df:h:insw:"));) {
 		switch (opt) {
 			break; case 'B': bright = true;
 			break; case 'b': defaultBg = atoi(optarg);
 			break; case 'd': debug = true;
 			break; case 'f': defaultFg = atoi(optarg);
 			break; case 'h': rows = atoi(optarg);
+			break; case 'i': colors = true;
 			break; case 'n': hide = true;
 			break; case 's': size = true;
 			break; case 'w': cols = atoi(optarg);
-- 
cgit 1.4.1