mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat(Custom): handle continuous script
This commit is contained in:
parent
d5d620e72d
commit
00959c7d65
@ -13,7 +13,8 @@ class Custom : public ALabel {
|
|||||||
Custom(const std::string, const Json::Value&);
|
Custom(const std::string, const Json::Value&);
|
||||||
auto update() -> void;
|
auto update() -> void;
|
||||||
private:
|
private:
|
||||||
void worker();
|
void delayWorker();
|
||||||
|
void continuousWorker();
|
||||||
|
|
||||||
const std::string name_;
|
const std::string name_;
|
||||||
waybar::util::SleeperThread thread_;
|
waybar::util::SleeperThread thread_;
|
||||||
|
@ -70,7 +70,7 @@ struct SleeperThread {
|
|||||||
condvar_.notify_all();
|
condvar_.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
~SleeperThread()
|
auto stop()
|
||||||
{
|
{
|
||||||
do_run_ = false;
|
do_run_ = false;
|
||||||
condvar_.notify_all();
|
condvar_.notify_all();
|
||||||
@ -79,6 +79,11 @@ struct SleeperThread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~SleeperThread()
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::thread thread_;
|
std::thread thread_;
|
||||||
std::condition_variable condvar_;
|
std::condition_variable condvar_;
|
||||||
|
@ -62,6 +62,7 @@
|
|||||||
"custom/spotify": {
|
"custom/spotify": {
|
||||||
"format": " {}",
|
"format": " {}",
|
||||||
"max-length": 40,
|
"max-length": 40,
|
||||||
|
"interval": 30, // Remove this if your script is endless and write in loop
|
||||||
"exec": "$HOME/.config/waybar/mediaplayer.sh", // Script in resources folder
|
"exec": "$HOME/.config/waybar/mediaplayer.sh", // Script in resources folder
|
||||||
"exec-if": "pgrep spotify"
|
"exec-if": "pgrep spotify"
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,16 @@ waybar::modules::Custom::Custom(const std::string name,
|
|||||||
if (!config_["exec"]) {
|
if (!config_["exec"]) {
|
||||||
throw std::runtime_error(name_ + " has no exec path.");
|
throw std::runtime_error(name_ + " has no exec path.");
|
||||||
}
|
}
|
||||||
worker();
|
if (config_["interval"]) {
|
||||||
|
delayWorker();
|
||||||
|
} else {
|
||||||
|
continuousWorker();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Custom::worker()
|
void waybar::modules::Custom::delayWorker()
|
||||||
{
|
{
|
||||||
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
|
auto interval = config_["interval"].asUInt();
|
||||||
thread_ = [this, interval] {
|
thread_ = [this, interval] {
|
||||||
bool can_update = true;
|
bool can_update = true;
|
||||||
if (config_["exec-if"]) {
|
if (config_["exec-if"]) {
|
||||||
@ -31,6 +35,34 @@ void waybar::modules::Custom::worker()
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void waybar::modules::Custom::continuousWorker()
|
||||||
|
{
|
||||||
|
auto cmd = config_["exec"].asString();
|
||||||
|
FILE* fp(popen(cmd.c_str(), "r"));
|
||||||
|
if (!fp) {
|
||||||
|
throw std::runtime_error("Unable to open " + cmd);
|
||||||
|
}
|
||||||
|
thread_ = [this, fp] {
|
||||||
|
char* buff = nullptr;
|
||||||
|
size_t len = 0;
|
||||||
|
if (getline(&buff, &len, fp) == -1) {
|
||||||
|
thread_.stop();
|
||||||
|
output_ = { 1, "" };
|
||||||
|
dp.emit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string output = buff;
|
||||||
|
|
||||||
|
// Remove last newline
|
||||||
|
if (!output.empty() && output[output.length()-1] == '\n') {
|
||||||
|
output.erase(output.length()-1);
|
||||||
|
}
|
||||||
|
output_ = { 0, output };
|
||||||
|
dp.emit();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
auto waybar::modules::Custom::update() -> void
|
auto waybar::modules::Custom::update() -> void
|
||||||
{
|
{
|
||||||
// Hide label if output is empty
|
// Hide label if output is empty
|
||||||
|
Loading…
Reference in New Issue
Block a user