From 7a372b0f4627b9482d1276238348a1432c13fbe3 Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Thu, 16 May 2013 22:49:13 +0200 Subject: Implement term-output-format --- src/auto_detect_format.c | 5 +++++ src/output.c | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/auto_detect_format.c b/src/auto_detect_format.c index 6ec5f73..524e2e9 100644 --- a/src/auto_detect_format.c +++ b/src/auto_detect_format.c @@ -30,6 +30,11 @@ * */ char *auto_detect_format(void) { + /* If stdout is a tty, we output directly to a terminal. */ + if (isatty(STDOUT_FILENO)) { + return "term"; + } + pid_t myppid = getppid(); pid_t mypid = getpid(); diff --git a/src/output.c b/src/output.c index c4a2d27..4473ca5 100644 --- a/src/output.c +++ b/src/output.c @@ -24,7 +24,19 @@ char *color(const char *colorstr) { (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr)); else if (output_format == O_XMOBAR) (void)snprintf(colorbuf, sizeof(colorbuf), "", cfg_getstr(cfg_general, colorstr)); - + else if (output_format == O_TERM) { + /* The escape-sequence for color is ;1m (bright/bold + * output), where col is a 3-bit rgb-value with b in the + * least-significant bit. We round the given color to the + * nearist 3-bit-depth color and output the escape-sequence */ + char *str = cfg_getstr(cfg_general, colorstr); + int col = strtol(str + 1, NULL, 16); + int r = (col & (0xFF << 0)) / 0x80; + int g = (col & (0xFF << 8)) / 0x8000; + int b = (col & (0xFF << 16)) / 0x800000; + col = (r << 2) | (g << 1) | b; + (void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col); + } return colorbuf; } @@ -35,6 +47,8 @@ char *color(const char *colorstr) { char *endcolor(void) { if (output_format == O_XMOBAR) return ""; + else if (output_format == O_TERM) + return "\033[0m"; else return ""; } @@ -43,6 +57,15 @@ void print_seperator(void) { printf("^fg(%s)^p(5;-2)^ro(2)^p()^fg()^p(5)", cfg_getstr(cfg_general, "color_separator")); else if (output_format == O_XMOBAR) printf(" | ", cfg_getstr(cfg_general, "color_separator")); + else if (output_format == O_TERM) + printf(" %s|%s ", color("color_separator"), endcolor()); else if (output_format == O_NONE) printf(" | "); } + +/* + * The term-output hides the cursor. We call this on exit to reset that. + */ +void reset_cursor(void) { + printf("\033[?25h"); +} -- cgit v1.2.3