feat(config): format modules

pull/2/head
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
#include <json/json.h>
#include <filesystem>
#include <fstream>
#include <gtkmm.h>
@ -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<fs::path> _batteries;
util::SleeperThread _thread;
Gtk::Label _label;
Json::Value _config;
};
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <json/json.h>
#include <gtkmm.h>
#include <fmt/format.h>
#include <thread>
@ -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;
};
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <json/json.h>
#include <gtkmm.h>
#include <fmt/format.h>
#include <sys/sysinfo.h>
@ -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;
};
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <json/json.h>
#include <gtkmm.h>
#include <fmt/format.h>
#include <sys/sysinfo.h>
@ -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;
};
}

View File

@ -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": "{}% "
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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 &() {

View File

@ -1,7 +1,8 @@
#include "modules/cpu.hpp"
#include <iostream>
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));
}
}

View File

@ -1,7 +1,8 @@
#include "modules/memory.hpp"
#include <iostream>
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));
}
}