1 // vim:ts=4:sw=4:expandtab
14 * Returns the correct color format for dzen (^fg(color)), xmobar (<fc=color>)
15 * or lemonbar (%{Fcolor})
18 char *color(const char *colorstr) {
19 static char colorbuf[32];
20 if (!cfg_getbool(cfg_general, "colors")) {
24 if (output_format == O_DZEN2)
25 (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
26 else if (output_format == O_XMOBAR)
27 (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
28 else if (output_format == O_LEMONBAR)
29 (void)snprintf(colorbuf, sizeof(colorbuf), "%%{F%s}", cfg_getstr(cfg_general, colorstr));
30 else if (output_format == O_TERM) {
31 /* The escape-sequence for color is <CSI><col>;1m (bright/bold
32 * output), where col is a 3-bit rgb-value with b in the
33 * least-significant bit. We round the given color to the
34 * nearist 3-bit-depth color and output the escape-sequence */
35 char *str = cfg_getstr(cfg_general, colorstr);
36 int col = strtol(str + 1, NULL, 16);
37 int r = (col & (0xFF << 0)) / 0x80;
38 int g = (col & (0xFF << 8)) / 0x8000;
39 int b = (col & (0xFF << 16)) / 0x800000;
40 col = (r << 2) | (g << 1) | b;
41 (void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col);
47 * Some color formats (xmobar) require to terminate colors again
50 char *endcolor(void) {
51 if (output_format == O_XMOBAR)
53 else if (output_format == O_TERM)
59 void print_separator(const char *separator) {
60 if (output_format == O_I3BAR || strlen(separator) == 0)
63 if (output_format == O_DZEN2)
64 printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
65 else if (output_format == O_XMOBAR)
66 printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
67 else if (output_format == O_LEMONBAR)
68 printf("%%{F%s}%s%%{F-}", cfg_getstr(cfg_general, "color_separator"), separator);
69 else if (output_format == O_TERM)
70 printf("%s%s%s", color("color_separator"), separator, endcolor());
71 else if (output_format == O_NONE)
72 printf("%s", separator);
76 * The term-output hides the cursor. We call this on exit to reset that.
78 void reset_cursor(void) {
83 * Escapes ampersand, less-than, greater-than, single-quote, and double-quote
84 * characters with the corresponding Pango markup strings if markup is enabled.
85 * See the glib implementation:
86 * https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142
89 void maybe_escape_markup(char *text, char **buffer) {
90 if (markup_format == M_NONE) {
91 *buffer += sprintf(*buffer, "%s", text);
94 for (; *text != '\0'; text++) {
97 *buffer += sprintf(*buffer, "%s", "&");
100 *buffer += sprintf(*buffer, "%s", "<");
103 *buffer += sprintf(*buffer, "%s", ">");
106 *buffer += sprintf(*buffer, "%s", "'");
109 *buffer += sprintf(*buffer, "%s", """);
112 if ((0x1 <= *text && *text <= 0x8) ||
113 (0xb <= *text && *text <= 0xc) ||
114 (0xe <= *text && *text <= 0x1f)) {
115 *buffer += sprintf(*buffer, "&#x%x;", *text);
117 *(*buffer)++ = *text;