[RFC PATCH 2/3] cli: add support for limiting the number of search results

Subject: [RFC PATCH 2/3] cli: add support for limiting the number of search results

Date: Fri, 28 Oct 2011 23:59:30 +0300

To: notmuch@notmuchmail.org

Cc: amdragon@mit.edu

From: Jani Nikula


Add command line parameter --maxitems=N to notmuch search to limit the
number of displayed messages to N.

These two are equal:

$ notmuch search --output=messages --sort=newest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=newest-first SEARCH | head

As are these:

$ notmuch search --output=messages --sort=oldest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=oldest-first SEARCH | tail

Note that N refers to the maximum amount of messages, even for
--output=threads.

Signed-off-by: Jani Nikula <jani@nikula.org>
---
 notmuch-search.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 6f04d9a..a3a6475 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -394,6 +394,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     const search_format_t *format = &format_text;
     int i, ret;
     output_t output = OUTPUT_SUMMARY;
+    unsigned int maxitems = 0;
 
     argc--; argv++; /* skip subcommand argument */
 
@@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 		fprintf (stderr, "Invalid value for --sort: %s\n", opt);
 		return 1;
 	    }
+	} else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
+	    const char *p;
+	    opt = argv[i] + sizeof ("--maxitems=") - 1;
+	    maxitems = strtoul(opt, &p, 10);
+	    if (*opt == '\0' || p == opt || *p != '\0') {
+		fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);
+		return 1;
+	    }
 	} else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
 	    opt = argv[i] + sizeof ("--format=") - 1;
 	    if (strcmp (opt, "text") == 0) {
@@ -473,6 +482,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     }
 
     notmuch_query_set_sort (query, sort);
+    notmuch_query_set_maxitems (query, maxitems);
 
     switch (output) {
     default:
-- 
1.7.5.4


Thread: