summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-31 18:29:27 -0500
committerJune McEnroe <june@causal.agency>2019-12-31 18:29:27 -0500
commit7021511fcae5173cb8b5f092a53e61de22d9ca85 (patch)
tree2dc839825d5a4f1424d82804be4e73f927e6755e
parentRefactor scoop formatters and group colored output (diff)
downloadlitterbox-7021511fcae5173cb8b5f092a53e61de22d9ca85.tar.gz
litterbox-7021511fcae5173cb8b5f092a53e61de22d9ca85.zip
Add scoop output format option
Diffstat (limited to '')
-rw-r--r--scoop.111
-rw-r--r--scoop.c26
2 files changed, 32 insertions, 5 deletions
diff --git a/scoop.1 b/scoop.1
index 2dfdf09..82ac29a 100644
--- a/scoop.1
+++ b/scoop.1
@@ -15,6 +15,7 @@
 .Op Fl T Ar target
 .Op Fl c Ar context
 .Op Fl d Ar path
+.Op Fl f Ar format
 .Op Fl h Ar host
 .Op Fl l Ar limit
 .Op Fl n Ar nick
@@ -85,6 +86,16 @@ The database must have been initialized by
 The default path is as in
 .Xr litterbox 1 .
 .
+.It Fl f Ar format
+Set the output format to one of
+.Cm plain ,
+.Cm color .
+The default format is
+.Cm color
+if standard output is a terminal,
+.Cm plain
+otherwise.
+.
 .It Fl g
 Group events by context.
 .
diff --git a/scoop.c b/scoop.c
index 5c00c6b..55ee857 100644
--- a/scoop.c
+++ b/scoop.c
@@ -206,6 +206,21 @@ static enum Type parseType(const char *input) {
 	errx(EX_USAGE, "no such type %s", input);
 }
 
+static const struct {
+	const char *name;
+	Format *fn;
+} Formats[] = {
+	{ "plain", formatPlain },
+	{ "color", formatColor },
+};
+
+static Format *parseFormat(const char *name) {
+	for (size_t i = 0; i < ARRAY_LEN(Formats); ++i) {
+		if (!strcmp(name, Formats[i].name)) return Formats[i].fn;
+	}
+	errx(EX_USAGE, "no such format %s", name);
+}
+
 static struct Bind {
 	const char *param;
 	const char *text;
@@ -215,16 +230,19 @@ static struct Bind {
 }
 
 int main(int argc, char *argv[]) {
+	bool tty = isatty(STDOUT_FILENO);
+
 	char *path = NULL;
 	bool shell = false;
 	bool group = false;
+	Format *format = (tty ? formatColor : formatPlain);
 
 	int n = 0;
 	struct Bind binds[argc];
 	const char *search = NULL;
 
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "D:F:N:T:c:d:gh:l:n:pqst:u:v"))) {
+	while (0 < (opt = getopt(argc, argv, "D:F:N:T:c:d:f:gh:l:n:pqst:u:v"))) {
 		switch (opt) {
 			break; case 'D': binds[n++] = Bind(":date", optarg, 0);
 			break; case 'F': binds[n++] = Bind(":format", optarg, 0);
@@ -232,6 +250,7 @@ int main(int argc, char *argv[]) {
 			break; case 'T': binds[n++] = Bind(":target", optarg, 0);
 			break; case 'c': binds[n++] = Bind(":context", optarg, 0);
 			break; case 'd': path = optarg;
+			break; case 'f': format = parseFormat(optarg);
 			break; case 'g': group = true;
 			break; case 'h': binds[n++] = Bind(":host", optarg, 0);
 			break; case 'l': binds[n++] = Bind(":limit", optarg, 0);
@@ -256,7 +275,6 @@ int main(int argc, char *argv[]) {
 		err(EX_UNAVAILABLE, "sqlite3");
 	}
 
-	bool tty = isatty(STDOUT_FILENO);
 	if (tty) {
 		const char *pager = getenv("PAGER");
 		if (!pager) pager = "less";
@@ -312,7 +330,7 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
-	if (tty) {
+	if (format == formatColor) {
 		dbBindText(stmt, ":open", "\33[7m");
 		dbBindText(stmt, ":close", "\33[27m");
 	} else {
@@ -328,8 +346,6 @@ int main(int argc, char *argv[]) {
 		sqlite3_free(expand);
 	}
 
-	Format *format = (tty ? formatColor : formatPlain);
-
 	int result;
 	while (SQLITE_ROW == (result = sqlite3_step(stmt))) {
 		struct Event event = {
sg'> Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe