X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Foutput.c;h=663700e691512d6e3b89d2834adc384d4697206d;hb=302966374bd7d9ca5b97171cedd3613ae10360c8;hp=3a43d582e4599d55444079077b98baf9082abfd4;hpb=0c2b518b8a30c08a69c07ab6ffeea98eb2d58424;p=i3%2Fi3status diff --git a/src/output.c b/src/output.c index 3a43d58..663700e 100644 --- a/src/output.c +++ b/src/output.c @@ -1,4 +1,4 @@ -// vim:ts=8:expandtab +// vim:ts=4:sw=4:expandtab #include #include #include @@ -11,33 +11,36 @@ #include "i3status.h" /* - * Returns the correct color format for dzen (^fg(color)) or xmobar () + * Returns the correct color format for dzen (^fg(color)), xmobar () + * or lemonbar (%{Fcolor}) * */ char *color(const char *colorstr) { - 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), "", cfg_getstr(cfg_general, colorstr)); - else if (output_format == O_TERM) { - /* The escape-sequence for color is ;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); - } + 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), "", 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 ;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; } /* @@ -45,30 +48,75 @@ char *color(const char *colorstr) { * */ char *endcolor(void) { - if (output_format == O_XMOBAR) - return ""; - else if (output_format == O_TERM) - return "\033[0m"; - else return ""; + if (output_format == O_XMOBAR) + return ""; + else if (output_format == O_TERM) + return "\033[0m"; + else + return ""; } void print_separator(const char *separator) { - if (output_format == O_I3BAR || strlen(separator) == 0) - return; + if (output_format == O_I3BAR || strlen(separator) == 0) + return; - if (output_format == O_DZEN2) - printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator); - else if (output_format == O_XMOBAR) - printf("%s", 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); + if (output_format == O_DZEN2) + printf("^fg(%s)%s^fg()", cfg_getstr(cfg_general, "color_separator"), separator); + else if (output_format == O_XMOBAR) + printf("%s", 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); } /* * The term-output hides the cursor. We call this on exit to reset that. */ void reset_cursor(void) { - printf("\033[?25h"); + printf("\033[?25h"); +} + +/* + * 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 maybe_escape_markup(char *text, char **buffer) { + if (markup_format == M_NONE) { + *buffer += sprintf(*buffer, "%s", text); + return; + } + 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; + } + break; + } + } }