diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 83f6b73..dc86d51 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 ec6abed..b20c681 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 e975ad5..5606764 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 1c40dac..eb62ebf 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 e29c97e..614726a 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 a181a6e..2955332 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 a66718b..2836f54 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 c652e84..c84dc94 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 cde3128..20e117b 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 104c973..bb981f4 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)); } }