Merge branch 'i3-sort-workspaces'

Closes #17
This commit is contained in:
Daniel Eklöf 2020-12-12 22:41:34 +01:00
commit ab8cc7df47
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 54 additions and 13 deletions

View file

@ -16,6 +16,10 @@
(https://codeberg.org/dnkl/yambar/issues/11). (https://codeberg.org/dnkl/yambar/issues/11).
* mpd: `volume` tag. This is a range tag that represents MPD's current * mpd: `volume` tag. This is a range tag that represents MPD's current
volume in percentage (0-100) 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 ### Deprecated

View file

@ -374,6 +374,10 @@ with the _application_ and _title_ tags to replace the X11-only
: Unlike other modules, _content_ is an associative array mapping : Unlike other modules, _content_ is an associative array mapping
workspace names to particles. Use *""* to specify a default workspace names to particles. Use *""* to specify a default
fallback particle, or *current* for the currently active workspace. 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 | left-spacing
: int : int
: no : no

View file

@ -21,6 +21,8 @@
#include "i3-ipc.h" #include "i3-ipc.h"
#include "i3-common.h" #include "i3-common.h"
enum sort_mode {SORT_NONE, SORT_ASCENDING, SORT_DESCENDING};
struct ws_content { struct ws_content {
char *name; char *name;
struct particle *content; struct particle *content;
@ -52,6 +54,7 @@ struct private {
size_t count; size_t count;
} ws_content; } ws_content;
enum sort_mode sort_mode;
tll(struct workspace) workspaces; tll(struct workspace) workspaces;
}; };
@ -105,18 +108,31 @@ workspaces_free(struct private *m)
static void static void
workspace_add(struct private *m, struct workspace ws) workspace_add(struct private *m, struct workspace ws)
{ {
#if 1 switch (m->sort_mode) {
tll_push_back(m->workspaces, ws); case SORT_NONE:
#else tll_push_back(m->workspaces, ws);
tll_rforeach(m->workspaces, it) { return;
if (strcasecmp(it->item.name, ws.name) < 0) {
tll_insert_after(m->workspaces, it, ws);
return;
}
}
tll_push_front(m->workspaces, ws); case SORT_ASCENDING:
#endif 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 static void
@ -600,7 +616,7 @@ struct i3_workspaces {
static struct module * static struct module *
i3_new(struct i3_workspaces workspaces[], size_t workspace_count, 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)); 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->ws_content.v[i].content = workspaces[i].content;
} }
m->sort_mode = sort_mode;
struct module *mod = module_common_new(); struct module *mod = module_common_new();
mod->private = m; mod->private = m;
mod->run = &run; 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 *spacing = yml_get_value(node, "spacing");
const struct yml_node *left_spacing = yml_get_value(node, "left-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 *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) : int left = spacing != NULL ? yml_value_as_int(spacing) :
left_spacing != NULL ? yml_value_as_int(left_spacing) : 0; left_spacing != NULL ? yml_value_as_int(left_spacing) : 0;
int right = spacing != NULL ? yml_value_as_int(spacing) : int right = spacing != NULL ? yml_value_as_int(spacing) :
right_spacing != NULL ? yml_value_as_int(right_spacing) : 0; 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)]; struct i3_workspaces workspaces[yml_dict_length(c)];
size_t idx = 0; 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); 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 static bool
@ -680,6 +705,13 @@ verify_content(keychain_t *chain, const struct yml_node *node)
return true; 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 static bool
verify_conf(keychain_t *chain, const struct yml_node *node) 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}, {"spacing", false, &conf_verify_int},
{"left-spacing", false, &conf_verify_int}, {"left-spacing", false, &conf_verify_int},
{"right-spacing", false, &conf_verify_int}, {"right-spacing", false, &conf_verify_int},
{"sort", false, &verify_sort},
{"content", true, &verify_content}, {"content", true, &verify_content},
{"anchors", false, NULL}, {"anchors", false, NULL},
{NULL, false, NULL}, {NULL, false, NULL},