feat(config): format modules

This commit is contained in:
Alexis 2018-08-09 13:30:11 +02:00
parent 45832fcbe3
commit dc4e4860bd
10 changed files with 45 additions and 20 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <json/json.h>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <gtkmm.h> #include <gtkmm.h>
@ -14,7 +15,7 @@ namespace waybar::modules {
class Battery : public IModule { class Battery : public IModule {
public: public:
Battery(); Battery(Json::Value config);
auto update() -> void; auto update() -> void;
operator Gtk::Widget&(); operator Gtk::Widget&();
private: private:
@ -22,6 +23,7 @@ namespace waybar::modules {
std::vector<fs::path> _batteries; std::vector<fs::path> _batteries;
util::SleeperThread _thread; util::SleeperThread _thread;
Gtk::Label _label; Gtk::Label _label;
Json::Value _config;
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <json/json.h>
#include <gtkmm.h> #include <gtkmm.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <thread> #include <thread>
@ -10,12 +11,13 @@ namespace waybar::modules {
class Clock : public IModule { class Clock : public IModule {
public: public:
Clock(); Clock(Json::Value config);
auto update() -> void; auto update() -> void;
operator Gtk::Widget &(); operator Gtk::Widget &();
private: private:
Gtk::Label _label; Gtk::Label _label;
waybar::util::SleeperThread _thread; waybar::util::SleeperThread _thread;
Json::Value _config;
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <json/json.h>
#include <gtkmm.h> #include <gtkmm.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
@ -11,12 +12,13 @@ namespace waybar::modules {
class Cpu : public IModule { class Cpu : public IModule {
public: public:
Cpu(); Cpu(Json::Value config);
auto update() -> void; auto update() -> void;
operator Gtk::Widget &(); operator Gtk::Widget &();
private: private:
Gtk::Label _label; Gtk::Label _label;
waybar::util::SleeperThread _thread; waybar::util::SleeperThread _thread;
Json::Value _config;
}; };
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <json/json.h>
#include <gtkmm.h> #include <gtkmm.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
@ -11,12 +12,13 @@ namespace waybar::modules {
class Memory : public IModule { class Memory : public IModule {
public: public:
Memory(); Memory(Json::Value config);
auto update() -> void; auto update() -> void;
operator Gtk::Widget &(); operator Gtk::Widget &();
private: private:
Gtk::Label _label; Gtk::Label _label;
waybar::util::SleeperThread _thread; waybar::util::SleeperThread _thread;
Json::Value _config;
}; };
} }

View File

@ -1,4 +1,13 @@
{ {
"modules-left": ["workspaces"], "modules-left": ["workspaces"],
"modules-right": ["cpu", "memory", "battery", "clock"] "modules-right": ["cpu", "memory", "battery", "clock"],
"cpu": {
"format": "{}% "
},
"memory": {
"format": "{}% "
},
"battery": {
"format": "{}% "
}
} }

View File

@ -7,14 +7,14 @@ waybar::Factory::Factory(Bar &bar, Json::Value config)
waybar::IModule &waybar::Factory::makeModule(std::string name) waybar::IModule &waybar::Factory::makeModule(std::string name)
{ {
if (name == "battery") if (name == "battery")
return *new waybar::modules::Battery(); return *new waybar::modules::Battery(_config[name]);
if (name == "workspaces") if (name == "workspaces")
return *new waybar::modules::Workspaces(_bar); return *new waybar::modules::Workspaces(_bar);
if (name == "memory") if (name == "memory")
return *new waybar::modules::Memory(); return *new waybar::modules::Memory(_config[name]);
if (name == "cpu") if (name == "cpu")
return *new waybar::modules::Cpu(); return *new waybar::modules::Cpu(_config[name]);
if (name == "clock") if (name == "clock")
return *new waybar::modules::Clock(); return *new waybar::modules::Clock(_config[name]);
throw std::runtime_error("Unknown module: " + name); throw std::runtime_error("Unknown module: " + name);
} }

View File

@ -1,6 +1,7 @@
#include "modules/battery.hpp" #include "modules/battery.hpp"
waybar::modules::Battery::Battery() waybar::modules::Battery::Battery(Json::Value config)
: _config(config)
{ {
try { try {
for (auto &node : fs::directory_iterator(_data_dir)) { for (auto &node : fs::directory_iterator(_data_dir)) {
@ -41,7 +42,8 @@ auto waybar::modules::Battery::update() -> void
} else { } else {
_label.get_style_context()->remove_class("charging"); _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) { } catch (std::exception &e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }

View File

@ -1,6 +1,7 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
waybar::modules::Clock::Clock() waybar::modules::Clock::Clock(Json::Value config)
: _config(config)
{ {
_label.get_style_context()->add_class("clock"); _label.get_style_context()->add_class("clock");
_thread = [this] { _thread = [this] {
@ -16,8 +17,9 @@ auto waybar::modules::Clock::update() -> void
{ {
auto t = std::time(nullptr); auto t = std::time(nullptr);
auto localtime = std::localtime(&t); auto localtime = std::localtime(&t);
_label.set_text( auto format =
fmt::format("{:02}:{:02}", localtime->tm_hour, localtime->tm_min)); _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 &() { waybar::modules::Clock::operator Gtk::Widget &() {

View File

@ -1,7 +1,8 @@
#include "modules/cpu.hpp" #include "modules/cpu.hpp"
#include <iostream> #include <iostream>
waybar::modules::Cpu::Cpu() waybar::modules::Cpu::Cpu(Json::Value config)
: _config(config)
{ {
_label.get_style_context()->add_class("cpu"); _label.get_style_context()->add_class("cpu");
_thread = [this] { _thread = [this] {
@ -15,8 +16,9 @@ auto waybar::modules::Cpu::update() -> void
struct sysinfo info; struct sysinfo info;
if (!sysinfo(&info)) { if (!sysinfo(&info)) {
float f_load = 1.f / (1 << SI_LOAD_SHIFT); float f_load = 1.f / (1 << SI_LOAD_SHIFT);
_label.set_text(fmt::format("{:.{}f}% ", int load = info.loads[0] * f_load * 100 / get_nprocs();
info.loads[0] * f_load * 100 / get_nprocs(), 0)); auto format = _config["format"] ? _config["format"].asString() : "{}%";
_label.set_text(fmt::format(format, load));
} }
} }

View File

@ -1,7 +1,8 @@
#include "modules/memory.hpp" #include "modules/memory.hpp"
#include <iostream> #include <iostream>
waybar::modules::Memory::Memory() waybar::modules::Memory::Memory(Json::Value config)
: _config(config)
{ {
_label.get_style_context()->add_class("memory"); _label.get_style_context()->add_class("memory");
_thread = [this] { _thread = [this] {
@ -14,8 +15,9 @@ auto waybar::modules::Memory::update() -> void
{ {
struct sysinfo info; struct sysinfo info;
if (!sysinfo(&info)) { if (!sysinfo(&info)) {
double available = (double)info.freeram / (double)info.totalram; int available = ((double)info.freeram / (double)info.totalram) * 100;
_label.set_text(fmt::format("{:.{}f}% ", available * 100, 0)); auto format = _config["format"] ? _config["format"].asString() : "{}%";
_label.set_text(fmt::format(format, available));
} }
} }