From 9c1f6d96e2d64b4763ff02c23aa942849b28176b Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Tue, 14 Aug 2012 19:03:29 -0400 Subject: colorize pretty printed output .,,. ,;;*;;;;, .-'``;-');;. /' .-. /*;; .' \d \;; .;;;, / o ` \; ,__. ,;*;;;*;, \__, _.__,' \_.-') __)--.;;;;;*;;;;, `""`;;;\ /-')_) __) `\' ';;;;;; ;*;;; -') `)_) |\ | ;;;;*; ;;;;| `---` O | | ;;*;;; *;*;\| O / ;;;;;* ;;;;;/| .-------\ / ;*;;;;; ;;;*;/ \ | '. (`. ;;;*;;; ;;;;;'. ; | ) \ | ;;;;;; ,;*;;;;\/ |. / /` | ';;;*; ;;;;;;/ |/ / /__/ ';;; '*jgs/ | / | ;*; `""""` `""""` ;' --- ponymix.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/ponymix.c b/ponymix.c index 81ef211..ebe5012 100644 --- a/ponymix.c +++ b/ponymix.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -46,6 +47,12 @@ ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \ }) +#define COLOR_RESET "\033[0m" +#define COLOR_BOLD "\033[1m" +#define COLOR_RED "\033[31m" +#define COLOR_GREEN "\033[32m" +#define COLOR_YELLOW "\033[33m" + enum mode { MODE_DEVICE = 0, MODE_APP, @@ -126,6 +133,15 @@ struct pulseaudio_t { struct io_t *head; }; +struct colstr_t { + const char *name; + const char *high; + const char *mid; + const char *low; + const char *mute; + const char *nc; +}; + static int xstrtol(const char *str, long *out) { char *end = NULL; @@ -421,19 +437,48 @@ static int move_client(struct pulseaudio_t *pulse, struct io_t *dev) return !pulse->success; } -static void print(struct io_t *dev) +static void print(struct colstr_t *colstr, struct io_t *dev) { - printf("%s %2d: %s\n %s\n Avg. Volume: %d%% %s\n", dev->pp_name, - dev->idx, dev->name, dev->desc, dev->volume_percent, - dev->mute ? "[Muted]" : ""); + const char *level; + const char *mute = dev->mute ? "[Muted]" : ""; + + if (dev->volume_percent < 50) + level = colstr->low; + else if (dev->volume_percent < 75) + level = colstr->mid; + else + level = colstr->high; + + printf("%s%s %d:%s %s\n", colstr->name, dev->pp_name, dev->idx, + colstr->nc, dev->name); + printf(" %s\n", dev->desc); + printf(" Avg. Volume: %s%d%%%s %s%s%s\n", level, dev->volume_percent, + colstr->nc, colstr->mute, mute, colstr->nc); } static void print_all(struct pulseaudio_t *pulse) { struct io_t *dev = pulse->head; + struct colstr_t colstr; + + if (isatty(fileno(stdout))) { + colstr.name = COLOR_BOLD; + colstr.high = COLOR_RED; + colstr.mid = COLOR_YELLOW; + colstr.low = COLOR_GREEN; + colstr.mute = COLOR_BOLD COLOR_RED; + colstr.nc = COLOR_RESET; + } else { + colstr.name = ""; + colstr.high = ""; + colstr.mid = ""; + colstr.low = ""; + colstr.mute = ""; + colstr.nc = ""; + } while (dev) { - print(dev); + print(&colstr, dev); dev = dev->next; } } -- cgit v1.2.3