From 20df36093725ea5d2764b5a9a83bebf9d6c5862c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 8 Dec 2020 19:03:30 +0100 Subject: [PATCH] =?UTF-8?q?module/i3:=20add=20option=20=E2=80=98sort?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Possible values: none, ascending, descending. Sorts the workspace list accordingly. The default value is ‘none’. --- CHANGELOG.md | 4 +++ doc/yambar-modules.5.scd | 4 +++ modules/i3.c | 59 +++++++++++++++++++++++++++++++--------- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39a557e..46ecdeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ (https://codeberg.org/dnkl/yambar/issues/11). * mpd: `volume` tag. This is a range tag that represents MPD's current volume in percentage (0-100) +* i3: `sort` configuration option, that controls how the workspace + list is sorted. Can be set to one of `none`, `ascending` or + `descending`. Default is `none` + (https://codeberg.org/dnkl/yambar/issues/17). ### Deprecated diff --git a/doc/yambar-modules.5.scd b/doc/yambar-modules.5.scd index 652d6b5..693f502 100644 --- a/doc/yambar-modules.5.scd +++ b/doc/yambar-modules.5.scd @@ -374,6 +374,10 @@ with the _application_ and _title_ tags to replace the X11-only : Unlike other modules, _content_ is an associative array mapping workspace names to particles. Use *""* to specify a default fallback particle, or *current* for the currently active workspace. +| sort +: enum +: no +: How to sort the list of workspaces; one of _none_, _ascending_ or _descending_, defaults to _none_. | left-spacing : int : no diff --git a/modules/i3.c b/modules/i3.c index 471bcf8..02bd575 100644 --- a/modules/i3.c +++ b/modules/i3.c @@ -21,6 +21,8 @@ #include "i3-ipc.h" #include "i3-common.h" +enum sort_mode {SORT_NONE, SORT_ASCENDING, SORT_DESCENDING}; + struct ws_content { char *name; struct particle *content; @@ -52,6 +54,7 @@ struct private { size_t count; } ws_content; + enum sort_mode sort_mode; tll(struct workspace) workspaces; }; @@ -105,18 +108,31 @@ workspaces_free(struct private *m) static void workspace_add(struct private *m, struct workspace ws) { -#if 1 - tll_push_back(m->workspaces, ws); -#else - tll_rforeach(m->workspaces, it) { - if (strcasecmp(it->item.name, ws.name) < 0) { - tll_insert_after(m->workspaces, it, ws); - return; - } - } + switch (m->sort_mode) { + case SORT_NONE: + tll_push_back(m->workspaces, ws); + return; - tll_push_front(m->workspaces, ws); -#endif + case SORT_ASCENDING: + tll_foreach(m->workspaces, it) { + if (strcoll(it->item.name, ws.name) > 0) { + tll_insert_before(m->workspaces, it, ws); + return; + } + } + tll_push_back(m->workspaces, ws); + return; + + case SORT_DESCENDING: + tll_foreach(m->workspaces, it) { + if (strcoll(it->item.name, ws.name) < 0) { + tll_insert_before(m->workspaces, it, ws); + return; + } + } + tll_push_back(m->workspaces, ws); + return; + } } static void @@ -600,7 +616,7 @@ struct i3_workspaces { static struct module * i3_new(struct i3_workspaces workspaces[], size_t workspace_count, - int left_spacing, int right_spacing) + int left_spacing, int right_spacing, enum sort_mode sort_mode) { struct private *m = calloc(1, sizeof(*m)); @@ -615,6 +631,8 @@ i3_new(struct i3_workspaces workspaces[], size_t workspace_count, m->ws_content.v[i].content = workspaces[i].content; } + m->sort_mode = sort_mode; + struct module *mod = module_common_new(); mod->private = m; mod->run = &run; @@ -630,12 +648,19 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) const struct yml_node *spacing = yml_get_value(node, "spacing"); const struct yml_node *left_spacing = yml_get_value(node, "left-spacing"); const struct yml_node *right_spacing = yml_get_value(node, "right-spacing"); + const struct yml_node *sort = yml_get_value(node, "sort"); int left = spacing != NULL ? yml_value_as_int(spacing) : left_spacing != NULL ? yml_value_as_int(left_spacing) : 0; int right = spacing != NULL ? yml_value_as_int(spacing) : right_spacing != NULL ? yml_value_as_int(right_spacing) : 0; + const char *sort_value = sort != NULL ? yml_value_as_string(sort) : NULL; + enum sort_mode sort_mode = + sort_value == NULL ? SORT_NONE : + strcmp(sort_value, "none") == 0 ? SORT_NONE : + strcmp(sort_value, "ascending") == 0 ? SORT_ASCENDING : SORT_DESCENDING; + struct i3_workspaces workspaces[yml_dict_length(c)]; size_t idx = 0; @@ -647,7 +672,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) workspaces[idx].content = conf_to_particle(it.value, inherited); } - return i3_new(workspaces, yml_dict_length(c), left, right); + return i3_new(workspaces, yml_dict_length(c), left, right, sort_mode); } static bool @@ -680,6 +705,13 @@ verify_content(keychain_t *chain, const struct yml_node *node) return true; } +static bool +verify_sort(keychain_t *chain, const struct yml_node *node) +{ + return conf_verify_enum( + chain, node, (const char *[]){"none", "ascending", "descending"}, 3); +} + static bool verify_conf(keychain_t *chain, const struct yml_node *node) { @@ -687,6 +719,7 @@ verify_conf(keychain_t *chain, const struct yml_node *node) {"spacing", false, &conf_verify_int}, {"left-spacing", false, &conf_verify_int}, {"right-spacing", false, &conf_verify_int}, + {"sort", false, &verify_sort}, {"content", true, &verify_content}, {"anchors", false, NULL}, {NULL, false, NULL},