]> git.sur5r.net Git - i3/i3status/blob - src/output.c
Added support for lemonbar
[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 }