mirror of https://github.com/djcb/mu.git
* update 'mu view' and 'mu cfind' to use colors in some cases
This commit is contained in:
parent
2b56e46260
commit
f1ad2ebc51
|
@ -22,6 +22,7 @@
|
|||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mu-cmd.h"
|
||||
#include "mu-util.h"
|
||||
|
@ -138,12 +139,37 @@ each_contact_org_contact (const char *email, const char *name)
|
|||
name, email);
|
||||
}
|
||||
|
||||
static void
|
||||
print_plain (const char *email, const char *name, gboolean color)
|
||||
{
|
||||
if (name) {
|
||||
if (color)
|
||||
mu_util_color_print (MU_COLOR_MAGENTA, name);
|
||||
else
|
||||
fputs (name, stdout);
|
||||
|
||||
fputs (" ", stdout);
|
||||
}
|
||||
|
||||
if (color)
|
||||
mu_util_color_print (MU_COLOR_GREEN, email);
|
||||
else
|
||||
fputs (email, stdout);
|
||||
|
||||
fputs ("\n", stdout);
|
||||
}
|
||||
|
||||
struct _ECData {
|
||||
OutputFormat format;
|
||||
gboolean color;
|
||||
};
|
||||
typedef struct _ECData ECData;
|
||||
|
||||
|
||||
static void
|
||||
each_contact (const char *email, const char *name, time_t tstamp,
|
||||
OutputFormat format)
|
||||
each_contact (const char *email, const char *name, time_t tstamp, ECData *ecdata)
|
||||
{
|
||||
switch (format) {
|
||||
switch (ecdata->format) {
|
||||
case FORMAT_MUTT_ALIAS: each_contact_mutt_alias (email, name); break;
|
||||
case FORMAT_MUTT_AB:
|
||||
g_print ("%s\t%s\t\n", email, name ? name : ""); break;
|
||||
|
@ -155,17 +181,19 @@ each_contact (const char *email, const char *name, time_t tstamp,
|
|||
g_print ("%s,%s\n", name ? name : "", email);
|
||||
break;
|
||||
default:
|
||||
g_print ("%s%s%s\n", name ? name : "", name ? " " : "", email);
|
||||
print_plain (email, name, ecdata->color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static MuExitCode
|
||||
run_cmd_cfind (const char* pattern, OutputFormat format)
|
||||
run_cmd_cfind (const char* pattern, OutputFormat format,
|
||||
gboolean color)
|
||||
{
|
||||
gboolean rv;
|
||||
MuContacts *contacts;
|
||||
size_t num;
|
||||
ECData ecdata = {format, color};
|
||||
|
||||
contacts = mu_contacts_new (mu_runtime_path(MU_RUNTIME_PATH_CONTACTS));
|
||||
if (!contacts) {
|
||||
|
@ -176,7 +204,7 @@ run_cmd_cfind (const char* pattern, OutputFormat format)
|
|||
print_header (format);
|
||||
rv = mu_contacts_foreach (contacts,
|
||||
(MuContactsForeachFunc)each_contact,
|
||||
GINT_TO_POINTER(format), pattern, &num);
|
||||
&ecdata, pattern, &num);
|
||||
|
||||
mu_contacts_destroy (contacts);
|
||||
|
||||
|
@ -208,10 +236,10 @@ mu_cmd_cfind (MuConfig *opts)
|
|||
|
||||
/* only one pattern allowed */
|
||||
if (opts->params[1] && opts->params[2]) {
|
||||
g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]");
|
||||
g_warning ("usage: mu cfind [options] [<ptrn>]");
|
||||
return MU_EXITCODE_ERROR;
|
||||
}
|
||||
|
||||
return run_cmd_cfind (opts->params[1], format);
|
||||
return run_cmd_cfind (opts->params[1], format, opts->color);
|
||||
}
|
||||
|
||||
|
|
52
src/mu-cmd.c
52
src/mu-cmd.c
|
@ -24,6 +24,7 @@
|
|||
#endif /*HAVE_CONFIG_H*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mu-msg.h"
|
||||
#include "mu-msg-part.h"
|
||||
|
@ -64,43 +65,56 @@ get_attach_str (MuMsg *msg)
|
|||
}
|
||||
|
||||
|
||||
static void
|
||||
print_field (const char* field, const char *val, gboolean color)
|
||||
{
|
||||
if (!val)
|
||||
return;
|
||||
|
||||
if (color) {
|
||||
mu_util_color_print (MU_COLOR_MAGENTA, field);
|
||||
mu_util_color_print (MU_COLOR_BLUE, val);
|
||||
} else {
|
||||
fputs (field, stdout);
|
||||
fputs (val, stdout);
|
||||
}
|
||||
fputs ("\n", stdout);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* we ignore fields for now */
|
||||
static gboolean
|
||||
view_msg (MuMsg *msg, const gchar *fields, size_t summary_len)
|
||||
view_msg (MuMsg *msg, const gchar *fields, gboolean summary,
|
||||
gboolean color)
|
||||
{
|
||||
const char *field;
|
||||
gchar *attachs;
|
||||
time_t date;
|
||||
const int SUMMARY_LEN = 5;
|
||||
|
||||
if ((field = mu_msg_get_from (msg)))
|
||||
g_print ("From: %s\n", field);
|
||||
print_field ("From: ", mu_msg_get_from (msg), color);
|
||||
print_field ("To: ", mu_msg_get_to (msg), color);
|
||||
print_field ("Cc: ", mu_msg_get_cc (msg), color);
|
||||
print_field ("Bcc: ", mu_msg_get_bcc (msg), color);
|
||||
print_field ("Subject: ", mu_msg_get_subject (msg), color);
|
||||
|
||||
if ((field = mu_msg_get_to (msg)))
|
||||
g_print ("To: %s\n", field);
|
||||
|
||||
if ((field = mu_msg_get_cc (msg)))
|
||||
g_print ("Cc: %s\n", field);
|
||||
|
||||
if ((field = mu_msg_get_subject (msg)))
|
||||
g_print ("Subject: %s\n", field);
|
||||
|
||||
if ((date = mu_msg_get_date (msg)))
|
||||
g_print ("Date: %s\n", mu_str_date_s ("%c", date));
|
||||
if ((date = mu_msg_get_date (msg)))
|
||||
print_field ("Date: ", mu_str_date_s ("%c", date),
|
||||
color);
|
||||
|
||||
if ((attachs = get_attach_str (msg))) {
|
||||
g_print ("Attachment(s): %s\n", attachs);
|
||||
print_field ("Attachments: ", attachs, color);
|
||||
g_free (attachs);
|
||||
}
|
||||
|
||||
if (!(field = mu_msg_get_body_text (msg)))
|
||||
return TRUE; /* no body -- nothing more to do */
|
||||
|
||||
if (summary_len > 0) {
|
||||
if (summary) {
|
||||
gchar *summ;
|
||||
summ = mu_str_summarize (field, summary_len);
|
||||
g_print ("Summary: %s\n", summ);
|
||||
summ = mu_str_summarize (field, SUMMARY_LEN);
|
||||
print_field ("Summary: ", summ, color);
|
||||
g_free (summ);
|
||||
} else
|
||||
g_print ("\n%s\n", field);
|
||||
|
@ -133,7 +147,7 @@ mu_cmd_view (MuConfig *opts)
|
|||
g_error_free (err);
|
||||
return MU_EXITCODE_ERROR;
|
||||
}
|
||||
if (!view_msg (msg, NULL, opts->summary_len))
|
||||
if (!view_msg (msg, NULL, opts->summary, opts->color))
|
||||
rv = MU_EXITCODE_ERROR;
|
||||
|
||||
mu_msg_unref (msg);
|
||||
|
|
Loading…
Reference in New Issue