Skip to content

Commit 1f6277e

Browse files
authored
Merge pull request #783 from f0rki/duplicate-num-assignment
make waybar itself assign numbers to workspaces like sway
2 parents 759602a + 006850e commit 1f6277e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

include/modules/sway/workspaces.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Workspaces : public AModule, public sigc::trackable {
2222
private:
2323
static inline const std::string workspace_switch_cmd_ = "workspace --no-auto-back-and-forth \"{}\"";
2424

25+
static int convertWorkspaceNameToNum(std::string name);
26+
2527
void onCmd(const struct Ipc::ipc_response&);
2628
void onEvent(const struct Ipc::ipc_response&);
2729
bool filterButtons();

src/modules/sway/workspaces.cpp

+38-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22

33
#include <spdlog/spdlog.h>
44

5+
#include <cctype>
6+
#include <string>
7+
58
namespace waybar::modules::sway {
69

10+
// Helper function to to assign a number to a workspace, just like sway. In fact
11+
// this is taken quite verbatim from `sway/ipc-json.c`.
12+
int Workspaces::convertWorkspaceNameToNum(std::string name) {
13+
if (isdigit(name[0])) {
14+
errno = 0;
15+
char * endptr = NULL;
16+
long long parsed_num = strtoll(name.c_str(), &endptr, 10);
17+
if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name.c_str()) {
18+
return -1;
19+
} else {
20+
return (int)parsed_num;
21+
}
22+
}
23+
return -1;
24+
}
25+
726
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
827
: AModule(config, "workspaces", id, false, !config["disable-scroll"].asBool()),
928
bar_(bar),
@@ -102,13 +121,29 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
102121
// the "num" property (integer type):
103122
// The workspace number or -1 for workspaces that do
104123
// not start with a number.
105-
auto l = lhs["num"].asInt();
106-
auto r = rhs["num"].asInt();
124+
// We could rely on sway providing this property:
125+
//
126+
// auto l = lhs["num"].asInt();
127+
// auto r = rhs["num"].asInt();
128+
//
129+
// We cannot rely on the "num" property as provided by sway
130+
// via IPC, because persistent workspace might not exist in
131+
// sway's view. However, we need this property also for
132+
// not-yet created persistent workspace. As such, we simply
133+
// duplicate sway's logic of assigning the "num" property
134+
// into waybar (see convertWorkspaceNameToNum). This way the
135+
// sorting should work out even when we include workspaces
136+
// that do not currently exist.
137+
auto lname = lhs["name"].asString();
138+
auto rname = rhs["name"].asString();
139+
int l = convertWorkspaceNameToNum(lname);
140+
int r = convertWorkspaceNameToNum(rname);
141+
107142
if (l == r) {
108143
// in case both integers are the same, lexicographical
109144
// sort. This also covers the case when both don't have a
110145
// number (i.e., l == r == -1).
111-
return lhs["name"].asString() < rhs["name"].asString();
146+
return lname < rname;
112147
}
113148

114149
// one of the workspaces doesn't begin with a number, so

0 commit comments

Comments
 (0)