introduce colstr for colourizing

colstr_t colstr will hold the colourizing agents.

Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Simon Gomizelj 2013-03-01 14:52:16 -05:00 committed by Allan McRae
parent c8c7a51374
commit 6582f68c9d
3 changed files with 65 additions and 0 deletions

View File

@ -39,6 +39,43 @@
/* global config variable */
config_t *config = NULL;
#define NOCOLOR "\033[0m"
#define BLACK "\033[0;30m"
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define BLUE "\033[0;34m"
#define MAGENTA "\033[0;35m"
#define CYAN "\033[0;36m"
#define WHITE "\033[0;37m"
#define BOLDBLACK "\033[1;30m"
#define BOLDRED "\033[1;31m"
#define BOLDGREEN "\033[1;32m"
#define BOLDYELLOW "\033[1;33m"
#define BOLDBLUE "\033[1;34m"
#define BOLDMAGENTA "\033[1;35m"
#define BOLDCYAN "\033[1;36m"
#define BOLDWHITE "\033[1;37m"
void enable_colors(int colors)
{
colstr_t *colstr = &config->colstr;
if(colors == PM_COLOR_ON) {
colstr->colon = BOLDBLUE "::" BOLDWHITE " ";
colstr->title = BOLDWHITE;
colstr->repo = BOLDMAGENTA;
colstr->version = BOLDGREEN;
colstr->groups = BOLDBLUE;
colstr->meta = BOLDCYAN;
colstr->warn = BOLDYELLOW;
colstr->err = BOLDRED;
colstr->nocolor = NOCOLOR;
}
}
config_t *config_new(void)
{
config_t *newconfig = calloc(1, sizeof(config_t));
@ -60,6 +97,16 @@ config_t *config_new(void)
newconfig->remotefilesiglevel = ALPM_SIG_USE_DEFAULT;
}
newconfig->colstr.colon = ":: ";
newconfig->colstr.title = "";
newconfig->colstr.repo = "";
newconfig->colstr.version = "";
newconfig->colstr.groups = "";
newconfig->colstr.meta = "";
newconfig->colstr.warn = "";
newconfig->colstr.err = "";
newconfig->colstr.nocolor = "";
return newconfig;
}
@ -439,6 +486,7 @@ static int _parse_options(const char *key, char *value,
} else if(strcmp(key, "Color") == 0) {
if(config->color == PM_COLOR_UNSET) {
config->color = isatty(fileno(stdout)) ? PM_COLOR_ON : PM_COLOR_OFF;
enable_colors(config->color);
}
} else {
pm_printf(ALPM_LOG_WARNING,

View File

@ -22,6 +22,18 @@
#include <alpm.h>
typedef struct __colstr_t {
const char *colon;
const char *title;
const char *repo;
const char *version;
const char *groups;
const char *meta;
const char *warn;
const char *err;
const char *nocolor;
} colstr_t;
typedef struct __config_t {
unsigned short op;
unsigned short quiet;
@ -98,6 +110,9 @@ typedef struct __config_t {
alpm_list_t *explicit_adds;
alpm_list_t *explicit_removes;
/* Color strings for output */
colstr_t colstr;
} config_t;
/* Operations */
@ -156,6 +171,7 @@ enum {
/* global config variable */
extern config_t *config;
void enable_colors(int colors);
config_t *config_new(void);
int config_free(config_t *oldconfig);

View File

@ -407,6 +407,7 @@ static int parsearg_global(int opt)
optarg, "--color");
return 1;
}
enable_colors(config->color);
break;
case OP_CONFIG:
check_optarg();