swaybar: fix whitespace

This commit is contained in:
progandy 2015-12-22 17:26:40 +01:00
parent 70b24fbb1f
commit d37169a927

View file

@ -46,13 +46,13 @@ struct workspace {
}; };
struct status_block { struct status_block {
char *full_text, *short_text, *align; char *full_text, *short_text, *align;
bool urgent; bool urgent;
uint32_t color; uint32_t color;
int min_width; int min_width;
char *name, *instance; char *name, *instance;
bool separator; bool separator;
int separator_block_width; int separator_block_width;
}; };
list_t *status_line = NULL; list_t *status_line = NULL;
@ -375,20 +375,20 @@ void render() {
cairo_set_source_u32(window->cairo, colors.statusline); cairo_set_source_u32(window->cairo, colors.statusline);
int width, height; int width, height;
if (status_line) { if (status_line) {
int i; int i;
int moved = 0; int moved = 0;
for ( i = status_line->length - 1; i >= 0; --i ) { for ( i = status_line->length - 1; i >= 0; --i ) {
struct status_block *block = status_line->items[i]; struct status_block *block = status_line->items[i];
if (block->full_text) { if (block->full_text) {
get_text_size(window, &width, &height, "%s", block->full_text); get_text_size(window, &width, &height, "%s", block->full_text);
moved += width + block->separator_block_width; moved += width + block->separator_block_width;
cairo_move_to(window->cairo, window->width - margin - moved, margin); cairo_move_to(window->cairo, window->width - margin - moved, margin);
cairo_set_source_u32(window->cairo, block->color); cairo_set_source_u32(window->cairo, block->color);
pango_printf(window, "%s", block->full_text); pango_printf(window, "%s", block->full_text);
} }
} }
} }
// Workspaces // Workspaces
cairo_set_line_width(window->cairo, 1.0); cairo_set_line_width(window->cairo, 1.0);
@ -425,124 +425,118 @@ void render() {
} }
void parse_json(const char *text) { void parse_json(const char *text) {
/* the array of objects looks like this:
/* * [ {
* { * "full_text": "E: 10.0.0.1 (1000 Mbit/s)",
"full_text": "E: 10.0.0.1 (1000 Mbit/s)", * "short_text": "10.0.0.1",
"short_text": "10.0.0.1", * "color": "#00ff00",
"color": "#00ff00", * "min_width": 300,
"min_width": 300, * "align": "right",
"align": "right", * "urgent": false,
"urgent": false, * "name": "ethernet",
"name": "ethernet", * "instance": "eth0",
"instance": "eth0", * "separator": true,
"separator": true, * "separator_block_width": 9
"separator_block_width": 9 * },
} * { ... }, ...
* * ]
* */
*
* */
json_object *results = json_tokener_parse(text); json_object *results = json_tokener_parse(text);
if (!results) { if (!results) {
sway_log(L_DEBUG, "xxx Failed to parse json"); sway_log(L_DEBUG, "xxx Failed to parse json");
return; return;
} }
if (json_object_array_length(results) < 1) { if (json_object_array_length(results) < 1) {
return; return;
} }
if (status_line) { if (status_line) {
free_flat_list(status_line); free_flat_list(status_line);
} }
status_line = create_list(); status_line = create_list();
int i; int i;
for (i = 0; i < json_object_array_length(results); ++i) { for (i = 0; i < json_object_array_length(results); ++i) {
json_object *full_text, *short_text, *color, *min_width, *align, *urgent; json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
json_object *name, *instance, *separator, *separator_block_width; json_object *name, *instance, *separator, *separator_block_width;
json_object *json = json_object_array_get_idx(results, i); json_object *json = json_object_array_get_idx(results, i);
if (!json) {
continue;
}
if (!json) { json_object_object_get_ex(json, "full_text", &full_text);
continue; json_object_object_get_ex(json, "short_text", &short_text);
} json_object_object_get_ex(json, "color", &color);
json_object_object_get_ex(json, "min_width", &min_width);
json_object_object_get_ex(json, "align", &align);
json_object_object_get_ex(json, "urgent", &urgent);
json_object_object_get_ex(json, "name", &name);
json_object_object_get_ex(json, "instance", &instance);
json_object_object_get_ex(json, "separator", &separator);
json_object_object_get_ex(json, "separator_block_width", &separator_block_width);
json_object_object_get_ex(json, "full_text", &full_text); struct status_block *new = malloc(sizeof(struct status_block));
json_object_object_get_ex(json, "short_text", &short_text); memset(new, 0, sizeof(struct status_block));
json_object_object_get_ex(json, "color", &color);
json_object_object_get_ex(json, "min_width", &min_width);
json_object_object_get_ex(json, "align", &align);
json_object_object_get_ex(json, "urgent", &urgent);
json_object_object_get_ex(json, "name", &name);
json_object_object_get_ex(json, "instance", &instance);
json_object_object_get_ex(json, "separator", &separator);
json_object_object_get_ex(json, "separator_block_width", &separator_block_width);
struct status_block *new = malloc(sizeof(struct status_block)); if (full_text) {
memset(new, 0, sizeof(struct status_block)); new->full_text = strdup(json_object_get_string(full_text));
}
if (full_text) { if (short_text) {
new->full_text = strdup(json_object_get_string(full_text)); new->short_text = strdup(json_object_get_string(short_text));
} }
if (short_text) { if (color) {
new->short_text = strdup(json_object_get_string(short_text)); new->color = parse_color(json_object_get_string(color));
} }
else {
new->color = 0xFFFFFFFF;
}
if (color) { if (min_width) {
new->color = parse_color(json_object_get_string(color)); new->min_width = json_object_get_int(min_width);
} }
else {
new->color = 0xFFFFFFFF;
}
if (min_width) { if (align) {
new->min_width = json_object_get_int(min_width); new->align = strdup(json_object_get_string(align));
} }
else {
new->align = strdup("left");
}
if (align) { if (urgent) {
new->align = strdup(json_object_get_string(align)); new->urgent = json_object_get_int(urgent);
} }
else {
new->align = strdup("left");
}
if (urgent) { if (name) {
new->urgent = json_object_get_int(urgent); new->name = strdup(json_object_get_string(name));
} }
if (name) { if (instance) {
new->name = strdup(json_object_get_string(name)); new->instance = strdup(json_object_get_string(instance));
} }
if (instance) { if (separator) {
new->instance = strdup(json_object_get_string(instance)); new->separator = json_object_get_int(separator);
} }
else {
new->separator = true; // i3bar spec
}
if (separator) { if (separator_block_width) {
new->separator = json_object_get_int(separator); new->separator_block_width = json_object_get_int(separator_block_width);
} }
else { else {
new->separator = true; // i3bar spec new->separator_block_width = 9; // i3bar spec
} }
if (separator_block_width) {
new->separator_block_width = json_object_get_int(separator_block_width);
}
else {
new->separator_block_width = 9; // i3bar spec
}
list_add(status_line, new); list_add(status_line, new);
}
}
json_object_put(results); json_object_put(results);
} }
void poll_for_update() { void poll_for_update() {
@ -581,16 +575,16 @@ void poll_for_update() {
if (status_command && FD_ISSET(pipefd[0], &readfds)) { if (status_command && FD_ISSET(pipefd[0], &readfds)) {
sway_log(L_DEBUG, "Got update from status command."); sway_log(L_DEBUG, "Got update from status command.");
fgets(line, sizeof(line), command); fgets(line, sizeof(line), command);
sway_log(L_DEBUG, "zzz %s", line); sway_log(L_DEBUG, "zzz %s", line);
int l = strlen(line) - 1; int l = strlen(line) - 1;
if (line[l] == '\n') { if (line[l] == '\n') {
line[l] = '\0'; line[l] = '\0';
} }
if (line[0] == ',') { if (line[0] == ',') {
line[0] = ' '; line[0] = ' ';
} }
dirty = true; dirty = true;
parse_json(line); parse_json(line);
} }
} }
} }