sway,feat: allow alphabetical sort

for users who do not utilize any form of "workspace prev/next" commands,
it can be very handle to sort the workspaces alphabetically.

this commit adds a new "alphabetical_sort" to the `sway/workspaces`
module which allows the module to alway sort workspaces alphabetically.

this docs are updated to warn the user of the implications involved.

Signed-off-by: Louis DeLosSantos <louis.delos@gmail.com>
pull/1918/head
Louis DeLosSantos 2023-01-02 17:03:28 -05:00
parent fb083f93dc
commit 8b512e7b22
2 changed files with 13 additions and 2 deletions

View File

@ -73,6 +73,10 @@ Addressed by *sway/workspaces*
typeof: bool ++ typeof: bool ++
Whether to disable *workspace_auto_back_and_forth* when clicking on workspaces. If this is set to *true*, clicking on a workspace you are already on won't do anything, even if *workspace_auto_back_and_forth* is enabled in the Sway configuration. Whether to disable *workspace_auto_back_and_forth* when clicking on workspaces. If this is set to *true*, clicking on a workspace you are already on won't do anything, even if *workspace_auto_back_and_forth* is enabled in the Sway configuration.
*alphabetical_sort*: ++
typeof: bool ++
Whether to sort workspaces alphabetically. Please note this can make "swaymsg workspace prev/next" move to workspaces inconsistent with the ordering shown in Waybar.
# FORMAT REPLACEMENTS # FORMAT REPLACEMENTS
*{value}*: Name of the workspace, as defined by sway. *{value}*: Name of the workspace, as defined by sway.

View File

@ -130,6 +130,10 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
// In a first pass, the maximum "num" value is computed to enqueue // In a first pass, the maximum "num" value is computed to enqueue
// unnumbered workspaces behind numbered ones when computing the sort // unnumbered workspaces behind numbered ones when computing the sort
// attribute. // attribute.
//
// Note: if the 'alphabetical_sort' option is true, the user is in
// agreement that the "workspace prev/next" commands may not follow
// the order displayed in Waybar.
int max_num = -1; int max_num = -1;
for (auto &workspace : workspaces_) { for (auto &workspace : workspaces_) {
max_num = std::max(workspace["num"].asInt(), max_num); max_num = std::max(workspace["num"].asInt(), max_num);
@ -143,16 +147,19 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
} }
} }
std::sort(workspaces_.begin(), workspaces_.end(), std::sort(workspaces_.begin(), workspaces_.end(),
[](const Json::Value &lhs, const Json::Value &rhs) { [this](const Json::Value &lhs, const Json::Value &rhs) {
auto lname = lhs["name"].asString(); auto lname = lhs["name"].asString();
auto rname = rhs["name"].asString(); auto rname = rhs["name"].asString();
int l = lhs["sort"].asInt(); int l = lhs["sort"].asInt();
int r = rhs["sort"].asInt(); int r = rhs["sort"].asInt();
if (l == r) { if (l == r || config_["alphabetical_sort"].asBool()) {
// In case both integers are the same, lexicographical // In case both integers are the same, lexicographical
// sort. The code above already ensure that this will only // sort. The code above already ensure that this will only
// happend in case of explicitly numbered workspaces. // happend in case of explicitly numbered workspaces.
//
// Additionally, if the config specifies to sort workspaces
// alphabetically do this here.
return lname < rname; return lname < rname;
} }