From 9792e3c4e1338c3c30fe2d12d03f1f6318c582d6 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 11 Dec 2009 23:55:35 +0200 Subject: [PATCH] * mu-config, mu-query: implement sortfield and ascending/descending --- src/mu-config.c | 9 ++++++--- src/mu-config.h | 8 +++----- src/mu-query.c | 32 ++++++++++++++------------------ src/mu.c | 1 - 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/mu-config.c b/src/mu-config.c index 59626837..7cc0353c 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -86,11 +86,11 @@ mu_config_options_group_query (MuConfigOptions *opts) "print the Xapian query", NULL}, {"fields", 'f', 0, G_OPTION_ARG_STRING, &opts->fields, "fields to display in output", NULL}, - {"sortfield", 's', 0, G_OPTION_ARG_STRING, &opts->sortfield_str, + {"sortfield", 's', 0, G_OPTION_ARG_STRING, &opts->sortfield, "field to sort on", NULL}, - {"ascending", 'u', 0, G_OPTION_ARG_NONE, &opts->ascending_flag, + {"ascending", 'u', 0, G_OPTION_ARG_NONE, &opts->ascending, "sort ascending (up)", NULL}, - {"descending", 'd', 0, G_OPTION_ARG_NONE, &opts->descending_flag, + {"descending", 'd', 0, G_OPTION_ARG_NONE, &opts->descending, "sort descending (down)", NULL}, { NULL, 0, 0, 0, NULL, NULL, NULL } }; @@ -128,6 +128,9 @@ mu_config_init (MuConfigOptions *opts) /* querying */ opts->xquery = FALSE; opts->fields = "d f s"; + opts->sortfield = "d"; + opts->ascending = FALSE; + opts->descending = TRUE; } diff --git a/src/mu-config.h b/src/mu-config.h index 23307247..ba5574b1 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -48,12 +48,10 @@ struct _MuConfigOptions { search results */ const char *fields; /* fields to show in output */ - const char *sortfield_str; /* field to sort by (string) */ + const char *sortfield; /* field to sort by (string) */ - /* FIXME: clean up this mess */ - gboolean sortdir_descending ; /* sort descending? */ - gboolean sortdir_ascending; - gboolean ascending_flag, descending_flag; + gboolean descending; /* sort descending? */ + gboolean ascending; }; typedef struct _MuConfigOptions MuConfigOptions; diff --git a/src/mu-query.c b/src/mu-query.c index b3bae21b..ed7ba22d 100644 --- a/src/mu-query.c +++ b/src/mu-query.c @@ -81,10 +81,7 @@ const MuMsgField* sort_field_from_string (const char* fieldstr) { const MuMsgField *field; - - if (fieldstr) - return NULL; - + field = mu_msg_field_from_name (fieldstr); if (!field && strlen(fieldstr) == 1) field = mu_msg_field_from_shortcut(fieldstr[0]); @@ -95,36 +92,35 @@ sort_field_from_string (const char* fieldstr) } -/* FIXME */ static gboolean handle_options (MuConfigOptions *opts) { - if (opts->ascending_flag && opts->descending_flag) { - g_printerr ("ignoring option '--descending'\n"); - opts->sortdir_ascending = TRUE; - } else if (!opts->descending_flag) - opts->sortdir_ascending = !opts->descending_flag; - - return TRUE; + if (opts->ascending && opts->descending) { + g_printerr ("only one of --ascending and --descending " + "may be specied\n"); + return FALSE; + } + + return opts->ascending = opts->descending ? FALSE : TRUE; } static gboolean print_rows (MuQueryXapian *xapian, const gchar *query, MuConfigOptions *opts) { - MuMsgXapian *row; - const MuMsgField *sortfield; + MuMsgXapian *row; + const MuMsgField *sortfield; - sortfield = NULL; - if (opts->sortfield_str) { - sortfield = sort_field_from_string (opts->sortfield_str); + sortfield = NULL; + if (opts->sortfield) { + sortfield = sort_field_from_string (opts->sortfield); if (!sortfield) /* error occured? */ return FALSE; } row = mu_query_xapian_run (xapian, query, sortfield, - opts->sortdir_ascending); + opts->ascending); if (!row) { g_printerr ("error: running query failed\n"); return FALSE; diff --git a/src/mu.c b/src/mu.c index e3e0df1e..1cfea1e4 100644 --- a/src/mu.c +++ b/src/mu.c @@ -41,7 +41,6 @@ enum _MuCmd { typedef enum _MuCmd MuCmd; - MuCmd parse_cmd (const char* cmd) {