Skip to content

Commit 9e53174

Browse files
committed
feat(sway/workspaces): add format for negative indices
1 parent 0648454 commit 9e53174

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

man/waybar-sway-workspaces.5.scd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Addressed by *sway/workspaces*
2222
default: {value} ++
2323
The format, how information should be displayed.
2424

25+
*format-for-negative-index*: ++
26+
typeof: string ++
27+
default: *format* ++
28+
An alternative format, which will be used for workspaces with no explict or negative index ("num" in sway terms). Requires *format* to be set.
29+
2530
*format-icons*: ++
2631
typeof: array ++
2732
Based on the workspace name and state, the corresponding icon gets selected. See *icons*.
@@ -187,6 +192,13 @@ n.b.: the list of outputs can be obtained from command line using *swaymsg -t ge
187192
}
188193
```
189194

195+
```
196+
"sway/workspaces": {
197+
"format": "{index} - {name}",
198+
"format-for-negative-index": "{name}"
199+
}
200+
```
201+
190202
# Style
191203

192204
- *#workspaces button*

src/modules/sway/workspaces.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,43 @@ auto Workspaces::update() -> void {
329329
} else {
330330
button.get_style_context()->remove_class("current_output");
331331
}
332-
std::string output = (*it)["name"].asString();
332+
333333
std::string windows = "";
334334
if (config_["window-rewrite"].isObject()) {
335335
updateWindows((*it), windows);
336336
}
337+
338+
auto index = (*it)["num"].asInt();
339+
auto full_name = (*it)["name"].asString();
340+
337341
if (config_["format"].isString()) {
338-
auto format = config_["format"].asString();
339-
output = fmt::format(
340-
fmt::runtime(format), fmt::arg("icon", getIcon(output, *it)), fmt::arg("value", output),
341-
fmt::arg("name", trimWorkspaceName(output)), fmt::arg("index", (*it)["num"].asString()),
342-
fmt::arg("windows",
343-
windows.substr(0, windows.length() - m_formatWindowSeparator.length())),
344-
fmt::arg("output", (*it)["output"].asString()));
342+
std::string format;
343+
if(config_["format-for-negative-index"].isString() && index < 0) {
344+
format = config_["format-for-negative-index"].asString();
345+
} else {
346+
format = config_["format"].asString();
347+
}
348+
349+
auto name = trimWorkspaceName(full_name);
350+
auto output = (*it)["output"].asString();
351+
auto icon = getIcon(full_name, *it);
352+
auto separated_windows = windows.substr(0, windows.length() - m_formatWindowSeparator.length());
353+
354+
full_name = fmt::format(
355+
fmt::runtime(format),
356+
fmt::arg("index", index),
357+
fmt::arg("name", name),
358+
fmt::arg("value", full_name),
359+
fmt::arg("output", output),
360+
fmt::arg("icon", icon),
361+
fmt::arg("windows", separated_windows)
362+
);
345363
}
364+
346365
if (!config_["disable-markup"].asBool()) {
347-
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(output);
366+
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(full_name);
348367
} else {
349-
button.set_label(output);
368+
button.set_label(full_name);
350369
}
351370
onButtonReady(*it, button);
352371
}

0 commit comments

Comments
 (0)