From 5b2de60353ded15dc62076bfcd0ca3e035de5b86 Mon Sep 17 00:00:00 2001 From: indev Date: Sun, 6 Mar 2022 15:34:12 +0700 Subject: [PATCH] feat(script): add repeat interval for script failure and exec-if --- CHANGELOG.md | 1 + include/adapters/script_runner.hpp | 5 +++-- include/modules/script.hpp | 4 +++- src/adapters/script_runner.cpp | 15 ++++++++------- src/modules/script.cpp | 8 +++++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2dfc20e..2f082cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `polybar-msg hook` is deprecated in favor of using the hook action. `polybar-msg` will tell you the correct command to use. ### Added +- Add repeat interval for script failure (`interval-fail`) and `exec-if` (`interval-if`) ([`#943`](https://github.com/polybar/polybar/issues/943), [`#2606`](https://github.com/polybar/polybar/issues/2606)) - Support `px` and `pt` units everyhwere where before only a number of spaces or pixels could be specified. ([`#2578`](https://github.com/polybar/polybar/pull/2578), [`#1651`](https://github.com/polybar/polybar/issues/1651), [`#951`](https://github.com/polybar/polybar/issues/951)) - `internal/alsa`: Right and middle click settings. ([`#2566`](https://github.com/polybar/polybar/issues/2566), [`#2573`](https://github.com/polybar/polybar/pull/2573)) - `internal/network`: diff --git a/include/adapters/script_runner.hpp b/include/adapters/script_runner.hpp index a2322e50..3df931ea 100644 --- a/include/adapters/script_runner.hpp +++ b/include/adapters/script_runner.hpp @@ -13,7 +13,7 @@ class script_runner { public: using interval = std::chrono::duration; script_runner(std::function on_update, const string& exec, const string& exec_if, bool tail, - interval interval, const vector>& env); + interval interval_success, interval interval_fail, const vector>& env); bool check_condition() const; interval process(); @@ -44,7 +44,8 @@ class script_runner { const string m_exec; const string m_exec_if; const bool m_tail; - const interval m_interval; + const interval m_interval_success; + const interval m_interval_fail; const vector> m_env; std::mutex m_output_lock; diff --git a/include/modules/script.hpp b/include/modules/script.hpp index 94308249..7d764b96 100644 --- a/include/modules/script.hpp +++ b/include/modules/script.hpp @@ -31,7 +31,9 @@ namespace modules { static constexpr auto FORMAT_FAIL = "format-fail"; const bool m_tail; - const script_runner::interval m_interval{0}; + const script_runner::interval m_interval_success{0}; + const script_runner::interval m_interval_fail{0}; + const script_runner::interval m_interval_if{0}; script_runner m_runner; diff --git a/src/adapters/script_runner.cpp b/src/adapters/script_runner.cpp index 8af30102..cec6e240 100644 --- a/src/adapters/script_runner.cpp +++ b/src/adapters/script_runner.cpp @@ -12,13 +12,14 @@ POLYBAR_NS script_runner::script_runner(std::function on_update, const string& exec, const string& exec_if, bool tail, - interval interval, const vector>& env) + interval interval_success, interval interval_fail, const vector>& env) : m_log(logger::make()) , m_on_update(on_update) , m_exec(exec) , m_exec_if(exec_if) , m_tail(tail) - , m_interval(interval) + , m_interval_success(interval_success) + , m_interval_fail(interval_fail) , m_env(env) {} /** @@ -60,7 +61,7 @@ int script_runner::get_counter() const { return m_counter; } -int script_runner::get_exit_status() const { +int script_runner::get_exit_status() const { return m_exit_status; } @@ -131,9 +132,9 @@ script_runner::interval script_runner::run() { } if (m_exit_status == 0) { - return m_interval; + return m_interval_success; } else { - return std::max(m_interval, interval{1s}); + return std::max(m_interval_fail, interval{1s}); } } @@ -168,9 +169,9 @@ script_runner::interval script_runner::run_tail() { auto exit_status = cmd.wait(); if (exit_status == 0) { - return m_interval; + return m_interval_success; } else { - return std::max(m_interval, interval{1s}); + return std::max(m_interval_fail, interval{1s}); } } diff --git a/src/modules/script.cpp b/src/modules/script.cpp index af9466c9..cb7c0771 100644 --- a/src/modules/script.cpp +++ b/src/modules/script.cpp @@ -9,9 +9,11 @@ namespace modules { script_module::script_module(const bar_settings& bar, string name_) : module(bar, move(name_)) , m_tail(m_conf.get(name(), "tail", false)) - , m_interval(m_conf.get(name(), "interval", m_tail ? 0s : 5s)) + , m_interval_success(m_conf.get(name(), "interval", m_tail ? 0s : 5s)) + , m_interval_fail(m_conf.get(name(), "interval-fail", m_interval_success)) + , m_interval_if(m_conf.get(name(), "interval-if", m_interval_success)) , m_runner([this]() { broadcast(); }, m_conf.get(name(), "exec", ""s), m_conf.get(name(), "exec-if", ""s), m_tail, - m_interval, m_conf.get_with_prefix(name(), "env-")) { + m_interval_success, m_interval_fail, m_conf.get_with_prefix(name(), "env-")) { // Load configured click handlers m_actions[mousebtn::LEFT] = m_conf.get(name(), "click-left", ""s); m_actions[mousebtn::MIDDLE] = m_conf.get(name(), "click-middle", ""s); @@ -47,7 +49,7 @@ namespace modules { sleep_time = m_runner.process(); } else { m_runner.clear_output(); - sleep_time = std::max(m_interval, script_runner::interval(1s)); + sleep_time = std::max(m_interval_if, script_runner::interval(1s)); } if (m_runner.is_stopping()) {