From ccdcdc339679284887778ec1eff548afdc4c5511 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 3 Jan 2016 17:33:47 +0100 Subject: [PATCH] swaybar: Implement strip_workspace_numbers --- swaybar/main.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/swaybar/main.c b/swaybar/main.c index cbedb2003..34c54287a 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -76,6 +76,7 @@ struct registry *registry; struct window *window; bool dirty = true; char *separator_symbol = NULL; +bool strip_workspace_numbers = false; typedef enum {UNDEF, TEXT, I3BAR} command_protocol; command_protocol protocol = UNDEF; @@ -289,7 +290,7 @@ void bar_ipc_init(int outputi, const char *bar_id) { json_object *bar_config = json_tokener_parse(res); json_object *tray_output, *mode, *hidden_state, *position, *_status_command; - json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers; + json_object *font, *bar_height, *workspace_buttons, *_strip_workspace_numbers; json_object *binding_mode_indicator, *verbose, *_colors, *sep_symbol; json_object_object_get_ex(bar_config, "tray_output", &tray_output); json_object_object_get_ex(bar_config, "mode", &mode); @@ -299,7 +300,7 @@ void bar_ipc_init(int outputi, const char *bar_id) { json_object_object_get_ex(bar_config, "font", &font); json_object_object_get_ex(bar_config, "bar_height", &bar_height); json_object_object_get_ex(bar_config, "workspace_buttons", &workspace_buttons); - json_object_object_get_ex(bar_config, "strip_workspace_numbers", &strip_workspace_numbers); + json_object_object_get_ex(bar_config, "strip_workspace_numbers", &_strip_workspace_numbers); json_object_object_get_ex(bar_config, "binding_mode_indicator", &binding_mode_indicator); json_object_object_get_ex(bar_config, "verbose", &verbose); json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol); @@ -323,6 +324,10 @@ void bar_ipc_init(int outputi, const char *bar_id) { separator_symbol = strdup(json_object_get_string(sep_symbol)); } + if (_strip_workspace_numbers) { + strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers); + } + if (bar_height) { int width, height; get_text_size(window, &width, &height, "Test string for measuring purposes"); @@ -567,9 +572,36 @@ void render_block(struct status_block *block, double *x, bool edge) { } +char *handle_workspace_number(bool strip_num, const char *ws_name) { + bool strip = false; + int i; + + if (strip_num) { + int len = strlen(ws_name); + for (i = 0; i < len; ++i) { + if (!('0' <= ws_name[i] && ws_name[i] <= '9')) { + if (':' == ws_name[i] && i < len-1) { + strip = true; + ++i; + } + break; + } + } + } + + if (strip) { + return strdup(ws_name + i); + } + + return strdup(ws_name); +} + void render_workspace_button(struct workspace *ws, double *x) { + // strip workspace numbers if required + char *name = handle_workspace_number(strip_workspace_numbers, ws->name); + int width, height; - get_text_size(window, &width, &height, "%s", ws->name); + get_text_size(window, &width, &height, "%s", name); struct box_colors box_colors; if (ws->urgent) { box_colors = colors.urgent_workspace; @@ -596,9 +628,11 @@ void render_workspace_button(struct workspace *ws, double *x) { // text cairo_set_source_u32(window->cairo, box_colors.text); cairo_move_to(window->cairo, (int)*x + ws_hor_padding, margin); - pango_printf(window, "%s", ws->name); + pango_printf(window, "%s", name); *x += width + ws_hor_padding * 2 + ws_spacing; + + free(name); } void render() {