From a2907dc8e56b166e41a854d81291ca0ef2116d20 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 26 May 2011 23:33:36 +0300 Subject: [PATCH] * mu-output: implement color output for 'mu find' --- src/mu-output.c | 60 +++++++++++++++++++++++++++++++++++++++++-------- src/mu-output.h | 12 ++++++---- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/mu-output.c b/src/mu-output.c index 6ec58371..5fb441d1 100644 --- a/src/mu-output.c +++ b/src/mu-output.c @@ -139,7 +139,48 @@ mu_output_links (MuMsgIter *iter, const char* linksdir, } -static const gchar* + +static void +ansi_color (MuMsgFieldId mfid) +{ + const char* ansi; + + switch (mfid) { + + case MU_MSG_FIELD_ID_FROM: + ansi = MU_COLOR_CYAN; break; + + case MU_MSG_FIELD_ID_TO: + case MU_MSG_FIELD_ID_CC: + case MU_MSG_FIELD_ID_BCC: + ansi = MU_COLOR_BLUE; break; + + case MU_MSG_FIELD_ID_SUBJECT: + ansi = MU_COLOR_GREEN; break; + + case MU_MSG_FIELD_ID_DATE: + ansi = MU_COLOR_MAGENTA; break; + + default: + if (mu_msg_field_type(mfid) == MU_MSG_FIELD_TYPE_STRING) + ansi = MU_COLOR_YELLOW; + else + ansi = MU_COLOR_RED; + } + + fputs (ansi, stdout); +} + + +static void +ansi_reset (MuMsgFieldId mfid) +{ + fputs (MU_COLOR_DEFAULT, stdout); + +} + + +static const char* display_field (MuMsgIter *iter, MuMsgFieldId mfid) { gint64 val; @@ -147,8 +188,7 @@ display_field (MuMsgIter *iter, MuMsgFieldId mfid) msg = mu_msg_iter_get_msg (iter, NULL); /* don't unref */ if (!msg) - return FALSE; - + return NULL; switch (mu_msg_field_type(mfid)) { case MU_MSG_FIELD_TYPE_STRING: { @@ -165,8 +205,8 @@ display_field (MuMsgIter *iter, MuMsgFieldId mfid) val = mu_msg_get_field_numeric (msg, mfid); return mu_str_flags_s ((MuMsgFlags)val); } else /* as string */ - return mu_msg_get_field_string (msg, mfid); - + return mu_msg_get_field_string (msg, mfid); + case MU_MSG_FIELD_TYPE_TIME_T: val = mu_msg_get_field_numeric (msg, mfid); return mu_str_date_s ("%c", (time_t)val); @@ -205,7 +245,7 @@ print_summary (MuMsgIter *iter) gboolean mu_output_plain (MuMsgIter *iter, const char *fields, gboolean summary, - size_t *count) + gboolean color, size_t *count) { MuMsgIter *myiter; size_t mycount; @@ -222,13 +262,15 @@ mu_output_plain (MuMsgIter *iter, const char *fields, gboolean summary, for (myfields = fields, len = 0; *myfields; ++myfields) { MuMsgFieldId mfid; mfid = mu_msg_field_id_from_shortcut (*myfields, FALSE); - if (mfid == MU_MSG_FIELD_ID_NONE || (!mu_msg_field_xapian_value (mfid) && !mu_msg_field_xapian_contact (mfid))) len += printf ("%c", *myfields); - else - len += printf ("%s", display_field(myiter, mfid)); + else { + if (color) ansi_color (mfid); + len += fputs (display_field (myiter, mfid), stdout); + if (color) ansi_reset (mfid); + } } g_print (len > 0 ? "\n" : ""); diff --git a/src/mu-output.h b/src/mu-output.h index e9cf0fa8..33363a17 100644 --- a/src/mu-output.h +++ b/src/mu-output.h @@ -33,14 +33,18 @@ G_BEGIN_DECLS * * @param iter iterator pointing to a message row * @param fields the fields to print (see MuMsgFields) - * @param summary whether we should include a summary (TRUE) or not (FALSE - * ) - * @param count output param to receive the number of messages found, or NULL + * + * @param summary whether we should include a summary (TRUE) or not + * (FALSE) + * @param whether we should display ANSI-colors in output (TRUE) or + * not (FALSE) + * @param count output param to receive the number of messages found, + * or NULL * * @return TRUE if the printing succeeded, FALSE in case of error */ gboolean mu_output_plain (MuMsgIter *iter, const char *fields, - gboolean summary, size_t *count); + gboolean summary, gboolean color, size_t *count); /** * output the search results (MsgIter) as a maildir of symlinks