summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--scoop.17
-rw-r--r--scoop.c21
2 files changed, 21 insertions, 7 deletions
diff --git a/scoop.1 b/scoop.1
index 480e8be..7211990 100644
--- a/scoop.1
+++ b/scoop.1
@@ -1,4 +1,4 @@
-.Dd May  7, 2021
+.Dd May 17, 2021
 .Dt SCOOP 1
 .Os
 .
@@ -8,7 +8,7 @@
 .
 .Sh SYNOPSIS
 .Nm
-.Op Fl Lgpqsv
+.Op Fl Lgpqrsv
 .Op Fl D Ar date
 .Op Fl F Ar format
 .Op Fl N Ar network
@@ -169,6 +169,9 @@ Match only events from channels.
 .It Fl q
 Match only events from queries.
 .
+.It Fl r
+Output results in reverse order.
+.
 .It Fl s
 Sort the results from oldest to newest.
 By default events are output
diff --git a/scoop.c b/scoop.c
index 241c566..156d5c8 100644
--- a/scoop.c
+++ b/scoop.c
@@ -361,11 +361,12 @@ int main(int argc, char *argv[]) {
 
 	bool sort = false;
 	bool group = false;
+	bool reverse = false;
 	const char *limit = NULL;
 
 	int n = 0;
 	struct Bind binds[argc + 2];
-	const char *Opts = "D:F:LN:ST:a:b:c:d:f:gh:l:m:n:pqst:u:vw:";
+	const char *Opts = "D:F:LN:ST:a:b:c:d:f:gh:l:m:n:pqrst:u:vw:";
 	for (int opt; 0 < (opt = getopt(argc, argv, Opts));) {
 		switch (opt) {
 			break; case 'D': {
@@ -470,6 +471,9 @@ int main(int argc, char *argv[]) {
 				append(where, SQL(AND contexts.query = :query));
 				binds[n++] = Bind(":query", NULL, 1);
 			}
+			break; case 'r': {
+				reverse = true;
+			}
 			break; case 's': {
 				sort = true;
 			}
@@ -492,7 +496,8 @@ int main(int argc, char *argv[]) {
 		}
 	}
 
-	if (optind < argc) {
+	bool search = (optind < argc);
+	if (search) {
 		append(select, SQL(highlight(search, 6, :open, :close)));
 		append(from, SQL(JOIN search ON search.rowid = events.event));
 		append(where, SQL(AND search MATCH :search));
@@ -510,7 +515,7 @@ int main(int argc, char *argv[]) {
 	}
 
 	if (limit) {
-		if (optind < argc) {
+		if (search) {
 			append(where, SQL(ORDER BY search.rowid DESC LIMIT :limit));
 		} else {
 			append(where, SQL(ORDER BY event DESC LIMIT :limit));
@@ -543,9 +548,15 @@ int main(int argc, char *argv[]) {
 			SQL(
 				WITH results AS (%s %s %s)
 				SELECT * FROM results
-				ORDER BY %s time, event;
+				ORDER BY %s time %s, event %s;
 			),
-			select, from, where, (group ? "network, context," : "")
+			select, from, where, (group ? "network, context," : ""),
+			(reverse ? "DESC" : ""), (reverse ? "DESC" : "")
+		);
+	} else if (reverse) {
+		len = asprintf(
+			&query, "%s %s %s ORDER BY %s DESC;",
+			select, from, where, (search ? "search.rowid" : "event")
 		);
 	} else {
 		len = asprintf(&query, "%s %s %s;", select, from, where);