From dc4e4860bdaf46188428ccc9d0d682745518b96f Mon Sep 17 00:00:00 2001 From: Alexis Date: Thu, 9 Aug 2018 13:30:11 +0200 Subject: [PATCH] feat(config): format modules --- include/modules/battery.hpp | 4 +++- include/modules/clock.hpp | 4 +++- include/modules/cpu.hpp | 4 +++- include/modules/memory.hpp | 4 +++- resources/config | 11 ++++++++++- src/factory.cpp | 8 ++++---- src/modules/battery.cpp | 6 ++++-- src/modules/clock.cpp | 8 +++++--- src/modules/cpu.cpp | 8 +++++--- src/modules/memory.cpp | 8 +++++--- 10 files changed, 45 insertions(+), 20 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 83f6b737..dc86d511 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -14,7 +15,7 @@ namespace waybar::modules { class Battery : public IModule { public: - Battery(); + Battery(Json::Value config); auto update() -> void; operator Gtk::Widget&(); private: @@ -22,6 +23,7 @@ namespace waybar::modules { std::vector _batteries; util::SleeperThread _thread; Gtk::Label _label; + Json::Value _config; }; } diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index ec6abedc..b20c6816 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -10,12 +11,13 @@ namespace waybar::modules { class Clock : public IModule { public: - Clock(); + Clock(Json::Value config); auto update() -> void; operator Gtk::Widget &(); private: Gtk::Label _label; waybar::util::SleeperThread _thread; + Json::Value _config; }; } diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index e975ad56..56067644 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -11,12 +12,13 @@ namespace waybar::modules { class Cpu : public IModule { public: - Cpu(); + Cpu(Json::Value config); auto update() -> void; operator Gtk::Widget &(); private: Gtk::Label _label; waybar::util::SleeperThread _thread; + Json::Value _config; }; } diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 1c40dac3..eb62ebf8 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -11,12 +12,13 @@ namespace waybar::modules { class Memory : public IModule { public: - Memory(); + Memory(Json::Value config); auto update() -> void; operator Gtk::Widget &(); private: Gtk::Label _label; waybar::util::SleeperThread _thread; + Json::Value _config; }; } diff --git a/resources/config b/resources/config index e29c97e1..614726a2 100644 --- a/resources/config +++ b/resources/config @@ -1,4 +1,13 @@ { "modules-left": ["workspaces"], - "modules-right": ["cpu", "memory", "battery", "clock"] + "modules-right": ["cpu", "memory", "battery", "clock"], + "cpu": { + "format": "{}% " + }, + "memory": { + "format": "{}% " + }, + "battery": { + "format": "{}% " + } } diff --git a/src/factory.cpp b/src/factory.cpp index a181a6e8..2955332a 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -7,14 +7,14 @@ waybar::Factory::Factory(Bar &bar, Json::Value config) waybar::IModule &waybar::Factory::makeModule(std::string name) { if (name == "battery") - return *new waybar::modules::Battery(); + return *new waybar::modules::Battery(_config[name]); if (name == "workspaces") return *new waybar::modules::Workspaces(_bar); if (name == "memory") - return *new waybar::modules::Memory(); + return *new waybar::modules::Memory(_config[name]); if (name == "cpu") - return *new waybar::modules::Cpu(); + return *new waybar::modules::Cpu(_config[name]); if (name == "clock") - return *new waybar::modules::Clock(); + return *new waybar::modules::Clock(_config[name]); throw std::runtime_error("Unknown module: " + name); } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index a66718b6..2836f54c 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -1,6 +1,7 @@ #include "modules/battery.hpp" -waybar::modules::Battery::Battery() +waybar::modules::Battery::Battery(Json::Value config) + : _config(config) { try { for (auto &node : fs::directory_iterator(_data_dir)) { @@ -41,7 +42,8 @@ auto waybar::modules::Battery::update() -> void } else { _label.get_style_context()->remove_class("charging"); } - _label.set_text(fmt::format("{}% ", total / _batteries.size())); + auto format = _config["format"] ? _config["format"].asString() : "{}%"; + _label.set_text(fmt::format(format, total / _batteries.size())); } catch (std::exception &e) { std::cerr << e.what() << std::endl; } diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index c652e844..c84dc941 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,6 +1,7 @@ #include "modules/clock.hpp" -waybar::modules::Clock::Clock() +waybar::modules::Clock::Clock(Json::Value config) + : _config(config) { _label.get_style_context()->add_class("clock"); _thread = [this] { @@ -16,8 +17,9 @@ auto waybar::modules::Clock::update() -> void { auto t = std::time(nullptr); auto localtime = std::localtime(&t); - _label.set_text( - fmt::format("{:02}:{:02}", localtime->tm_hour, localtime->tm_min)); + auto format = + _config["format"] ? _config["format"].asString() : "{:02}:{:02}"; + _label.set_text(fmt::format(format, localtime->tm_hour, localtime->tm_min)); } waybar::modules::Clock::operator Gtk::Widget &() { diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index cde31287..20e117be 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -1,7 +1,8 @@ #include "modules/cpu.hpp" #include -waybar::modules::Cpu::Cpu() +waybar::modules::Cpu::Cpu(Json::Value config) + : _config(config) { _label.get_style_context()->add_class("cpu"); _thread = [this] { @@ -15,8 +16,9 @@ auto waybar::modules::Cpu::update() -> void struct sysinfo info; if (!sysinfo(&info)) { float f_load = 1.f / (1 << SI_LOAD_SHIFT); - _label.set_text(fmt::format("{:.{}f}% ", - info.loads[0] * f_load * 100 / get_nprocs(), 0)); + int load = info.loads[0] * f_load * 100 / get_nprocs(); + auto format = _config["format"] ? _config["format"].asString() : "{}%"; + _label.set_text(fmt::format(format, load)); } } diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 104c973c..bb981f42 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -1,7 +1,8 @@ #include "modules/memory.hpp" #include -waybar::modules::Memory::Memory() +waybar::modules::Memory::Memory(Json::Value config) + : _config(config) { _label.get_style_context()->add_class("memory"); _thread = [this] { @@ -14,8 +15,9 @@ auto waybar::modules::Memory::update() -> void { struct sysinfo info; if (!sysinfo(&info)) { - double available = (double)info.freeram / (double)info.totalram; - _label.set_text(fmt::format("{:.{}f}% ", available * 100, 0)); + int available = ((double)info.freeram / (double)info.totalram) * 100; + auto format = _config["format"] ? _config["format"].asString() : "{}%"; + _label.set_text(fmt::format(format, available)); } }