diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index dcab363c..bb0204fd 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -17,6 +17,7 @@ class Custom : public ALabel { const std::string name_; waybar::util::SleeperThread thread_; + waybar::util::command::res output_; }; } diff --git a/include/util/command.hpp b/include/util/command.hpp index 80773cb5..18069580 100644 --- a/include/util/command.hpp +++ b/include/util/command.hpp @@ -4,12 +4,12 @@ namespace waybar::util::command { -struct cmd_res { +struct res { int exit_code; std::string out; }; -inline struct cmd_res exec(const std::string cmd) +inline struct res exec(const std::string cmd) { FILE* fp(popen(cmd.c_str(), "r")); if (!fp) { diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index ccb9e4d2..a47d5085 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -24,6 +24,7 @@ void waybar::modules::Custom::worker() } } if (can_update) { + output_ = waybar::util::command::exec(config_["exec"].asString()); dp.emit(); } thread_.sleep_for(chrono::seconds(interval)); @@ -32,17 +33,15 @@ void waybar::modules::Custom::worker() auto waybar::modules::Custom::update() -> void { - auto res = waybar::util::command::exec(config_["exec"].asString()); - // Hide label if output is empty - if (res.out.empty() || res.exit_code != 0) { + if (output_.out.empty() || output_.exit_code != 0) { label_.hide(); label_.set_name(""); } else { label_.set_name("custom-" + name_); - auto str = fmt::format(format_, res.out); + auto str = fmt::format(format_, output_.out); label_.set_text(str); label_.set_tooltip_text(str); label_.show(); } -} +} \ No newline at end of file