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