From b563128a4f4044272c8f7078c0983f7e758d0984 Mon Sep 17 00:00:00 2001 From: djcb Date: Sat, 21 Jan 2012 12:12:41 +0200 Subject: [PATCH] * make colorized output the default - turn of with --nocolor, or by setting MU_NOCOLOR to non-empty --- man/mu-easy.1 | 7 ++- man/mu.1 | 77 ++++++++++++++++--------------- src/mu-cmd-cfind.c | 6 +-- src/mu-cmd-extract.c | 4 +- src/mu-cmd-find.c | 4 +- src/mu-cmd-index.c | 105 +++++++++++++++++++++++++++++++------------ src/mu-cmd.c | 5 ++- src/mu-config.c | 17 ++++--- src/mu-config.h | 9 ++-- 9 files changed, 144 insertions(+), 90 deletions(-) diff --git a/man/mu-easy.1 b/man/mu-easy.1 index bcc2410b..288dc36b 100644 --- a/man/mu-easy.1 +++ b/man/mu-easy.1 @@ -1,4 +1,4 @@ -.TH MU-EASY 1 "December 2011" "User Manuals" +.TH MU-EASY 1 "January 2012" "User Manuals" .SH NAME @@ -19,6 +19,11 @@ that, such as \fBfind\fR), require that you store your mail in the Maildir-format. If you don't do so, you can still use the other commands, but you won't be able to index/search your mail. +By default, \fBmu\fR uses colorized output when your terminal is capable of +doing so. If you don't like color, you can use the \fB--nocolor\fR +command-line option, or set the \fBMU_NOCOLOR\fR environment variable to +non-empty. + .SH INDEXING YOUR E-MAIL Before you can search e-mails, you'll first need to index them: diff --git a/man/mu.1 b/man/mu.1 index 402982c5..98c13e10 100644 --- a/man/mu.1 +++ b/man/mu.1 @@ -1,6 +1,6 @@ -.TH MU 1 "May 2011" "User Manuals" +.TH MU 1 "January 2012" "User Manuals" -.SH NAME +.SH NAME mu \- a set of tools to deal with Maildirs and message files, in particular to index and search e-mail messages. @@ -98,14 +98,13 @@ for extract MIME-parts (such as attachments) from messages. See .SH COLORS -Some \fBmu\fR sub-commands support colorized output. By default, this is -disabled, but you can use the \fI--color\fR/ option to enable it. Even that, -colors will only shown when output goes to a sufficiently capable terminal -(this roughly mirrors the \fI--color=auto\fR of the GNU-version of the -\fBls\fR-command). +Some \fBmu\fR sub-commands support colorized output. If you don't want this, +you can use the \fI--nocolor\fR/ option to disable it. Even then, colors will +only shown when output goes to a sufficiently capable terminal (this roughly +mirrors the \fI--color=auto\fR of the GNU-version of the \fBls\fR-command). -Instead of the \fI--color\fR/, you can also set the \fBMU_COLORS\fR -environment variable to non-empty to enable colors. +Instead of the \fI--color\fR/, you can also set the \fBMU_NOCOLOR\fR +environment variable to non-empty to disable colors. Currently, \fBmu find\fR, \fBmu view\fR, \fBmu cfind\fR and \fBmu extract\fR support colors. @@ -184,36 +183,36 @@ The various mu subcommands typically exit with 0 (zero) upon success, and non-zero when some error occured. The table lists the various error codes. .nf -exit code | error +exit code | error ----------+------------------------------------------- - 1 | MU_ERROR - 2 | MU_ERROR_IN_PARAMETERS - 3 | MU_ERROR_INTERNAL - 4 | MU_ERROR_NO_MATCHES - | - 11 | MU_ERROR_XAPIAN - | - 13 | MU_ERROR_XAPIAN_QUERY - 14 | MU_ERROR_XAPIAN_DIR_NOT_ACCESSIBLE - 15 | MU_ERROR_XAPIAN_NOT_UP_TO_DATE - 16 | MU_ERROR_XAPIAN_MISSING_DATA - 17 | MU_ERROR_XAPIAN_CORRUPTION - 18 | MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK - 30 | MU_ERROR_GMIME - | - 50 | MU_ERROR_CONTACTS - 51 | MU_ERROR_CONTACTS_CANNOT_RETRIEVE - | - 70 | MU_ERROR_FILE - 71 | MU_ERROR_FILE_INVALID_NAME - 72 | MU_ERROR_FILE_CANNOT_LINK - 73 | MU_ERROR_FILE_CANNOT_OPEN - 74 | MU_ERROR_FILE_CANNOT_READ - 75 | MU_ERROR_FILE_CANNOT_CREATE - 76 | MU_ERROR_FILE_CANNOT_MKDIR - 77 | MU_ERROR_FILE_STAT_FAILED - 78 | MU_ERROR_FILE_READDIR_FAILED - 79 | MU_ERROR_FILE_INVALID_SOURCE + 1 | MU_ERROR + 2 | MU_ERROR_IN_PARAMETERS + 3 | MU_ERROR_INTERNAL + 4 | MU_ERROR_NO_MATCHES + | + 11 | MU_ERROR_XAPIAN + | + 13 | MU_ERROR_XAPIAN_QUERY + 14 | MU_ERROR_XAPIAN_DIR_NOT_ACCESSIBLE + 15 | MU_ERROR_XAPIAN_NOT_UP_TO_DATE + 16 | MU_ERROR_XAPIAN_MISSING_DATA + 17 | MU_ERROR_XAPIAN_CORRUPTION + 18 | MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK + 30 | MU_ERROR_GMIME + | + 50 | MU_ERROR_CONTACTS + 51 | MU_ERROR_CONTACTS_CANNOT_RETRIEVE + | + 70 | MU_ERROR_FILE + 71 | MU_ERROR_FILE_INVALID_NAME + 72 | MU_ERROR_FILE_CANNOT_LINK + 73 | MU_ERROR_FILE_CANNOT_OPEN + 74 | MU_ERROR_FILE_CANNOT_READ + 75 | MU_ERROR_FILE_CANNOT_CREATE + 76 | MU_ERROR_FILE_CANNOT_MKDIR + 77 | MU_ERROR_FILE_STAT_FAILED + 78 | MU_ERROR_FILE_READDIR_FAILED + 79 | MU_ERROR_FILE_INVALID_SOURCE .fi .SH BUGS @@ -235,4 +234,4 @@ Dirk-Jan C. Binnema .BR mu-view(1) .BR mu-extract(1) .BR mu-easy(1) -.BR mu-bookmarks(5) +.BR mu-bookmarks(5) diff --git a/src/mu-cmd-cfind.c b/src/mu-cmd-cfind.c index ae5ae2d8..7fa56795 100644 --- a/src/mu-cmd-cfind.c +++ b/src/mu-cmd-cfind.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2011 Dirk-Jan C. Binnema +** Copyright (C) 2011-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -239,6 +239,6 @@ mu_cmd_cfind (MuConfig *opts, GError **err) return MU_ERROR_IN_PARAMETERS; } - return run_cmd_cfind (opts->params[1], opts->format, opts->color, - err); + return run_cmd_cfind (opts->params[1], opts->format, + !opts->nocolor, err); } diff --git a/src/mu-cmd-extract.c b/src/mu-cmd-extract.c index e1b3c5c8..2c9bb95c 100644 --- a/src/mu-cmd-extract.c +++ b/src/mu-cmd-extract.c @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2010-2011 Dirk-Jan C. Binnema +** Copyright (C) 2010-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -345,7 +345,7 @@ show_parts (const char* path, MuConfig *opts, GError **err) mu_msg_part_foreach (msg,(MuMsgPartForeachFunc)each_part_show, - GUINT_TO_POINTER(opts->color)); + GUINT_TO_POINTER(!opts->nocolor)); mu_msg_unref (msg); diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index a71910ae..3d19be20 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -100,7 +100,7 @@ output_query_results (MuMsgIter *iter, MuConfig *opts, GError **err) return output_links (iter, opts->linksdir, opts->clearlinks, err); case MU_CONFIG_FORMAT_PLAIN: return output_plain (iter, opts->fields, opts->summary, - opts->threads, opts->color, + opts->threads, !opts->nocolor, opts->include_unreadable, err); case MU_CONFIG_FORMAT_XML: return output_xml (iter, opts->include_unreadable, err); diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index 470f8edc..028553ff 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -148,9 +148,8 @@ backspace (unsigned u) } - static void -print_stats (MuIndexStats* stats, gboolean clear) +print_stats (MuIndexStats* stats, gboolean clear, gboolean color) { const char *kars="-\\|/"; char output[120]; @@ -161,25 +160,49 @@ print_stats (MuIndexStats* stats, gboolean clear) if (clear) backspace (len); - len = (unsigned)snprintf (output, sizeof(output), - "%c processing mail; processed: %u; " - "updated/new: %u, cleaned-up: %u", - (unsigned)kars[++i % 4], - (unsigned)stats->_processed, - (unsigned)stats->_updated, - (unsigned)stats->_cleaned_up); + if (color) + len = (unsigned)snprintf + (output, sizeof(output), + MU_COLOR_BLUE "%c " MU_COLOR_DEFAULT + "processing mail; processed: " + MU_COLOR_GREEN "%u; " MU_COLOR_DEFAULT + "updated/new: " + MU_COLOR_GREEN "%u" MU_COLOR_DEFAULT + ", cleaned-up: " + MU_COLOR_GREEN "%u" MU_COLOR_DEFAULT, + (unsigned)kars[++i % 4], + (unsigned)stats->_processed, + (unsigned)stats->_updated, + (unsigned)stats->_cleaned_up); + else + len = (unsigned)snprintf + (output, sizeof(output), + "%c " + "processing mail; processed: %u; " + "updated/new: %u, cleaned-up: %u", + (unsigned)kars[++i % 4], + (unsigned)stats->_processed, + (unsigned)stats->_updated, + (unsigned)stats->_cleaned_up); + fputs (output, stdout); fflush (stdout); } +struct _IndexData { + gboolean color; +}; +typedef struct _IndexData IndexData; + + static MuError -index_msg_cb (MuIndexStats* stats, void *user_data) +index_msg_cb (MuIndexStats* stats, IndexData *idata) { if (stats->_processed % 25) return MU_OK; - print_stats (stats, TRUE); + print_stats (stats, TRUE, idata->color); return MU_CAUGHT_SIGNAL ? MU_STOP: MU_OK; } @@ -217,13 +240,28 @@ database_version_check_and_update (MuStore *store, MuConfig *opts, static void -show_time (unsigned t, unsigned processed) +show_time (unsigned t, unsigned processed, gboolean color) { - if (t) - g_message ("elapsed: %u second(s), ~ %u msg/s", - t, processed/t); - else - g_message ("elapsed: %u second(s)", t); + + if (color) { + if (t) + g_message ("elapsed: " + MU_COLOR_GREEN "%u" MU_COLOR_DEFAULT + " second(s), ~ " + MU_COLOR_GREEN "%u" MU_COLOR_DEFAULT + " msg/s", + t, processed/t); + else + g_message ("elapsed: " + MU_COLOR_GREEN "%u" MU_COLOR_DEFAULT + " second(s)", t); + } else { + if (t) + g_message ("elapsed: %u second(s), ~ %u msg/s", + t, processed/t); + else + g_message ("elapsed: %u second(s)", t); + } } @@ -234,6 +272,7 @@ cleanup_missing (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, { MuError rv; time_t t; + IndexData idata; g_message ("cleaning up messages [%s]", mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB)); @@ -241,13 +280,19 @@ cleanup_missing (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, mu_index_stats_clear (stats); t = time (NULL); - rv = mu_index_cleanup (midx, stats, - show_progress ? index_msg_cb : index_msg_silent_cb, - NULL, err); + idata.color = !opts->nocolor; + rv = mu_index_cleanup + (midx, stats, + show_progress ? + (MuIndexCleanupDeleteCallback)index_msg_cb : + (MuIndexCleanupDeleteCallback)index_msg_silent_cb, + &idata, err); + if (!opts->quiet) { - print_stats (stats, TRUE); + print_stats (stats, TRUE, !opts->nocolor); g_print ("\n"); - show_time ((unsigned)(time(NULL)-t),stats->_processed); + show_time ((unsigned)(time(NULL)-t),stats->_processed, + !opts->nocolor); } return (rv == MU_OK || rv == MU_STOP) ? MU_OK: MU_G_ERROR_CODE(err); @@ -259,6 +304,7 @@ static MuError cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, gboolean show_progress, GError **err) { + IndexData idata; MuError rv; time_t t; @@ -266,14 +312,18 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB)); t = time (NULL); + idata.color = !opts->nocolor; rv = mu_index_run (midx, opts->maildir, opts->reindex, stats, - show_progress ? index_msg_cb:index_msg_silent_cb, - NULL, NULL); + show_progress ? + (MuIndexMsgCallback)index_msg_cb : + (MuIndexMsgCallback)index_msg_silent_cb, + NULL, &idata); if (!opts->quiet) { - print_stats (stats, TRUE); + print_stats (stats, TRUE, !opts->nocolor); g_print ("\n"); - show_time ((unsigned)(time(NULL)-t),stats->_processed); + show_time ((unsigned)(time(NULL)-t), + stats->_processed, !opts->nocolor); } if (rv == MU_OK || rv == MU_STOP) @@ -348,4 +398,3 @@ mu_cmd_index (MuStore *store, MuConfig *opts, GError **err) return rv; } - diff --git a/src/mu-cmd.c b/src/mu-cmd.c index 4cd43313..dcdede43 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -1,6 +1,6 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2010-2011 Dirk-Jan C. Binnema +** Copyright (C) 2010-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -180,7 +180,8 @@ 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->color); + rv = view_msg_plain (msg, NULL, opts->summary, + !opts->nocolor); break; case MU_CONFIG_FORMAT_SEXP: rv = view_msg_sexp (msg); diff --git a/src/mu-config.c b/src/mu-config.c index d787b1bc..bed0049f 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -80,14 +80,13 @@ set_group_mu_defaults (MuConfig *opts) opts->muhome = exp; } - /* check for the MU_COLORS env var; but in any case don't use - * colors unless we're writing to a tty */ - - if (g_getenv (MU_COLORS) != NULL) - opts->color = TRUE; + /* check for the MU_NOCOLOR env var; but in any case don't + * use colors unless we're writing to a tty */ + if (g_getenv (MU_NOCOLOR) != NULL) + opts->nocolor = TRUE; if (!isatty(fileno(stdout))) - opts->color = FALSE; + opts->nocolor = TRUE; } @@ -106,8 +105,8 @@ config_options_group_mu (MuConfig *opts) "specify an alternative mu directory", NULL}, {"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr, "log to standard error (false)", NULL}, - {"color", 0, 0, G_OPTION_ARG_NONE, &opts->color, - "use ANSI-colors in some output (false)", NULL}, + {"nocolor", 0, 0, G_OPTION_ARG_NONE, &opts->nocolor, + "don't use ANSI-colors in some output (false)", NULL}, {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts->params, "parameters", NULL}, diff --git a/src/mu-config.h b/src/mu-config.h index 77ec9c0e..fb35021b 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -29,8 +29,8 @@ G_BEGIN_DECLS -/* env var; if non-empty, color are enabled for some commands */ -#define MU_COLORS "MU_COLORS" +/* env var; if non-empty, color are disabled */ +#define MU_NOCOLOR "MU_NOCOLOR" enum _MuConfigFormat { @@ -93,7 +93,8 @@ struct _MuConfig { gboolean version; /* request mu version */ gboolean log_stderr; /* log to stderr (not logfile) */ gchar** params; /* parameters (for querying) */ - gboolean color; /* use ansi-colors in some output */ + gboolean nocolor; /* don't use use ansi-colors + * in some output */ /* options for indexing */ char *maildir; /* where the mails are */