From c8c7a5137473770eb4b2d9d54495c2a39b32cbe3 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Fri, 1 Mar 2013 14:27:40 -0500 Subject: add a config settings and flag for colours Colours can be enabled in two ways: - Add Color to pacman.conf. This enables colours automatically. - Use --color=WHEN where WHEN is none/auto/always. WHEN as 'never' disables colours (overrides config file), as 'auto' enables colours when stdout is a tty, and 'always' enables colours no matter what. Signed-off-by: Simon Gomizelj Signed-off-by: Allan McRae --- src/pacman/conf.c | 4 ++++ src/pacman/conf.h | 9 ++++++++- src/pacman/pacman.c | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'src/pacman') diff --git a/src/pacman/conf.c b/src/pacman/conf.c index 218ffb46..dca6e3e1 100644 --- a/src/pacman/conf.c +++ b/src/pacman/conf.c @@ -436,6 +436,10 @@ static int _parse_options(const char *key, char *value, pm_printf(ALPM_LOG_DEBUG, "config: totaldownload\n"); } else if(strcmp(key, "CheckSpace") == 0) { config->checkspace = 1; + } else if(strcmp(key, "Color") == 0) { + if(config->color == PM_COLOR_UNSET) { + config->color = isatty(fileno(stdout)) ? PM_COLOR_ON : PM_COLOR_OFF; + } } else { pm_printf(ALPM_LOG_WARNING, _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), diff --git a/src/pacman/conf.h b/src/pacman/conf.h index d85d11f2..6cabd33e 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -34,6 +34,7 @@ typedef struct __config_t { unsigned short print; unsigned short checkspace; unsigned short usesyslog; + unsigned short color; double deltaratio; char *arch; char *print_format; @@ -129,7 +130,8 @@ enum { OP_PRINTFORMAT, OP_GPGDIR, OP_DBONLY, - OP_FORCE + OP_FORCE, + OP_COLOR }; /* clean method */ @@ -145,6 +147,11 @@ enum { PKG_LOCALITY_FOREIGN = (1 << 1) }; +enum { + PM_COLOR_UNSET = 0, + PM_COLOR_OFF, + PM_COLOR_ON +}; /* global config variable */ extern config_t *config; diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 38b28e1f..bb8b439f 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -197,6 +197,7 @@ static void usage(int op, const char * const myname) addlist(_(" -v, --verbose be verbose\n")); addlist(_(" --arch set an alternate architecture\n")); addlist(_(" --cachedir set an alternate package cache location\n")); + addlist(_(" --color colorize the output\n")); addlist(_(" --config set an alternate configuration file\n")); addlist(_(" --debug display debug messages\n")); addlist(_(" --gpgdir set an alternate home directory for GnuPG\n")); @@ -394,6 +395,19 @@ static int parsearg_global(int opt) check_optarg(); config->cachedirs = alpm_list_add(config->cachedirs, strdup(optarg)); break; + case OP_COLOR: + if(strcmp("never", optarg) == 0) { + config->color = PM_COLOR_OFF; + } else if(strcmp("auto", optarg) == 0) { + config->color = isatty(fileno(stdout)) ? PM_COLOR_ON : PM_COLOR_OFF; + } else if(strcmp("always", optarg) == 0) { + config->color = PM_COLOR_ON; + } else { + pm_printf(ALPM_LOG_ERROR, _("invalid agument '%s' for %s\n"), + optarg, "--color"); + return 1; + } + break; case OP_CONFIG: check_optarg(); if(config->configfile) { @@ -632,6 +646,7 @@ static int parseargs(int argc, char *argv[]) {"print-format", required_argument, 0, OP_PRINTFORMAT}, {"gpgdir", required_argument, 0, OP_GPGDIR}, {"dbonly", no_argument, 0, OP_DBONLY}, + {"color", required_argument, 0, OP_COLOR}, {0, 0, 0, 0} }; -- cgit v1.2.3-70-g09d2