i3 - improved tiling WM


i3status: Allow customization of module separator

Patch status: merged

Patch by Marco Hunsicker

Long description:

This patch adds the ability to customize the separator that is placed
between modules.

Specifically this patch:

* adds the "separator" general directive
* moves the definition of the default separator for the different
  output formats (excluding color formatting) to src/i3status.c
* updates the SEC_CLOSE_MAP macro to disable the separator for the
  i3bar output format if the separator directive dictates so
* changes print_seperator() in src/output.c to take a separator
  parameter in order to disable the output of the separator if
  the separator is empty and to use the provided separator
  otherwise
* updates the manpage to explain the new directive

To apply this patch, use:
curl http://cr.i3wm.org/patch/440/raw.patch | git am

b/i3status.c

31
@@ -191,12 +191,25 @@ static char *get_config_path(void) {
32
         return NULL;
33
 }
34
 
35
+/*
36
+ * Returns the default separator to use if no custom separator has been specified.
37
+ */
38
+static char *get_default_separator() {
39
+        if (output_format == O_DZEN2)
40
+                return "^p(5;-2)^ro(2)^p()^p(5)";
41
+        if (output_format == O_I3BAR)
42
+                // anything besides the empty string indicates that the default separator should be used
43
+                return "default";
44
+        return " | ";
45
+}
46
+
47
 int main(int argc, char *argv[]) {
48
         unsigned int j;
49
 
50
         cfg_opt_t general_opts[] = {
51
                 CFG_STR("output_format", "auto", CFGF_NONE),
52
                 CFG_BOOL("colors", 1, CFGF_NONE),
53
+                CFG_STR("separator", "default", CFGF_NONE),
54
                 CFG_STR("color_separator", "#333333", CFGF_NONE),
55
                 CFG_INT("interval", 1, CFGF_NONE),
56
                 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
57
@@ -403,6 +416,12 @@ int main(int argc, char *argv[]) {
58
                 output_format = O_NONE;
59
         else die("Unknown output format: \"%s\"\n", output_str);
60
 
61
+        const char *separator = cfg_getstr(cfg_general, "separator");
62
+
63
+        // if no custom separator has been provided, use the default one
64
+        if (strcasecmp(separator, "default") == 0)
65
+                separator = get_default_separator();
66
+
67
         if (!valid_color(cfg_getstr(cfg_general, "color_good"))
68
                         || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
69
                         || !valid_color(cfg_getstr(cfg_general, "color_bad"))
70
@@ -457,7 +476,7 @@ int main(int argc, char *argv[]) {
71
                         printf("\033[u\033[K");
72
                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
73
                         if (j > 0)
74
-                                print_seperator();
75
+                                print_seperator(separator);
76
 
77
                         const char *current = cfg_getnstr(cfg, "order", j);
78
 

b/include/i3status.h

83
@@ -88,6 +88,11 @@ enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
84
 #define SEC_CLOSE_MAP \
85
 	do { \
86
 		if (output_format == O_I3BAR) { \
87
+			const char *_sep = cfg_getstr(cfg_general, "separator"); \
88
+			if (strlen(_sep) == 0) {\
89
+				yajl_gen_string(json_gen, (const unsigned char *)"separator", strlen("separator")); \
90
+				yajl_gen_bool(json_gen, false); \
91
+			} \
92
 			yajl_gen_map_close(json_gen); \
93
 		} \
94
 	} while (0)
95
@@ -133,7 +138,7 @@ void die(const char *fmt, ...);
96
 bool slurp(const char *filename, char *destination, int size);
97
 
98
 /* src/output.c */
99
-void print_seperator();
100
+void print_seperator(const char *separator);
101
 char *color(const char *colorstr);
102
 char *endcolor() __attribute__ ((pure));
103
 void reset_cursor(void);

b/man/i3status.man

108
@@ -160,14 +160,41 @@ easier because the terminal-output of i3status becomes much more readable, but
109
 should only used for such quick glances, because it will only support very
110
 basic output-features (for example you only get 3 bits of color depth).
111
 none::
112
-Does not use any color codes. Separates values by the pipe symbol. This should
113
-be used with i3bar and can be used for custom scripts.
114
+Does not use any color codes. Separates values by the pipe symbol by default.
115
+This should be used with i3bar and can be used for custom scripts.
116
 
117
 It's also possible to use the color_good, color_degraded, color_bad directives
118
 to define specific colors per module. If one of these directives is defined
119
 in a module section its value will override the value defined in the general
120
 section just for this module.
121
 
122
+If you don't fancy the vertical separators between modules i3status/i3bar
123
+uses by default, you can employ the +separator+ directive to configure how
124
+modules are separated. You can either disable the default separator altogether
125
+setting it to the empty string. You might then define separation as part of a
126
+module's format string. This is your only option when using the i3bar output
127
+format as the separator is drawn by i3bar directly otherwise. For the other
128
+output formats, the provided non-empty string will be automatically enclosed
129
+with the necessary coloring bits if color support is enabled.
130
+
131
+*Example configuration*:
132
+-------------------------------------------------------------
133
+general {
134
+    output_format = "xmobar"
135
+    separator = "  "
136
+}
137
+
138
+order += "load"
139
+order += "disk /"
140
+
141
+load {
142
+    format = "[ load: %1min, %5min, %15min ]"
143
+}
144
+disk "/" {
145
+    format = "%avail"
146
+}
147
+-------------------------------------------------------------
148
+
149
 === IPv6
150
 
151
 This module gets the IPv6 address used for outgoing connections (that is, the

b/src/output.c

156
@@ -52,15 +52,18 @@ char *endcolor(void) {
157
         else return "";
158
 }
159
 
160
-void print_seperator(void) {
161
+void print_seperator(const char *separator) {
162
+        if (output_format == O_I3BAR || strlen(separator) == 0)
163
+                return;
164
+
165
         if (output_format == O_DZEN2)
166
-                printf("^fg(%s)^p(5;-2)^ro(2)^p()^fg()^p(5)", cfg_getstr(cfg_general, "color_separator"));
167
+                printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
168
         else if (output_format == O_XMOBAR)
169
-                printf("<fc=%s> | </fc>", cfg_getstr(cfg_general, "color_separator"));
170
+                printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
171
         else if (output_format == O_TERM)
172
-                printf(" %s|%s ", color("color_separator"), endcolor());
173
+                printf("%s%s%s", color("color_separator"), separator, endcolor());
174
         else if (output_format == O_NONE)
175
-                printf(" | ");
176
+                printf("%s", separator);
177
 }
178
 
179
 /*