summaryrefslogtreecommitdiff
path: root/src/output.c
blob: 4473ca505fdf744317d3df3132231d13ee7ec3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// vim:ts=8:expandtab
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>

#include "i3status.h"

/*
 * Returns the correct color format for dzen (^fg(color)) or xmobar (<fc=color>)
 *
 */
char *color(const char *colorstr) {
        static char colorbuf[32];
        if (!cfg_getbool(cfg_general, "colors")) {
                colorbuf[0] = '\0';
                return colorbuf;
        }
        if (output_format == O_DZEN2)
                (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
        else if (output_format == O_XMOBAR)
                (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
        else if (output_format == O_TERM) {
                /* The escape-sequence for color is <CSI><col>;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;
}

/*
 * Some color formats (xmobar) require to terminate colors again
 *
 */
char *endcolor(void) {
        if (output_format == O_XMOBAR)
                return "</fc>";
        else if (output_format == O_TERM)
                return "\033[0m";
        else return "";
}

void print_seperator(void) {
        if (output_format == O_DZEN2)
                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("<fc=%s> | </fc>", 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");
}