cpu module: Reuse getLoad of load module

pull/2114/head
Mann mit Hut 2023-07-29 16:02:36 +02:00
parent d1602e383c
commit 8d7341da6e
No known key found for this signature in database
GPG Key ID: 02D4D60B8250CB8C
4 changed files with 7 additions and 14 deletions

View File

@ -21,8 +21,6 @@ class Cpu : public ALabel {
auto update() -> void override; auto update() -> void override;
private: private:
double getCpuLoad();
std::vector<std::tuple<size_t, size_t>> prev_times_; std::vector<std::tuple<size_t, size_t>> prev_times_;
util::SleeperThread thread_; util::SleeperThread thread_;

View File

@ -20,8 +20,10 @@ class Load : public ALabel {
virtual ~Load() = default; virtual ~Load() = default;
auto update() -> void override; auto update() -> void override;
// This is a static member because it is also used by the cpu module.
static std::tuple<double, double, double> getLoad();
private: private:
std::tuple<double, double, double> getLoad();
util::SleeperThread thread_; util::SleeperThread thread_;
}; };

View File

@ -1,6 +1,7 @@
#include "modules/cpu.hpp" #include "modules/cpu.hpp"
#include "modules/cpu_frequency.hpp" #include "modules/cpu_frequency.hpp"
#include "modules/cpu_usage.hpp" #include "modules/cpu_usage.hpp"
#include "modules/load.hpp"
// In the 80000 version of fmt library authors decided to optimize imports // In the 80000 version of fmt library authors decided to optimize imports
// and moved declarations required for fmt::dynamic_format_arg_store in new // and moved declarations required for fmt::dynamic_format_arg_store in new
@ -21,7 +22,7 @@ waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
auto waybar::modules::Cpu::update() -> void { auto waybar::modules::Cpu::update() -> void {
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
auto cpu_load = getCpuLoad(); auto [load1, load5, load15] = Load::getLoad();
auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_); auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_);
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency(); auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency();
if (tooltipEnabled()) { if (tooltipEnabled()) {
@ -40,7 +41,7 @@ auto waybar::modules::Cpu::update() -> void {
event_box_.show(); event_box_.show();
auto icons = std::vector<std::string>{state}; auto icons = std::vector<std::string>{state};
fmt::dynamic_format_arg_store<fmt::format_context> store; fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("load", cpu_load)); store.push_back(fmt::arg("load", load1));
store.push_back(fmt::arg("usage", total_usage)); store.push_back(fmt::arg("usage", total_usage));
store.push_back(fmt::arg("icon", getIcon(total_usage, icons))); store.push_back(fmt::arg("icon", getIcon(total_usage, icons)));
store.push_back(fmt::arg("max_frequency", max_frequency)); store.push_back(fmt::arg("max_frequency", max_frequency));
@ -59,11 +60,3 @@ auto waybar::modules::Cpu::update() -> void {
// Call parent update // Call parent update
ALabel::update(); ALabel::update();
} }
double waybar::modules::Cpu::getCpuLoad() {
double load[1];
if (getloadavg(load, 1) != -1) {
return std::ceil(load[0] * 100.0) / 100.0;
}
throw std::runtime_error("Can't get Cpu load");
}

View File

@ -19,7 +19,7 @@ waybar::modules::Load::Load(const std::string& id, const Json::Value& config)
auto waybar::modules::Load::update() -> void { auto waybar::modules::Load::update() -> void {
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
auto [load1, load5, load15] = getLoad(); auto [load1, load5, load15] = Load::getLoad();
if (tooltipEnabled()) { if (tooltipEnabled()) {
auto tooltip = fmt::format("Load 1: {}\nLoad 5: {}\nLoad 15: {}", load1, load5, load15); auto tooltip = fmt::format("Load 1: {}\nLoad 5: {}\nLoad 15: {}", load1, load5, load15);
label_.set_tooltip_text(tooltip); label_.set_tooltip_text(tooltip);