cfg_t *cfg, *cfg_general, *cfg_section;
/*
- * Exit upon SIGPIPE because when we have nowhere to write to, gathering
- * system information is pointless.
+ * Exit upon SIGPIPE because when we have nowhere to write to, gathering system
+ * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
+ * only this will trigger a reset of the cursor in the terminal output-format.
*
*/
-void sigpipe(int signum) {
- fprintf(stderr, "Received SIGPIPE, exiting\n");
+void fatalsig(int signum) {
+ fprintf(stderr, "Received SIG%s, exiting\n", signum == SIGPIPE ? "PIPE" :
+ signum == SIGTERM ? "TERM" :
+ "INT");
exit(1);
}
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
- action.sa_handler = sigpipe;
+ action.sa_handler = fatalsig;
sigaction(SIGPIPE, &action, NULL);
+ sigaction(SIGTERM, &action, NULL);
+ sigaction(SIGINT, &action, NULL);
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = sigusr1;
output_format = O_XMOBAR;
else if (strcasecmp(output_str, "i3bar") == 0)
output_format = O_I3BAR;
+ else if (strcasecmp(output_str, "term") == 0)
+ output_format = O_TERM;
else if (strcasecmp(output_str, "none") == 0)
output_format = O_NONE;
else die("Unknown output format: \"%s\"\n", output_str);
yajl_gen_array_open(json_gen);
yajl_gen_clear(json_gen);
}
+ if (output_format == O_TERM) {
+ /* Save the cursor-position and hide the cursor */
+ printf("\033[s\033[?25l");
+ /* Undo at exit */
+ atexit(&reset_cursor);
+ }
if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
die("Could not create socket\n");
gettimeofday(&tv, NULL);
if (output_format == O_I3BAR)
yajl_gen_array_open(json_gen);
+ else if (output_format == O_TERM)
+ /* Restore the cursor-position */
+ printf("\033[u");
for (j = 0; j < cfg_size(cfg, "order"); j++) {
if (j > 0)
print_seperator();
#ifndef _I3STATUS_H
#define _I3STATUS_H
-enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_NONE } output_format;
+enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
#include <stdbool.h>
#include <confuse.h>
void print_seperator();
char *color(const char *colorstr);
char *endcolor() __attribute__ ((pure));
+void reset_cursor(void);
/* src/auto_detect_format.c */
char *auto_detect_format();
xmobar::
xmobar is a minimalistic, text based, status bar. It was designed to work
with the xmonad Window Manager.
+term::
+Use ANSI Escape sequences to produce a terminal-output as close as possible to
+the graphical outputs. This makes debugging your config file a little bit
+easier because the terminal-output of i3status becomes much more readable, but
+should only used for such quick glances, because it will only support very
+basic output-features (for example you only get 3 bits of color depth).
none::
Does not use any color codes. Separates values by the pipe symbol. This should
be used with i3bar and can be used for custom scripts.
*
*/
char *auto_detect_format(void) {
+ /* If stdout is a tty, we output directly to a terminal. */
+ if (isatty(STDOUT_FILENO)) {
+ return "term";
+ }
+
pid_t myppid = getppid();
pid_t mypid = getpid();
(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_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;
}
char *endcolor(void) {
if (output_format == O_XMOBAR)
return "</fc>";
+ else if (output_format == O_TERM)
+ return "\033[0m";
else return "";
}
printf("^fg(%s)^p(5;-2)^ro(2)^p()^fg()^p(5)", cfg_getstr(cfg_general, "color_separator"));
else if (output_format == O_XMOBAR)
printf("<fc=%s> | </fc>", cfg_getstr(cfg_general, "color_separator"));
+ else if (output_format == O_TERM)
+ printf(" %s|%s ", color("color_separator"), endcolor());
else if (output_format == O_NONE)
printf(" | ");
}
+
+/*
+ * The term-output hides the cursor. We call this on exit to reset that.
+ */
+void reset_cursor(void) {
+ printf("\033[?25h");
+}