summary refs log tree commit diff
path: root/scoop.c
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 /scoop.c
parentRefactor scoop formatters and group colored output (diff)
downloadlitterbox-7021511fcae5173cb8b5f092a53e61de22d9ca85.tar.gz
litterbox-7021511fcae5173cb8b5f092a53e61de22d9ca85.zip
Add scoop output format option
Diffstat (limited to 'scoop.c')
-rw-r--r--scoop.c26
1 files changed, 21 insertions, 5 deletions
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 = {