Skip to content

Commit

Permalink
Merge pull request #2756 from grimpy/custom_output_env_var
Browse files Browse the repository at this point in the history
Pass WAYBAR_OUTPUT_NAME environment variable to custom exec scripts
  • Loading branch information
Alexays authored Dec 21, 2023
2 parents 2674982 + 0ea5143 commit 08361be
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/modules/custom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace waybar::modules {

class Custom : public ALabel {
public:
Custom(const std::string&, const std::string&, const Json::Value&);
Custom(const std::string&, const std::string&, const Json::Value&, const std::string&);
virtual ~Custom();
auto update() -> void override;
void refresh(int /*signal*/) override;
Expand All @@ -30,6 +30,7 @@ class Custom : public ALabel {
bool handleToggle(GdkEventButton* const& e) override;

const std::string name_;
const std::string output_name_;

Check warning on line 33 in include/modules/custom.hpp

View workflow job for this annotation

GitHub Actions / build

include/modules/custom.hpp:33:21 [readability-identifier-naming]

invalid case style for private member 'output_name_'
std::string text_;
std::string id_;
std::string alt_;
Expand Down
11 changes: 7 additions & 4 deletions include/util/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ inline int close(FILE* fp, pid_t pid) {
return stat;
}

inline FILE* open(const std::string& cmd, int& pid) {
inline FILE* open(const std::string& cmd, int& pid, const std::string& output_name) {
if (cmd == "") return nullptr;
int fd[2];
// Open the pipe with the close-on-exec flag set, so it will not be inherited
Expand Down Expand Up @@ -109,6 +109,9 @@ inline FILE* open(const std::string& cmd, int& pid) {
::close(fd[0]);
dup2(fd[1], 1);
setpgid(child_pid, child_pid);
if (output_name != "") {

Check warning on line 112 in include/util/command.hpp

View workflow job for this annotation

GitHub Actions / build

include/util/command.hpp:112:9 [readability-container-size-empty]

the 'empty' method should be used to check for emptiness instead of comparing to an empty object
setenv("WAYBAR_OUTPUT_NAME", output_name.c_str(), 1);
}
execlp("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
exit(0);
} else {
Expand All @@ -118,9 +121,9 @@ inline FILE* open(const std::string& cmd, int& pid) {
return fdopen(fd[0], "r");
}

inline struct res exec(const std::string& cmd) {
inline struct res exec(const std::string& cmd, const std::string& output_name) {
int pid;
auto fp = command::open(cmd, pid);
auto fp = command::open(cmd, pid, output_name);

Check warning on line 126 in include/util/command.hpp

View workflow job for this annotation

GitHub Actions / build

include/util/command.hpp:126:3 [readability-qualified-auto]

'auto fp' can be declared as 'auto *fp'
if (!fp) return {-1, ""};
auto output = command::read(fp);
auto stat = command::close(fp, pid);
Expand All @@ -129,7 +132,7 @@ inline struct res exec(const std::string& cmd) {

inline struct res execNoRead(const std::string& cmd) {
int pid;
auto fp = command::open(cmd, pid);
auto fp = command::open(cmd, pid, "");

Check warning on line 135 in include/util/command.hpp

View workflow job for this annotation

GitHub Actions / build

include/util/command.hpp:135:3 [readability-qualified-auto]

'auto fp' can be declared as 'auto *fp'
if (!fp) return {-1, ""};
auto stat = command::close(fp, pid);
return {WEXITSTATUS(stat), ""};
Expand Down
2 changes: 1 addition & 1 deletion src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
return new waybar::modules::Temperature(id, config_[name]);
}
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
return new waybar::modules::Custom(ref.substr(7), id, config_[name]);
return new waybar::modules::Custom(ref.substr(7), id, config_[name], bar_.output->name);
}
if (ref.compare(0, 5, "cffi/") == 0 && ref.size() > 5) {
return new waybar::modules::CFFI(ref.substr(5), id, config_[name]);
Expand Down
11 changes: 6 additions & 5 deletions src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include "util/scope_guard.hpp"

waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
const Json::Value& config)
const Json::Value& config, const std::string& output_name)

Check warning on line 8 in src/modules/custom.cpp

View workflow job for this annotation

GitHub Actions / build

src/modules/custom.cpp:8:60 [modernize-pass-by-value]

pass by value and use std::move
: ALabel(config, "custom-" + name, id, "{}"),
name_(name),
output_name_(output_name),
id_(id),
percentage_(0),
fp_(nullptr),
Expand Down Expand Up @@ -42,7 +43,7 @@ void waybar::modules::Custom::delayWorker() {
}
if (can_update) {
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString());
output_ = util::command::exec(config_["exec"].asString(), output_name_);
}
dp.emit();
}
Expand All @@ -53,7 +54,7 @@ void waybar::modules::Custom::delayWorker() {
void waybar::modules::Custom::continuousWorker() {
auto cmd = config_["exec"].asString();
pid_ = -1;
fp_ = util::command::open(cmd, pid_);
fp_ = util::command::open(cmd, pid_, output_name_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
Expand All @@ -79,7 +80,7 @@ void waybar::modules::Custom::continuousWorker() {
if (config_["restart-interval"].isUInt()) {
pid_ = -1;
thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt()));
fp_ = util::command::open(cmd, pid_);
fp_ = util::command::open(cmd, pid_, output_name_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
Expand Down Expand Up @@ -112,7 +113,7 @@ void waybar::modules::Custom::waitingWorker() {
}
if (can_update) {
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString());
output_ = util::command::exec(config_["exec"].asString(), output_name_);
}
dp.emit();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ auto waybar::modules::Image::update() -> void {
if (config_["path"].isString()) {
path_ = config_["path"].asString();
} else if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString());
output_ = util::command::exec(config_["exec"].asString(), "");
parseOutputRaw();
} else {
path_ = "";
Expand Down

0 comments on commit 08361be

Please sign in to comment.