refactor(ALabel): add interval

This commit is contained in:
Alexis 2018-11-23 11:57:37 +01:00
parent 36652158ad
commit ad7400d5ce
10 changed files with 40 additions and 40 deletions

View File

@ -7,7 +7,7 @@ namespace waybar {
class ALabel : public IModule {
public:
ALabel(const Json::Value&, const std::string format);
ALabel(const Json::Value&, const std::string format, uint16_t interval = 0);
virtual ~ALabel() = default;
virtual auto update() -> void;
virtual std::string getIcon(uint16_t, const std::string& alt = "");
@ -19,6 +19,7 @@ class ALabel : public IModule {
const Json::Value& config_;
std::string format_;
std::mutex mutex_;
const std::chrono::seconds interval_;
private:
bool handleToggle(GdkEventButton* const& ev);

View File

@ -30,8 +30,8 @@ class Battery : public ALabel {
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
void worker();
std::tuple<uint16_t, std::string> getInfos();
std::string getState(uint16_t);
const std::tuple<uint8_t, std::string> getInfos() const;
const std::string getState(uint8_t) const;
util::SleeperThread thread_;
util::SleeperThread thread_timer_;

View File

@ -34,8 +34,8 @@ class Host {
GCancellable* cancellable_ = nullptr;
SnWatcher* watcher_ = nullptr;
const Json::Value &config_;
std::function<void(std::unique_ptr<Item>&)> on_add_;
std::function<void(std::unique_ptr<Item>&)> on_remove_;
const std::function<void(std::unique_ptr<Item>&)> on_add_;
const std::function<void(std::unique_ptr<Item>&)> on_remove_;
};
}

View File

@ -3,10 +3,11 @@
#include <iostream>
waybar::ALabel::ALabel(const Json::Value& config, const std::string format)
waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint16_t interval)
: config_(config),
format_(config_["format"].isString() ? config_["format"].asString() : format),
default_format_(format_)
interval_(std::chrono::seconds(config_["interval"].isUInt()
? config_["interval"].asUInt() : interval)), default_format_(format_)
{
event_box_.add(label_);
if (config_["max-length"].isUInt()) {

View File

@ -1,7 +1,7 @@
#include "modules/battery.hpp"
waybar::modules::Battery::Battery(const Json::Value& config)
: ALabel(config, "{capacity}%")
: ALabel(config, "{capacity}%", 60)
{
try {
if (config_["bat"].isString()) {
@ -46,9 +46,8 @@ void waybar::modules::Battery::worker()
{
// Trigger first values
update();
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
thread_timer_ = [this, interval] {
thread_.sleep_for(chrono::seconds(interval));
thread_timer_ = [this] {
thread_.sleep_for(interval_);
dp.emit();
};
thread_ = [this] {
@ -63,7 +62,7 @@ void waybar::modules::Battery::worker()
};
}
std::tuple<uint16_t, std::string> waybar::modules::Battery::getInfos()
const std::tuple<uint8_t, std::string> waybar::modules::Battery::getInfos() const
{
try {
uint16_t total = 0;
@ -86,10 +85,10 @@ std::tuple<uint16_t, std::string> waybar::modules::Battery::getInfos()
}
}
std::string waybar::modules::Battery::getState(uint16_t capacity)
const std::string waybar::modules::Battery::getState(uint8_t capacity) const
{
// Get current state
std::vector<std::pair<std::string, uint16_t>> states;
std::vector<std::pair<std::string, uint8_t>> states;
if (config_["states"].isObject()) {
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
if (it->isUInt() && it.key().isString()) {
@ -101,16 +100,16 @@ std::string waybar::modules::Battery::getState(uint16_t capacity)
std::sort(states.begin(), states.end(), [](auto &a, auto &b) {
return a.second < b.second;
});
std::string validState = "";
std::string valid_state;
for (auto state : states) {
if (capacity <= state.second && validState.empty()) {
if (capacity <= state.second && valid_state.empty()) {
label_.get_style_context()->add_class(state.first);
validState = state.first;
valid_state = state.first;
} else {
label_.get_style_context()->remove_class(state.first);
}
}
return validState;
return valid_state;
}
auto waybar::modules::Battery::update() -> void

View File

@ -1,16 +1,19 @@
#include "modules/clock.hpp"
#include <iostream>
waybar::modules::Clock::Clock(const Json::Value& config)
: ALabel(config, "{:%H:%M}")
: ALabel(config, "{:%H:%M}", 60)
{
label_.set_name("clock");
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
thread_ = [this, interval] {
thread_ = [this] {
auto now = waybar::chrono::clock::now();
dp.emit();
auto timeout = std::chrono::floor<std::chrono::seconds>(now
+ std::chrono::seconds(interval));
thread_.sleep_until(timeout);
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
auto time_s = std::chrono::time_point_cast<std::chrono::seconds>(timeout);
auto sub_m =
std::chrono::duration_cast<std::chrono::seconds>(time_s.time_since_epoch()).count() % 60;
thread_.sleep_until(timeout - std::chrono::seconds(sub_m - 1));
};
}

View File

@ -1,13 +1,12 @@
#include "modules/cpu.hpp"
waybar::modules::Cpu::Cpu(const Json::Value& config)
: ALabel(config, "{usage}%")
: ALabel(config, "{usage}%", 10)
{
label_.set_name("cpu");
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10;
thread_ = [this, interval] {
thread_ = [this] {
dp.emit();
thread_.sleep_for(chrono::seconds(interval));
thread_.sleep_for(interval_);
};
}

View File

@ -7,7 +7,7 @@ waybar::modules::Custom::Custom(const std::string name,
if (!config_["exec"].isString()) {
throw std::runtime_error(name_ + " has no exec path.");
}
if (config_["interval"].isUInt()) {
if (interval_.count() > 0) {
delayWorker();
} else {
continuousWorker();
@ -16,8 +16,7 @@ waybar::modules::Custom::Custom(const std::string name,
void waybar::modules::Custom::delayWorker()
{
auto interval = config_["interval"].asUInt();
thread_ = [this, interval] {
thread_ = [this] {
bool can_update = true;
if (config_["exec-if"].isString()) {
auto res = waybar::util::command::exec(config_["exec-if"].asString());
@ -31,7 +30,7 @@ void waybar::modules::Custom::delayWorker()
output_ = waybar::util::command::exec(config_["exec"].asString());
dp.emit();
}
thread_.sleep_for(chrono::seconds(interval));
thread_.sleep_for(interval_);
};
}

View File

@ -1,12 +1,11 @@
#include "modules/memory.hpp"
waybar::modules::Memory::Memory(const Json::Value& config)
: ALabel(config, "{}%")
: ALabel(config, "{}%", 30)
{
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30;
thread_ = [this, interval] {
thread_ = [this] {
dp.emit();
thread_.sleep_for(chrono::seconds(interval));
thread_.sleep_for(interval_);
};
}

View File

@ -1,7 +1,7 @@
#include "modules/network.hpp"
waybar::modules::Network::Network(const Json::Value& config)
: ALabel(config, "{ifname}"), family_(AF_INET),
: ALabel(config, "{ifname}", 60), family_(AF_INET),
signal_strength_dbm_(0), signal_strength_(0)
{
sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
@ -85,9 +85,8 @@ void waybar::modules::Network::worker()
dp.emit();
}
};
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
thread_timer_ = [this, interval] {
thread_.sleep_for(std::chrono::seconds(interval));
thread_timer_ = [this] {
thread_.sleep_for(interval_);
if (ifid_ > 0) {
getInfo();
dp.emit();