-// vim:ts=8:expandtab
+// vim:ts=4:sw=4:expandtab
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "i3status.h"
/*
- * Writes an errormessage to statusbar
- *
- */
-void write_error_to_statusbar(const char *message) {
- cleanup_rbar_dir();
- create_file("error");
- write_to_statusbar("error", message, true);
-}
-
-/*
- * Returns the correct color format for dzen (^fg(color)) or wmii (color <normcolors>)
+ * Returns the correct color format for dzen (^fg(color)), xmobar (<fc=color>)
+ * or lemonbar (%{Fcolor})
*
*/
char *color(const char *colorstr) {
- static char colorbuf[32];
- if (!use_colors) {
- colorbuf[0] = '\0';
- return colorbuf;
- }
-#ifdef DZEN
- (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
-#elif XMOBAR
- (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", colorstr);
-#else
- (void)snprintf(colorbuf, sizeof(colorbuf), "%s %s ", colorstr, wmii_normcolors);
-#endif
+ static char colorbuf[32];
+ if (!cfg_getbool(cfg_general, "colors")) {
+ colorbuf[0] = '\0';
return colorbuf;
+ }
+ if (output_format == O_DZEN2)
+ (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
+ else if (output_format == O_XMOBAR)
+ (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
+ else if (output_format == O_LEMONBAR)
+ (void)snprintf(colorbuf, sizeof(colorbuf), "%%{F%s}", cfg_getstr(cfg_general, colorstr));
+ else if (output_format == O_TERM) {
+ /* The escape-sequence for color is <CSI><col>;1m (bright/bold
+ * output), where col is a 3-bit rgb-value with b in the
+ * least-significant bit. We round the given color to the
+ * nearist 3-bit-depth color and output the escape-sequence */
+ char *str = cfg_getstr(cfg_general, colorstr);
+ int col = strtol(str + 1, NULL, 16);
+ int r = (col & (0xFF << 0)) / 0x80;
+ int g = (col & (0xFF << 8)) / 0x8000;
+ int b = (col & (0xFF << 16)) / 0x800000;
+ col = (r << 2) | (g << 1) | b;
+ (void)snprintf(colorbuf, sizeof(colorbuf), "\033[3%d;1m", col);
+ }
+ return colorbuf;
}
/*
* Some color formats (xmobar) require to terminate colors again
*
*/
-char *endcolor() {
-#ifdef XMOBAR
+char *endcolor(void) {
+ if (output_format == O_XMOBAR)
return "</fc>";
-#else
+ else if (output_format == O_TERM)
+ return "\033[0m";
+ else
return "";
-#endif
}
-/*
- * Cleans wmii's /rbar directory by deleting all regular files
- *
- */
-void cleanup_rbar_dir() {
-#if defined(DZEN) || defined(XMOBAR)
- return;
-#endif
- struct dirent *ent;
- DIR *dir;
- char pathbuf[strlen(wmii_path)+256+1];
-
- if ((dir = opendir(wmii_path)) == NULL)
- exit(EXIT_FAILURE);
-
- while ((ent = readdir(dir)) != NULL) {
- if (ent->d_type == DT_REG) {
- (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, ent->d_name);
- if (unlink(pathbuf) == -1)
- exit(EXIT_FAILURE);
- }
- }
-
- (void)closedir(dir);
-}
-
-/*
- * Creates the specified file in wmii's /rbar directory with
- * correct modes and initializes colors if colormode is enabled
- *
- */
-void create_file(const char *name) {
-#if defined(DZEN) || defined(XMOBAR)
+void print_separator(const char *separator) {
+ if (output_format == O_I3BAR || strlen(separator) == 0)
return;
-#endif
- char pathbuf[strlen(wmii_path)+256+1];
- int fd;
- int flags = O_CREAT | O_WRONLY;
- struct stat statbuf;
-
- (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
-
- /* Overwrite file's contents if it exists */
- if (stat(pathbuf, &statbuf) >= 0)
- flags |= O_TRUNC;
- if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
- exit(EXIT_FAILURE);
- if (use_colors) {
- char *tmp = color("#888888");
- if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
- exit(EXIT_FAILURE);
- }
- (void)close(fd);
+ if (output_format == O_DZEN2)
+ printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator);
+ else if (output_format == O_XMOBAR)
+ printf("<fc=%s>%s</fc>", cfg_getstr(cfg_general, "color_separator"), separator);
+ else if (output_format == O_LEMONBAR)
+ printf("%%{F%s}%s%%{F-}", cfg_getstr(cfg_general, "color_separator"), separator);
+ else if (output_format == O_TERM)
+ printf("%s%s%s", color("color_separator"), separator, endcolor());
+ else if (output_format == O_NONE)
+ printf("%s", separator);
}
/*
- * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
- * cleans up all files and creates the needed files
- *
+ * The term-output hides the cursor. We call this on exit to reset that.
*/
-void setup(void) {
- unsigned int i;
- char pathbuf[512];
-
-#if !defined(DZEN) && !defined(XMOBAR)
- struct stat statbuf;
- /* Wait until wmii_path/rbar exists */
- for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
-#endif
-#define cf(orderidx, name) create_file(order_to_str(order[orderidx], name));
-
- cleanup_rbar_dir();
- if (wlan_interface)
- cf(ORDER_WLAN, "wlan");
- if (eth_interface)
- cf(ORDER_ETH, "eth");
- if (get_cpu_temperature)
- cf(ORDER_CPU_TEMPERATURE, "cpu_temperature");
- cf(ORDER_LOAD, "load");
- if (time_format)
- cf(ORDER_TIME, "time");
- for (i = 0; i < num_run_watches; i += 2) {
- snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
- create_file(pathbuf);
- }
+void reset_cursor(void) {
+ printf("\033[?25h");
}
/*
- * Writes the given message in the corresponding file in wmii's /rbar directory
+ * Escapes ampersand, less-than, greater-than, single-quote, and double-quote
+ * characters with the corresponding Pango markup strings if markup is enabled.
+ * See the glib implementation:
+ * https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142
*
*/
-void write_to_statusbar(const char *name, const char *message, bool final_entry) {
-#ifdef DZEN
- if (final_entry) {
- if (printf("%s^p(6)\n", message) < 0) {
- perror("printf");
- exit(1);
- }
-
- fflush(stdout);
- return;
- }
- if (printf("%s" BAR, message) < 0) {
- perror("printf");
- exit(1);
- }
+void maybe_escape_markup(char *text, char **buffer) {
+ if (markup_format == M_NONE) {
+ *buffer += sprintf(*buffer, "%s", text);
return;
-#elif XMOBAR
- if (final_entry) {
- if (printf("%s\n", message) < 0) {
- perror("printf");
- exit(1);
+ }
+ for (; *text != '\0'; text++) {
+ switch (*text) {
+ case '&':
+ *buffer += sprintf(*buffer, "%s", "&");
+ break;
+ case '<':
+ *buffer += sprintf(*buffer, "%s", "<");
+ break;
+ case '>':
+ *buffer += sprintf(*buffer, "%s", ">");
+ break;
+ case '\'':
+ *buffer += sprintf(*buffer, "%s", "'");
+ break;
+ case '"':
+ *buffer += sprintf(*buffer, "%s", """);
+ break;
+ default:
+ if ((0x1 <= *text && *text <= 0x8) ||
+ (0xb <= *text && *text <= 0xc) ||
+ (0xe <= *text && *text <= 0x1f)) {
+ *buffer += sprintf(*buffer, "&#x%x;", *text);
+ } else {
+ *(*buffer)++ = *text;
}
-
- fflush(stdout);
- return;
- }
- if (printf("%s" BAR, message) < 0) {
- perror("printf");
- exit(1);
- }
- return;
-
-#endif
-
- char pathbuf[strlen(wmii_path)+256+1];
- int fd;
-
- (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
- if ((fd = open(pathbuf, O_RDWR)) == -1) {
- /* Try to re-setup stuff and just continue */
- setup();
- return;
+ break;
}
- if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
- exit(EXIT_FAILURE);
- (void)close(fd);
+ }
}