refactor(ALabel): add interval
This commit is contained in:
parent
36652158ad
commit
ad7400d5ce
|
@ -7,7 +7,7 @@ namespace waybar {
|
||||||
|
|
||||||
class ALabel : public IModule {
|
class ALabel : public IModule {
|
||||||
public:
|
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 ~ALabel() = default;
|
||||||
virtual auto update() -> void;
|
virtual auto update() -> void;
|
||||||
virtual std::string getIcon(uint16_t, const std::string& alt = "");
|
virtual std::string getIcon(uint16_t, const std::string& alt = "");
|
||||||
|
@ -19,6 +19,7 @@ class ALabel : public IModule {
|
||||||
const Json::Value& config_;
|
const Json::Value& config_;
|
||||||
std::string format_;
|
std::string format_;
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
|
const std::chrono::seconds interval_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool handleToggle(GdkEventButton* const& ev);
|
bool handleToggle(GdkEventButton* const& ev);
|
||||||
|
|
|
@ -30,8 +30,8 @@ class Battery : public ALabel {
|
||||||
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
|
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
|
||||||
|
|
||||||
void worker();
|
void worker();
|
||||||
std::tuple<uint16_t, std::string> getInfos();
|
const std::tuple<uint8_t, std::string> getInfos() const;
|
||||||
std::string getState(uint16_t);
|
const std::string getState(uint8_t) const;
|
||||||
|
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
util::SleeperThread thread_timer_;
|
util::SleeperThread thread_timer_;
|
||||||
|
|
|
@ -34,8 +34,8 @@ class Host {
|
||||||
GCancellable* cancellable_ = nullptr;
|
GCancellable* cancellable_ = nullptr;
|
||||||
SnWatcher* watcher_ = nullptr;
|
SnWatcher* watcher_ = nullptr;
|
||||||
const Json::Value &config_;
|
const Json::Value &config_;
|
||||||
std::function<void(std::unique_ptr<Item>&)> on_add_;
|
const 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_remove_;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,11 @@
|
||||||
|
|
||||||
#include <iostream>
|
#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),
|
: config_(config),
|
||||||
format_(config_["format"].isString() ? config_["format"].asString() : format),
|
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_);
|
event_box_.add(label_);
|
||||||
if (config_["max-length"].isUInt()) {
|
if (config_["max-length"].isUInt()) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "modules/battery.hpp"
|
#include "modules/battery.hpp"
|
||||||
|
|
||||||
waybar::modules::Battery::Battery(const Json::Value& config)
|
waybar::modules::Battery::Battery(const Json::Value& config)
|
||||||
: ALabel(config, "{capacity}%")
|
: ALabel(config, "{capacity}%", 60)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (config_["bat"].isString()) {
|
if (config_["bat"].isString()) {
|
||||||
|
@ -46,9 +46,8 @@ void waybar::modules::Battery::worker()
|
||||||
{
|
{
|
||||||
// Trigger first values
|
// Trigger first values
|
||||||
update();
|
update();
|
||||||
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
|
thread_timer_ = [this] {
|
||||||
thread_timer_ = [this, interval] {
|
thread_.sleep_for(interval_);
|
||||||
thread_.sleep_for(chrono::seconds(interval));
|
|
||||||
dp.emit();
|
dp.emit();
|
||||||
};
|
};
|
||||||
thread_ = [this] {
|
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 {
|
try {
|
||||||
uint16_t total = 0;
|
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
|
// 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()) {
|
if (config_["states"].isObject()) {
|
||||||
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
|
for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) {
|
||||||
if (it->isUInt() && it.key().isString()) {
|
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) {
|
std::sort(states.begin(), states.end(), [](auto &a, auto &b) {
|
||||||
return a.second < b.second;
|
return a.second < b.second;
|
||||||
});
|
});
|
||||||
std::string validState = "";
|
std::string valid_state;
|
||||||
for (auto state : states) {
|
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);
|
label_.get_style_context()->add_class(state.first);
|
||||||
validState = state.first;
|
valid_state = state.first;
|
||||||
} else {
|
} else {
|
||||||
label_.get_style_context()->remove_class(state.first);
|
label_.get_style_context()->remove_class(state.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return validState;
|
return valid_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto waybar::modules::Battery::update() -> void
|
auto waybar::modules::Battery::update() -> void
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
#include "modules/clock.hpp"
|
#include "modules/clock.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
waybar::modules::Clock::Clock(const Json::Value& config)
|
waybar::modules::Clock::Clock(const Json::Value& config)
|
||||||
: ALabel(config, "{:%H:%M}")
|
: ALabel(config, "{:%H:%M}", 60)
|
||||||
{
|
{
|
||||||
label_.set_name("clock");
|
label_.set_name("clock");
|
||||||
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
|
thread_ = [this] {
|
||||||
thread_ = [this, interval] {
|
|
||||||
auto now = waybar::chrono::clock::now();
|
auto now = waybar::chrono::clock::now();
|
||||||
dp.emit();
|
dp.emit();
|
||||||
auto timeout = std::chrono::floor<std::chrono::seconds>(now
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
||||||
+ std::chrono::seconds(interval));
|
auto time_s = std::chrono::time_point_cast<std::chrono::seconds>(timeout);
|
||||||
thread_.sleep_until(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));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
#include "modules/cpu.hpp"
|
#include "modules/cpu.hpp"
|
||||||
|
|
||||||
waybar::modules::Cpu::Cpu(const Json::Value& config)
|
waybar::modules::Cpu::Cpu(const Json::Value& config)
|
||||||
: ALabel(config, "{usage}%")
|
: ALabel(config, "{usage}%", 10)
|
||||||
{
|
{
|
||||||
label_.set_name("cpu");
|
label_.set_name("cpu");
|
||||||
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10;
|
thread_ = [this] {
|
||||||
thread_ = [this, interval] {
|
|
||||||
dp.emit();
|
dp.emit();
|
||||||
thread_.sleep_for(chrono::seconds(interval));
|
thread_.sleep_for(interval_);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ waybar::modules::Custom::Custom(const std::string name,
|
||||||
if (!config_["exec"].isString()) {
|
if (!config_["exec"].isString()) {
|
||||||
throw std::runtime_error(name_ + " has no exec path.");
|
throw std::runtime_error(name_ + " has no exec path.");
|
||||||
}
|
}
|
||||||
if (config_["interval"].isUInt()) {
|
if (interval_.count() > 0) {
|
||||||
delayWorker();
|
delayWorker();
|
||||||
} else {
|
} else {
|
||||||
continuousWorker();
|
continuousWorker();
|
||||||
|
@ -16,8 +16,7 @@ waybar::modules::Custom::Custom(const std::string name,
|
||||||
|
|
||||||
void waybar::modules::Custom::delayWorker()
|
void waybar::modules::Custom::delayWorker()
|
||||||
{
|
{
|
||||||
auto interval = config_["interval"].asUInt();
|
thread_ = [this] {
|
||||||
thread_ = [this, interval] {
|
|
||||||
bool can_update = true;
|
bool can_update = true;
|
||||||
if (config_["exec-if"].isString()) {
|
if (config_["exec-if"].isString()) {
|
||||||
auto res = waybar::util::command::exec(config_["exec-if"].asString());
|
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());
|
output_ = waybar::util::command::exec(config_["exec"].asString());
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
thread_.sleep_for(chrono::seconds(interval));
|
thread_.sleep_for(interval_);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#include "modules/memory.hpp"
|
#include "modules/memory.hpp"
|
||||||
|
|
||||||
waybar::modules::Memory::Memory(const Json::Value& config)
|
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] {
|
||||||
thread_ = [this, interval] {
|
|
||||||
dp.emit();
|
dp.emit();
|
||||||
thread_.sleep_for(chrono::seconds(interval));
|
thread_.sleep_for(interval_);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "modules/network.hpp"
|
#include "modules/network.hpp"
|
||||||
|
|
||||||
waybar::modules::Network::Network(const Json::Value& config)
|
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)
|
signal_strength_dbm_(0), signal_strength_(0)
|
||||||
{
|
{
|
||||||
sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
||||||
|
@ -85,9 +85,8 @@ void waybar::modules::Network::worker()
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 60;
|
thread_timer_ = [this] {
|
||||||
thread_timer_ = [this, interval] {
|
thread_.sleep_for(interval_);
|
||||||
thread_.sleep_for(std::chrono::seconds(interval));
|
|
||||||
if (ifid_ > 0) {
|
if (ifid_ > 0) {
|
||||||
getInfo();
|
getInfo();
|
||||||
dp.emit();
|
dp.emit();
|
||||||
|
|
Loading…
Reference in New Issue