mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Style code (#25)
This commit is contained in:
@ -1,30 +1,33 @@
|
||||
#include "modules/custom.hpp"
|
||||
#include <iostream>
|
||||
|
||||
waybar::modules::Custom::Custom(std::string name, Json::Value config)
|
||||
: _name(name), _config(config)
|
||||
waybar::modules::Custom::Custom(const std::string &name, Json::Value config)
|
||||
: name_(name), config_(std::move(config))
|
||||
{
|
||||
if (!_config["exec"])
|
||||
throw std::runtime_error(name + " has no exec path.");
|
||||
int interval = _config["interval"] ? _config["inveral"].asInt() : 30;
|
||||
_thread = [this, interval] {
|
||||
if (!config_["exec"]) {
|
||||
throw std::runtime_error(name_ + " has no exec path.");
|
||||
}
|
||||
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
|
||||
thread_ = [this, interval] {
|
||||
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Custom::update));
|
||||
_thread.sleep_for(chrono::seconds(interval));
|
||||
thread_.sleep_for(chrono::seconds(interval));
|
||||
};
|
||||
};
|
||||
|
||||
auto waybar::modules::Custom::update() -> void
|
||||
{
|
||||
std::array<char, 128> buffer;
|
||||
std::array<char, 128> buffer = {0};
|
||||
std::string output;
|
||||
std::shared_ptr<FILE> fp(popen(_config["exec"].asCString(), "r"), pclose);
|
||||
std::shared_ptr<FILE> fp(popen(config_["exec"].asCString(), "r"), pclose);
|
||||
if (!fp) {
|
||||
std::cerr << _name + " can't exec " + _config["exec"].asString() << std::endl;
|
||||
std::cerr << name_ + " can't exec " + config_["exec"].asString() << std::endl;
|
||||
return;
|
||||
}
|
||||
while (!feof(fp.get())) {
|
||||
if (fgets(buffer.data(), 128, fp.get()) != nullptr)
|
||||
|
||||
while (feof(fp.get()) == 0) {
|
||||
if (fgets(buffer.data(), 128, fp.get()) != nullptr) {
|
||||
output += buffer.data();
|
||||
}
|
||||
}
|
||||
|
||||
// Remove last newline
|
||||
@ -34,16 +37,16 @@ auto waybar::modules::Custom::update() -> void
|
||||
|
||||
// Hide label if output is empty
|
||||
if (output.empty()) {
|
||||
_label.set_name("");
|
||||
_label.hide();
|
||||
label_.set_name("");
|
||||
label_.hide();
|
||||
} else {
|
||||
_label.set_name("custom-" + _name);
|
||||
auto format = _config["format"] ? _config["format"].asString() : "{}";
|
||||
_label.set_text(fmt::format(format, output));
|
||||
_label.show();
|
||||
label_.set_name("custom-" + name_);
|
||||
auto format = config_["format"] ? config_["format"].asString() : "{}";
|
||||
label_.set_text(fmt::format(format, output));
|
||||
label_.show();
|
||||
}
|
||||
}
|
||||
|
||||
waybar::modules::Custom::operator Gtk::Widget &() {
|
||||
return _label;
|
||||
return label_;
|
||||
}
|
||||
|
Reference in New Issue
Block a user