Merge pull request #696 from f0rki/sort_by_sway_num

sway/workspaces: sort by the "num" property provided by sway
This commit is contained in:
Alex 2020-05-06 12:38:12 +02:00 committed by GitHub
commit 0e869e3aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 10 deletions

View File

@ -64,6 +64,10 @@ Addressed by *sway/workspaces*
typeof: string ++ typeof: string ++
Command to execute when the module is updated. Command to execute when the module is updated.
*numeric-first*: ++
typeof: bool ++
Whether to put workspaces starting with numbers before workspaces that do not start with a number.
# FORMAT REPLACEMENTS # FORMAT REPLACEMENTS
*{value}*: Name of the workspace, as defined by sway. *{value}*: Name of the workspace, as defined by sway.
@ -107,6 +111,7 @@ n.b.: the list of outputs can be obtained from command line using *swaymsg -t ge
"sway/workspaces": { "sway/workspaces": {
"disable-scroll": true, "disable-scroll": true,
"all-outputs": true, "all-outputs": true,
"numeric-first": false,
"format": "{name}: {icon}", "format": "{name}: {icon}",
"format-icons": { "format-icons": {
"1": "", "1": "",

View File

@ -1,4 +1,5 @@
#include "modules/sway/workspaces.hpp" #include "modules/sway/workspaces.hpp"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
namespace waybar::modules::sway { namespace waybar::modules::sway {
@ -90,16 +91,38 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
workspaces_.emplace_back(std::move(v)); workspaces_.emplace_back(std::move(v));
} }
} }
}
// config option to sort numeric workspace names before others
bool config_numeric_first = config_["numeric-first"].asBool();
std::sort(workspaces_.begin(), std::sort(workspaces_.begin(),
workspaces_.end(), workspaces_.end(),
[](const Json::Value &lhs, const Json::Value &rhs) { [config_numeric_first](const Json::Value &lhs, const Json::Value &rhs) {
if (lhs["name"].isInt() && rhs["name"].isInt()) { // the "num" property (integer type):
return lhs["name"].asInt() < rhs["name"].asInt(); // The workspace number or -1 for workspaces that do
} // not start with a number.
auto l = lhs["num"].asInt();
auto r = rhs["num"].asInt();
if (l == r) {
// in case both integers are the same, lexicographical
// sort. This also covers the case when both don't have a
// number (i.e., l == r == -1).
return lhs["name"].asString() < rhs["name"].asString(); return lhs["name"].asString() < rhs["name"].asString();
});
} }
// one of the workspaces doesn't begin with a number, so
// num is -1.
if (l < 0 || r < 0) {
if (config_numeric_first) {
return r < 0;
}
return l < 0;
}
// both workspaces have a "num" so let's just compare those
return l < r;
});
} }
dp.emit(); dp.emit();
} catch (const std::exception &e) { } catch (const std::exception &e) {
@ -217,7 +240,8 @@ std::string Workspaces::getIcon(const std::string &name, const Json::Value &node
if (config_["format-icons"][key].isString() && node[key].asBool()) { if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString(); return config_["format-icons"][key].asString();
} }
} else if (config_["format_icons"]["persistent"].isString() && node["target_output"].isString()) { } else if (config_["format_icons"]["persistent"].isString() &&
node["target_output"].isString()) {
return config_["format-icons"]["persistent"].asString(); return config_["format-icons"]["persistent"].asString();
} else if (config_["format-icons"][key].isString()) { } else if (config_["format-icons"][key].isString()) {
return config_["format-icons"][key].asString(); return config_["format-icons"][key].asString();