[pacman-dev] [PATCH 06/15] add a config settings and flag for colours

Simon Gomizelj simongmzlj at gmail.com
Wed Mar 6 12:51:47 EST 2013


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 <simongmzlj at gmail.com>
---
 src/pacman/conf.c   |  4 ++++
 src/pacman/conf.h   |  9 ++++++++-
 src/pacman/pacman.c | 15 +++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 218ffb4..dca6e3e 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 d85d11f..6cabd33 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 38b28e1..bb8b439 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 <arch>    set an alternate architecture\n"));
 		addlist(_("      --cachedir <dir> set an alternate package cache location\n"));
+		addlist(_("      --color <when>   colorize the output\n"));
 		addlist(_("      --config <path>  set an alternate configuration file\n"));
 		addlist(_("      --debug          display debug messages\n"));
 		addlist(_("      --gpgdir <path>  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}
 	};
 
-- 
1.8.1.5



More information about the pacman-dev mailing list