]> git.sur5r.net Git - i3/i3status/blob - src/output.c
Added support for Pango markup.
[i3/i3status] / src / output.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdbool.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <dirent.h>
10
11 #include "i3status.h"
12
13 /*
14  * Returns the correct color format for dzen (^fg(color)), xmobar (<fc=color>)
15  * or lemonbar (%{Fcolor})
16  *
17  */
18 char *color(const char *colorstr) {
19     static char colorbuf[32];
20     if (!cfg_getbool(cfg_general, "colors")) {
21         colorbuf[0] = '\0';
22         return colorbuf;
23     }
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);
42     }
43     return colorbuf;
44 }
45
46 /*
47  * Some color formats (xmobar) require to terminate colors again
48  *
49  */
50 char *endcolor(void) {
51     if (output_format == O_XMOBAR)
52         return "</fc>";
53     else if (output_format == O_TERM)
54         return "\033[0m";
55     else
56         return "";
57 }
58
59 void print_separator(const char *separator) {
60     if (output_format == O_I3BAR || strlen(separator) == 0)
61         return;
62
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);
73 }
74
75 /*
76  * The term-output hides the cursor. We call this on exit to reset that.
77  */
78 void reset_cursor(void) {
79     printf("\033[?25h");
80 }
81
82 /*
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
87  *
88  */
89 void maybe_escape_markup(char *text, char **buffer) {
90     if (markup_format == M_NONE) {
91         *buffer += sprintf(*buffer, "%s", text);
92         return;
93     }
94     for (; *text != '\0'; text++) {
95         switch (*text) {
96             case '&':
97                 *buffer += sprintf(*buffer, "%s", "&amp;");
98                 break;
99             case '<':
100                 *buffer += sprintf(*buffer, "%s", "&lt;");
101                 break;
102             case '>':
103                 *buffer += sprintf(*buffer, "%s", "&gt;");
104                 break;
105             case '\'':
106                 *buffer += sprintf(*buffer, "%s", "&apos;");
107                 break;
108             case '"':
109                 *buffer += sprintf(*buffer, "%s", "&quot;");
110                 break;
111             default:
112                 if ((0x1 <= *text && *text <= 0x8) ||
113                     (0xb <= *text && *text <= 0xc) ||
114                     (0xe <= *text && *text <= 0x1f) ||
115                     (0x7f <= *text && *text <= 0x84) ||
116                     (0x86 <= *text && *text <= 0x9f))
117                     *buffer += sprintf(*buffer, "&#x%x;", *text);
118                 else
119                     *(*buffer)++ = *text;
120                 break;
121         }
122     }
123 }