From 472363a6239397184270bbcaa0c3de60c6298ab5 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 1 May 2019 22:28:48 -0400 Subject: [PATCH] feat(percent): adds a percent class to numeric modules --- include/modules/backlight.hpp | 2 ++ include/modules/battery.hpp | 1 + include/modules/cpu.hpp | 2 ++ src/modules/backlight.cpp | 5 +++++ src/modules/battery.cpp | 5 +++++ src/modules/cpu.cpp | 4 ++++ 6 files changed, 19 insertions(+) diff --git a/include/modules/backlight.hpp b/include/modules/backlight.hpp index 42e38f2..6576ecc 100644 --- a/include/modules/backlight.hpp +++ b/include/modules/backlight.hpp @@ -51,6 +51,8 @@ class Backlight : public ALabel { const std::string preferred_device_; static constexpr int EPOLL_MAX_EVENTS = 16; + std::string old_percent_; + std::optional previous_best_; std::string previous_format_; diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 4b39744..09601f7 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -43,6 +43,7 @@ class Battery : public ALabel { int fd_; std::vector wds_; std::string old_status_; + std::string old_capacity_; }; } // namespace waybar::modules diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 5c17d3a..c7aa195 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -23,6 +23,8 @@ class Cpu : public ALabel { std::tuple getCpuUsage(); std::vector> parseCpuinfo(); + std::string old_usage_; + std::vector> prev_times_; waybar::util::SleeperThread thread_; }; diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index ea09b4e..576c682 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -179,8 +179,13 @@ auto waybar::modules::Backlight::update() -> void { } const auto percent = best->get_max() == 0 ? 100 : best->get_actual() * 100 / best->get_max(); + const auto percentClass = "percent" + std::to_string(int(std::ceil(percent/5)*5)); + label_.set_markup(fmt::format( format_, fmt::arg("percent", std::to_string(percent)), fmt::arg("icon", getIcon(percent)))); + label_.get_style_context()->remove_class(old_percent_); + label_.get_style_context()->add_class(percentClass); + old_percent_ = percentClass; } else { if (!previous_best_.has_value()) { return; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index f2e6a88..148865e 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -144,8 +144,13 @@ auto waybar::modules::Battery::update() -> void { std::transform(status.begin(), status.end(), status.begin(), ::tolower); auto format = format_; auto state = getState(capacity); + auto capacity_class = "percent" + std::to_string(int(std::ceil(capacity/5)*5)); label_.get_style_context()->remove_class(old_status_); + label_.get_style_context()->remove_class(old_capacity_); label_.get_style_context()->add_class(status); + label_.get_style_context()->add_class(capacity_class); + old_capacity_ = capacity_class; + std::cout << fmt::format(capacity_class); old_status_ = status; if (!state.empty() && config_["format-" + status + "-" + state].isString()) { format = config_["format-" + status + "-" + state].asString(); diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 880a05a..f0badf3 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -19,7 +19,11 @@ auto waybar::modules::Cpu::update() -> void { if (tooltipEnabled()) { label_.set_tooltip_text(tooltip); } + auto percent_class = "percent" + std::to_string(int(std::ceil(cpu_usage/5)*5)); label_.set_markup(fmt::format(format_, fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage))); + label_.get_style_context()->remove_class(old_usage_); + label_.get_style_context()->add_class(percent_class); + old_usage_ = percent_class; } uint16_t waybar::modules::Cpu::getCpuLoad() {