From 5013f4c23d78e24a6d37cf28fa8e1b4e0b521eda Mon Sep 17 00:00:00 2001 From: djcb Date: Sat, 28 Apr 2012 12:56:57 +0300 Subject: [PATCH] * add --summary-len option for mu find and mu view, and document it --- man/mu-find.1 | 6 +++++- man/mu-view.1 | 9 +++++++-- src/mu-cmd-find.c | 25 ++++++++++++++++--------- src/mu-cmd.c | 26 +++++++++++++++----------- src/mu-config.c | 14 ++++++++++++++ src/mu-config.h | 3 +++ 6 files changed, 60 insertions(+), 23 deletions(-) diff --git a/man/mu-find.1 b/man/mu-find.1 index 4574ab97..ee81579d 100644 --- a/man/mu-find.1 +++ b/man/mu-find.1 @@ -1,4 +1,4 @@ -.TH MU FIND 1 "December 2011" "User Manuals" +.TH MU FIND 1 "April 2012" "User Manuals" .SH NAME @@ -340,6 +340,10 @@ choice, but for dates it may be more useful to sort in the opposite direction. \fB\-\-summary\fR output a summary based upon the first lines of the message. +.TP +\fB\-\-summary-len=\fR +Number of lines to use for the summary. Default: 5. + .TP \fB\-\-include\-unreadable\fR normally, \fBmu find\fR does not include messages that are unreadable, diff --git a/man/mu-view.1 b/man/mu-view.1 index b78e2a88..4bd5d35f 100644 --- a/man/mu-view.1 +++ b/man/mu-view.1 @@ -1,6 +1,6 @@ -.TH MU VIEW 1 "July 2011" "User Manuals" +.TH MU VIEW 1 "April 2012" "User Manuals" -.SH NAME +.SH NAME mu view\- display an e-mail message file @@ -25,6 +25,11 @@ any). instead of displaying the full message, output a summary based upon the first lines of the message. +.TP +\fB\-\-summary-len=\fR +Number of lines to use for the summary. Default: 5. + + \fB\-\-terminate\fR terminate messaages with a \\f (\fIform-feed\fR) characters when displaying them. This is useful when you want to further process them. diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index e8de0c65..bbfac780 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -53,7 +53,8 @@ static gboolean output_sexp (MuMsgIter *iter, gboolean threads, static gboolean output_xml (MuMsgIter *iter,gboolean include_unreadable, GError **err); static gboolean output_plain (MuMsgIter *iter, const char *fields, - gboolean summary,gboolean threads, + gboolean summary, int summary_len, + gboolean threads, gboolean color, gboolean include_unreadable, GError **err); @@ -99,7 +100,8 @@ output_query_results (MuMsgIter *iter, MuConfig *opts, GError **err) case MU_CONFIG_FORMAT_LINKS: return output_links (iter, opts->linksdir, opts->clearlinks, err); case MU_CONFIG_FORMAT_PLAIN: - return output_plain (iter, opts->fields, opts->summary, + return output_plain (iter, opts->fields, + opts->summary, opts->summary_len, opts->threads, !opts->nocolor, opts->include_unreadable, err); case MU_CONFIG_FORMAT_XML: @@ -267,6 +269,7 @@ query_params_valid (MuConfig *opts, GError **err) return FALSE; } + static gchar* resolve_bookmark (MuConfig *opts, GError **err) { @@ -566,13 +569,11 @@ display_field (MuMsg *msg, MuMsgFieldId mfid) static void -print_summary (MuMsg *msg) +print_summary (MuMsg *msg, int summary_len) { char *summ; - - const guint SUMMARY_LEN = 5; /* summary based on first 5 - * lines */ - summ = mu_str_summarize (mu_msg_get_body_text(msg), SUMMARY_LEN); + summ = mu_str_summarize (mu_msg_get_body_text(msg), + (unsigned)summary_len); g_print ("Summary: %s\n", summ ? summ : ""); g_free (summ); } @@ -648,7 +649,7 @@ output_plain_fields (MuMsg *msg, const char *fields, } static gboolean -output_plain (MuMsgIter *iter, const char *fields, gboolean summary, +output_plain (MuMsgIter *iter, const char *fields, gboolean summary, int summary_len, gboolean threads, gboolean color, gboolean include_unreadable, GError **err) { @@ -658,6 +659,12 @@ output_plain (MuMsgIter *iter, const char *fields, gboolean summary, g_return_val_if_fail (iter, FALSE); g_return_val_if_fail (fields, FALSE); + if (summary && summary_len < 1) { + mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS, + "summary must be >= 1"); + return FALSE; + } + for (myiter = iter, count = 0; !mu_msg_iter_is_done (myiter); mu_msg_iter_next (myiter)) { @@ -680,7 +687,7 @@ output_plain (MuMsgIter *iter, const char *fields, gboolean summary, output_plain_fields (msg, fields, color, threads); if (summary) - print_summary (msg); + print_summary (msg, summary_len); ++count; } diff --git a/src/mu-cmd.c b/src/mu-cmd.c index a62be805..86a975b2 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -108,19 +108,19 @@ print_field (const char* field, const char *val, gboolean color) } +/* a summary_len of 0 mean 'don't show summary, show body */ static void -body_or_summary (MuMsg *msg, gboolean summary, gboolean color) +body_or_summary (MuMsg *msg, unsigned summary_len, gboolean color) { const char* field; - const int SUMMARY_LEN = 5; field = mu_msg_get_body_text (msg); if (!field) return; /* no body -- nothing more to do */ - if (summary) { + if (summary_len != 0) { gchar *summ; - summ = mu_str_summarize (field, SUMMARY_LEN); + summ = mu_str_summarize (field, summary_len); print_field ("Summary", summ, color); g_free (summ); } else { @@ -132,8 +132,9 @@ body_or_summary (MuMsg *msg, gboolean summary, gboolean color) /* we ignore fields for now */ +/* summary_len == 0 means "no summary */ static gboolean -view_msg_plain (MuMsg *msg, const gchar *fields, gboolean summary, +view_msg_plain (MuMsg *msg, const gchar *fields, unsigned summary_len, gboolean color) { gchar *attachs; @@ -162,7 +163,7 @@ view_msg_plain (MuMsg *msg, const gchar *fields, gboolean summary, g_free (attachs); } - body_or_summary (msg, summary, color); + body_or_summary (msg, summary_len, color); return TRUE; } @@ -181,8 +182,10 @@ handle_msg (const char *fname, MuConfig *opts, GError **err) switch (opts->format) { case MU_CONFIG_FORMAT_PLAIN: - rv = view_msg_plain (msg, NULL, opts->summary, - !opts->nocolor); + rv = view_msg_plain + (msg, NULL, + opts->summary ? opts->summary_len : 0, + !opts->nocolor); break; case MU_CONFIG_FORMAT_SEXP: rv = view_msg_sexp (msg); @@ -202,7 +205,7 @@ view_params_valid (MuConfig *opts, GError **err) { /* note: params[0] will be 'view' */ if (!opts->params[0] || !opts->params[1]) { - g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS, + mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS, "error in parameters"); return FALSE; } @@ -212,7 +215,7 @@ view_params_valid (MuConfig *opts, GError **err) case MU_CONFIG_FORMAT_SEXP: break; default: - g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS, + mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS, "invalid output format"); return FALSE; } @@ -438,7 +441,8 @@ check_params (MuConfig *opts, GError **err) { if (!opts->params||!opts->params[0]) {/* no command? */ show_usage (); - g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS, "error in parameters"); + mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS, + "error in parameters"); return FALSE; } diff --git a/src/mu-config.c b/src/mu-config.c index d415b7d2..ed572460 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -35,6 +35,8 @@ static MuConfig MU_CONFIG; +#define DEFAULT_SUMMARY_LEN 5 + static MuConfigFormat get_output_format (const char *formatstr) @@ -192,6 +194,10 @@ set_group_find_defaults (void) else g_free(old); } + + if ((MU_CONFIG.summary && !MU_CONFIG.summary_len)|| + (MU_CONFIG.summary_len < 1)) + MU_CONFIG.summary_len = DEFAULT_SUMMARY_LEN; } static GOptionGroup* @@ -211,6 +217,8 @@ config_options_group_find (void) "sort in reverse (descending) order (z -> a)", NULL}, {"summary", 'k', 0, G_OPTION_ARG_NONE, &MU_CONFIG.summary, "include a short summary of the message (false)", NULL}, + {"summary-len", 0, 0, G_OPTION_ARG_INT, &MU_CONFIG.summary_len, + "use up to lines for the summary (5)", NULL}, {"linksdir", 0, 0, G_OPTION_ARG_STRING, &MU_CONFIG.linksdir, "output as symbolic links to a target maildir", NULL}, {"clearlinks", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.clearlinks, @@ -292,6 +300,10 @@ set_group_view_defaults (void) MU_CONFIG.format = MU_CONFIG_FORMAT_PLAIN; else MU_CONFIG.format = get_output_format (MU_CONFIG.formatstr); + + if ((MU_CONFIG.summary && !MU_CONFIG.summary_len)|| + (MU_CONFIG.summary_len < 1)) + MU_CONFIG.summary_len = DEFAULT_SUMMARY_LEN; } static GOptionGroup * @@ -301,6 +313,8 @@ config_options_group_view (void) GOptionEntry entries[] = { {"summary", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.summary, "only show a short summary of the message (false)", NULL}, + {"summary-len", 0, 0, G_OPTION_ARG_INT, &MU_CONFIG.summary_len, + "use up to lines for the summary (5)", NULL}, {"terminate", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.terminator, "terminate messages with ascii-0x07 (\\f, form-feed)", NULL}, {"format", 'o', 0, G_OPTION_ARG_STRING, &MU_CONFIG.formatstr, diff --git a/src/mu-config.h b/src/mu-config.h index 7dd43c8c..ead79947 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -113,7 +113,10 @@ struct _MuConfig { char *sortfield; /* field to sort by (string) */ gboolean reverse; /* sort in revers order (z->a) */ gboolean threads; /* show message threads */ + gboolean summary; /* include a summary? */ + int summary_len; /* max # of lines for summary */ + char *bookmark; /* use bookmark */ char *formatstr; /* output type for find * (plain,links,xml,json,sexp)