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