]> git.sur5r.net Git - i3/i3status/blob - src/output.c
e56c89253a90e095a8c856a037caf1099b7a17e8
[i3/i3status] / src / output.c
1 // vim:ts=8: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)) or xmobar (<fc=color>)
15  *
16  */
17 char *color(const char *colorstr) {
18         static char colorbuf[32];
19         if (!cfg_getbool(cfg_general, "colors")) {
20                 colorbuf[0] = '\0';
21                 return colorbuf;
22         }
23         if (output_format == O_DZEN2)
24                 (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
25         else if (output_format == O_XMOBAR)
26                 (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
27         else if (output_format == O_TERM) {
28                 /* The escape-sequence for color is <CSI><col>;1m (bright/bold
29                  * output), where col is a 3-bit rgb-value with b in the
30                  * least-significant bit. We round the given color to the
31                  * nearist 3-bit-depth color and output the escape-sequence */
32                 char *str = cfg_getstr(cfg_general, colorstr);
33                 int col = strtol(str + 1, NULL, 16);
34                 int r = (col & (0xFF << 0)) / 0x80;
35                 int g = (col & (0xFF << 8)) / 0x8000;
36                 int b = (col & (0xFF << 16)) / 0x800000;
37                 col = (r << 2) | (g << 1) | b;
38                 (void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col);
39         }
40         return colorbuf;
41 }
42
43 /*
44  * Some color formats (xmobar) require to terminate colors again
45  *
46  */
47 char *endcolor(void) {
48         if (output_format == O_XMOBAR)
49                 return "</fc>";
50         else if (output_format == O_TERM)
51                 return "\033[0m";
52         else return "";
53 }
54
55 void print_seperator(const char *separator) {
56         if (output_format == O_I3BAR || strlen(separator) == 0)
57                 return;
58
59         if (output_format == O_DZEN2)
60                 printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
61         else if (output_format == O_XMOBAR)
62                 printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
63         else if (output_format == O_TERM)
64                 printf("%s%s%s", color("color_separator"), separator, endcolor());
65         else if (output_format == O_NONE)
66                 printf("%s", separator);
67 }
68
69 /*
70  * The term-output hides the cursor. We call this on exit to reset that.
71  */
72 void reset_cursor(void) {
73         printf("\033[?25h");
74 }