Skip to content

Commit f4b68f4

Browse files
feat: sway/window: provide {marks} format replacement
1 parent 0332d2e commit f4b68f4

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

include/modules/sway/window.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class Window : public AAppIconLabel, public sigc::trackable {
1919
auto update() -> void override;
2020

2121
private:
22-
void setClass(std::string classname, bool enable);
22+
void setClass(const std::string& classname, bool enable);
2323
void onEvent(const struct Ipc::ipc_response&);
2424
void onCmd(const struct Ipc::ipc_response&);
25-
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string>
25+
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string,
26+
std::string>
2627
getFocusedNode(const Json::Value& nodes, std::string& output);
2728
void getTree();
2829

@@ -35,6 +36,7 @@ class Window : public AAppIconLabel, public sigc::trackable {
3536
std::string old_app_id_;
3637
std::size_t app_nb_;
3738
std::string shell_;
39+
std::string marks_;
3840
int floating_count_;
3941
util::JsonParser parser_;
4042
std::mutex mutex_;

man/waybar-sway-window.5.scd

+7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Addressed by *sway/window*
8989
default: false ++
9090
If the workspace itself is focused and the workspace contains nodes or floating_nodes, show the workspace name. If not set, text remains empty but styles according to nodes in the workspace are still applied.
9191

92+
*show-hidden-marks*: ++
93+
typeof: bool ++
94+
default: false ++
95+
For the *{marks}* format replacement, include hidden marks that start with an underscore.
96+
9297
*rewrite*: ++
9398
typeof: object ++
9499
Rules to rewrite the module format output. See *rewrite rules*.
@@ -117,6 +122,8 @@ Addressed by *sway/window*
117122
*{shell}*: The shell of the focused window. It's 'xwayland' when the window is
118123
running through xwayland, otherwise, it's 'xdg-shell'.
119124

125+
*{marks}*: Marks of the window.
126+
120127
# REWRITE RULES
121128

122129
*rewrite* is an object where keys are regular expressions and values are

src/modules/sway/window.cpp

+39-18
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ void Window::onCmd(const struct Ipc::ipc_response& res) {
4141
std::lock_guard<std::mutex> lock(mutex_);
4242
auto payload = parser_.parse(res.payload);
4343
auto output = payload["output"].isString() ? payload["output"].asString() : "";
44-
std::tie(app_nb_, floating_count_, windowId_, window_, app_id_, app_class_, shell_, layout_) =
45-
getFocusedNode(payload["nodes"], output);
44+
std::tie(app_nb_, floating_count_, windowId_, window_, app_id_, app_class_, shell_, layout_,
45+
marks_) = getFocusedNode(payload["nodes"], output);
4646
updateAppIconName(app_id_, app_class_);
4747
dp.emit();
4848
} catch (const std::exception& e) {
@@ -96,7 +96,7 @@ auto Window::update() -> void {
9696

9797
label_.set_markup(waybar::util::rewriteString(
9898
fmt::format(fmt::runtime(format_), fmt::arg("title", window_), fmt::arg("app_id", app_id_),
99-
fmt::arg("shell", shell_)),
99+
fmt::arg("shell", shell_), fmt::arg("marks", marks_)),
100100
config_["rewrite"]));
101101
if (tooltipEnabled()) {
102102
label_.set_tooltip_text(window_);
@@ -108,7 +108,7 @@ auto Window::update() -> void {
108108
AAppIconLabel::update();
109109
}
110110

111-
void Window::setClass(std::string classname, bool enable) {
111+
void Window::setClass(const std::string& classname, bool enable) {
112112
if (enable) {
113113
if (!bar_.window.get_style_context()->has_class(classname)) {
114114
bar_.window.get_style_context()->add_class(classname);
@@ -169,17 +169,31 @@ std::optional<std::reference_wrapper<const Json::Value>> getSingleChildNode(
169169
return {getSingleChildNode(child)};
170170
}
171171

172-
std::tuple<std::string, std::string, std::string> getWindowInfo(const Json::Value& node) {
172+
std::tuple<std::string, std::string, std::string, std::string> getWindowInfo(
173+
const Json::Value& node, bool showHidden) {
173174
const auto app_id = node["app_id"].isString() ? node["app_id"].asString()
174175
: node["window_properties"]["instance"].asString();
175176
const auto app_class = node["window_properties"]["class"].isString()
176177
? node["window_properties"]["class"].asString()
177178
: "";
178179
const auto shell = node["shell"].isString() ? node["shell"].asString() : "";
179-
return {app_id, app_class, shell};
180+
std::string marks = "";
181+
if (node["marks"].isArray()) {
182+
for (const auto& m : node["marks"]) {
183+
if (!m.isString() || (!showHidden && m.asString().at(0) == '_')) {
184+
continue;
185+
}
186+
if (!marks.empty()) {
187+
marks += ',';
188+
}
189+
marks += m.asString();
190+
}
191+
}
192+
return {app_id, app_class, shell, marks};
180193
}
181194

182-
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string>
195+
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string,
196+
std::string>
183197
gfnWithWorkspace(const Json::Value& nodes, std::string& output, const Json::Value& config_,
184198
const Bar& bar_, Json::Value& parentWorkspace,
185199
const Json::Value& immediateParent) {
@@ -207,15 +221,17 @@ gfnWithWorkspace(const Json::Value& nodes, std::string& output, const Json::Valu
207221
"",
208222
"",
209223
"",
210-
node["layout"].asString()};
224+
node["layout"].asString(),
225+
""};
211226
}
212227
parentWorkspace = node;
213228
} else if ((node["type"].asString() == "con" || node["type"].asString() == "floating_con") &&
214229
(node["focused"].asBool())) {
215230
// found node
216231
spdlog::trace("actual output {}, output found {}, node (focused) found {}", bar_.output->name,
217232
output, node["name"].asString());
218-
const auto [app_id, app_class, shell] = getWindowInfo(node);
233+
const auto [app_id, app_class, shell, marks] =
234+
getWindowInfo(node, config_["show-hidden-marks"].asBool());
219235
int nb = node.size();
220236
int floating_count = 0;
221237
std::string workspace_layout = "";
@@ -232,20 +248,21 @@ gfnWithWorkspace(const Json::Value& nodes, std::string& output, const Json::Valu
232248
app_id,
233249
app_class,
234250
shell,
235-
workspace_layout};
251+
workspace_layout,
252+
marks};
236253
}
237254

238255
// iterate
239-
auto [nb, f, id, name, app_id, app_class, shell, workspace_layout] =
256+
auto [nb, f, id, name, app_id, app_class, shell, workspace_layout, marks] =
240257
gfnWithWorkspace(node["nodes"], output, config_, bar_, parentWorkspace, node);
241-
auto [nb2, f2, id2, name2, app_id2, app_class2, shell2, workspace_layout2] =
258+
auto [nb2, f2, id2, name2, app_id2, app_class2, shell2, workspace_layout2, marks2] =
242259
gfnWithWorkspace(node["floating_nodes"], output, config_, bar_, parentWorkspace, node);
243260

244261
// if ((id > 0 || ((id2 < 0 || name2.empty()) && id > -1)) && !name.empty()) {
245262
if ((id > 0) || (id2 < 0 && id > -1)) {
246-
return {nb, f, id, name, app_id, app_class, shell, workspace_layout};
263+
return {nb, f, id, name, app_id, app_class, shell, workspace_layout, marks};
247264
} else if (id2 > 0 && !name2.empty()) {
248-
return {nb2, f2, id2, name2, app_id2, app_class, shell2, workspace_layout2};
265+
return {nb2, f2, id2, name2, app_id2, app_class, shell2, workspace_layout2, marks2};
249266
}
250267
}
251268

@@ -258,10 +275,12 @@ gfnWithWorkspace(const Json::Value& nodes, std::string& output, const Json::Valu
258275
std::string app_id = "";
259276
std::string app_class = "";
260277
std::string workspace_layout = "";
278+
std::string marks = "";
261279
if (all_leaf_nodes.first == 1) {
262280
const auto single_child = getSingleChildNode(immediateParent);
263281
if (single_child.has_value()) {
264-
std::tie(app_id, app_class, workspace_layout) = getWindowInfo(single_child.value());
282+
std::tie(app_id, app_class, workspace_layout, marks) =
283+
getWindowInfo(single_child.value(), config_["show-hidden-marks"].asBool());
265284
}
266285
}
267286
return {all_leaf_nodes.first,
@@ -273,13 +292,15 @@ gfnWithWorkspace(const Json::Value& nodes, std::string& output, const Json::Valu
273292
app_id,
274293
app_class,
275294
workspace_layout,
276-
immediateParent["layout"].asString()};
295+
immediateParent["layout"].asString(),
296+
marks};
277297
}
278298

279-
return {0, 0, -1, "", "", "", "", ""};
299+
return {0, 0, -1, "", "", "", "", "", ""};
280300
}
281301

282-
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string>
302+
std::tuple<std::size_t, int, int, std::string, std::string, std::string, std::string, std::string,
303+
std::string>
283304
Window::getFocusedNode(const Json::Value& nodes, std::string& output) {
284305
Json::Value placeholder = Json::Value::null;
285306
return gfnWithWorkspace(nodes, output, config_, bar_, placeholder, placeholder);

0 commit comments

Comments
 (0)