feat(modules): generic label module to allow max-length on all labels

This commit is contained in:
Alexis 2018-08-18 11:43:48 +02:00
parent c128562284
commit b1fd4d7b82
19 changed files with 76 additions and 99 deletions

19
include/ALabel.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <json/json.h>
#include "IModule.hpp"
namespace waybar {
class ALabel : public IModule {
public:
ALabel(Json::Value);
virtual ~ALabel();
virtual auto update() -> void;
virtual operator Gtk::Widget &();
protected:
Gtk::Label label_;
Json::Value config_;
};
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <json/json.h>
#include <filesystem>
#include <fstream>
#include <iostream>
@ -8,24 +7,21 @@
#include <sys/inotify.h>
#include <algorithm>
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
namespace fs = std::filesystem;
class Battery : public IModule {
class Battery : public ALabel {
public:
Battery(Json::Value);
auto update() -> void;
operator Gtk::Widget&();
private:
std::string getIcon(uint16_t percentage);
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
Gtk::Label label_;
Json::Value config_;
util::SleeperThread thread_;
std::vector<fs::path> batteries_;
};

View File

@ -1,22 +1,18 @@
#pragma once
#include <json/json.h>
#include <fmt/format.h>
#include "fmt/time.h"
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Clock : public IModule {
class Clock : public ALabel {
public:
Clock(Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
Gtk::Label label_;
waybar::util::SleeperThread thread_;
Json::Value config_;
};
}

View File

@ -1,22 +1,18 @@
#pragma once
#include <json/json.h>
#include <fmt/format.h>
#include <sys/sysinfo.h>
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Cpu : public IModule {
class Cpu : public ALabel {
public:
Cpu(Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
Gtk::Label label_;
waybar::util::SleeperThread thread_;
Json::Value config_;
};
}

View File

@ -1,22 +1,18 @@
#pragma once
#include <json/json.h>
#include <fmt/format.h>
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Custom : public IModule {
class Custom : public ALabel {
public:
Custom(const std::string&, Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
const std::string name_;
Gtk::Label label_;
waybar::util::SleeperThread thread_;
Json::Value config_;
};
}

View File

@ -1,22 +1,18 @@
#pragma once
#include <json/json.h>
#include <fmt/format.h>
#include <sys/sysinfo.h>
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Memory : public IModule {
class Memory : public ALabel {
public:
Memory(Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
Gtk::Label label_;
waybar::util::SleeperThread thread_;
Json::Value config_;
};
}

View File

@ -5,18 +5,16 @@
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/nl80211.h>
#include <json/json.h>
#include <fmt/format.h>
#include "util/chrono.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Network : public IModule {
class Network : public ALabel {
public:
Network(Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
static uint64_t netlinkRequest(int, void*, uint32_t, uint32_t groups = 0);
static uint64_t netlinkResponse(int, void*, uint32_t, uint32_t groups = 0);
@ -29,10 +27,7 @@ class Network : public IModule {
bool associatedOrJoined(struct nlattr**);
auto getInfo() -> void;
Gtk::Label label_;
waybar::util::SleeperThread thread_;
Json::Value config_;
int ifid_;
sa_family_t family_;
int sock_fd_;

View File

@ -1,18 +1,16 @@
#pragma once
#include <pulse/pulseaudio.h>
#include <json/json.h>
#include <fmt/format.h>
#include <algorithm>
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules {
class Pulseaudio : public IModule {
class Pulseaudio : public ALabel {
public:
Pulseaudio(Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
static void subscribeCb(pa_context*, pa_subscription_event_type_t,
uint32_t, void*);
@ -22,8 +20,6 @@ class Pulseaudio : public IModule {
std::string getIcon(uint16_t);
Gtk::Label label_;
Json::Value config_;
pa_threaded_mainloop* mainloop_;
pa_mainloop_api* mainloop_api_;
pa_context* context_;

View File

@ -5,23 +5,20 @@
#include "client.hpp"
#include "util/chrono.hpp"
#include "util/json.hpp"
#include "IModule.hpp"
#include "ALabel.hpp"
namespace waybar::modules::sway {
class Window : public IModule {
class Window : public ALabel {
public:
Window(waybar::Bar&, Json::Value);
auto update() -> void;
operator Gtk::Widget &();
private:
std::string getFocusedNode(Json::Value nodes);
void getFocusedWindow();
Bar& bar_;
Json::Value config_;
waybar::util::SleeperThread thread_;
Gtk::Label label_;
util::JsonParser parser_;
int ipcfd_;
int ipc_eventfd_;

View File

@ -19,6 +19,9 @@
// "5": ""
// }
},
"sway/window": {
"max-length": 50
},
"cpu": {
"format": "{}% "
},

24
src/ALabel.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "ALabel.hpp"
waybar::ALabel::ALabel(Json::Value config)
: config_(std::move(config))
{
if (config_["max-length"]) {
label_.set_max_width_chars(config_["max-length"].asUInt());
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
}
}
waybar::ALabel::~ALabel()
{
// Nothing here
}
auto waybar::ALabel::update() -> void
{
// Nothing here
}
waybar::ALabel::operator Gtk::Widget &() {
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/battery.hpp"
waybar::modules::Battery::Battery(Json::Value config)
: config_(std::move(config))
: ALabel(std::move(config))
{
try {
for (auto &node : fs::directory_iterator(data_dir_)) {
@ -83,8 +83,3 @@ std::string waybar::modules::Battery::getIcon(uint16_t percentage)
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return config_["format-icons"][idx].asString();
}
waybar::modules::Battery::operator Gtk::Widget &()
{
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/clock.hpp"
waybar::modules::Clock::Clock(Json::Value config)
: config_(std::move(config))
: ALabel(std::move(config))
{
label_.set_name("clock");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60;
@ -12,7 +12,7 @@ waybar::modules::Clock::Clock(Json::Value config)
+ std::chrono::seconds(interval));
thread_.sleep_until(timeout);
};
};
}
auto waybar::modules::Clock::update() -> void
{
@ -20,7 +20,3 @@ auto waybar::modules::Clock::update() -> void
auto format = config_["format"] ? config_["format"].asString() : "{:%H:%M}";
label_.set_text(fmt::format(format, localtime));
}
waybar::modules::Clock::operator Gtk::Widget &() {
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/cpu.hpp"
waybar::modules::Cpu::Cpu(Json::Value config)
: config_(std::move(config))
: ALabel(std::move(config))
{
label_.set_name("cpu");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10;
@ -9,7 +9,7 @@ waybar::modules::Cpu::Cpu(Json::Value config)
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update));
thread_.sleep_for(chrono::seconds(interval));
};
};
}
auto waybar::modules::Cpu::update() -> void
{
@ -21,7 +21,3 @@ auto waybar::modules::Cpu::update() -> void
label_.set_text(fmt::format(format, load));
}
}
waybar::modules::Cpu::operator Gtk::Widget &() {
return label_;
}

View File

@ -2,21 +2,17 @@
#include <iostream>
waybar::modules::Custom::Custom(const std::string &name, Json::Value config)
: name_(name), config_(std::move(config))
: ALabel(std::move(config)), name_(name)
{
if (!config_["exec"]) {
throw std::runtime_error(name_ + " has no exec path.");
}
if (config_["max-length"]) {
label_.set_max_width_chars(config_["max-length"].asUInt());
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
}
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Custom::update));
thread_.sleep_for(chrono::seconds(interval));
};
};
}
auto waybar::modules::Custom::update() -> void
{
@ -52,7 +48,3 @@ auto waybar::modules::Custom::update() -> void
label_.show();
}
}
waybar::modules::Custom::operator Gtk::Widget &() {
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/memory.hpp"
waybar::modules::Memory::Memory(Json::Value config)
: config_(std::move(config))
: ALabel(std::move(config))
{
label_.set_name("memory");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
@ -9,7 +9,7 @@ waybar::modules::Memory::Memory(Json::Value config)
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update));
thread_.sleep_for(chrono::seconds(interval));
};
};
}
auto waybar::modules::Memory::update() -> void
{
@ -24,7 +24,3 @@ auto waybar::modules::Memory::update() -> void
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
}
}
waybar::modules::Memory::operator Gtk::Widget &() {
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/network.hpp"
waybar::modules::Network::Network(Json::Value config)
: config_(std::move(config)), family_(AF_INET),
: ALabel(std::move(config)), family_(AF_INET),
signal_strength_dbm_(0), signal_strength_(0)
{
sock_fd_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
@ -70,7 +70,7 @@ waybar::modules::Network::Network(Json::Value config)
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update));
}
};
};
}
auto waybar::modules::Network::update() -> void
{
@ -381,7 +381,3 @@ auto waybar::modules::Network::getInfo() -> void
nl_send_sync(sk, msg);
nl_socket_free(sk);
}
waybar::modules::Network::operator Gtk::Widget &() {
return label_;
}

View File

@ -1,7 +1,7 @@
#include "modules/pulseaudio.hpp"
waybar::modules::Pulseaudio::Pulseaudio(Json::Value config)
: config_(std::move(config)), mainloop_(nullptr), mainloop_api_(nullptr),
: ALabel(std::move(config)), mainloop_(nullptr), mainloop_api_(nullptr),
context_(nullptr), sink_idx_(0), volume_(0), muted_(false)
{
label_.set_name("pulseaudio");
@ -26,7 +26,7 @@ waybar::modules::Pulseaudio::Pulseaudio(Json::Value config)
throw std::runtime_error("pa_mainloop_run() failed.");
}
pa_threaded_mainloop_unlock(mainloop_);
};
}
void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data)
{
@ -123,7 +123,3 @@ std::string waybar::modules::Pulseaudio::getIcon(uint16_t percentage)
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
return config_["format-icons"][idx].asString();
}
waybar::modules::Pulseaudio::operator Gtk::Widget &() {
return label_;
}

View File

@ -2,7 +2,7 @@
#include "modules/sway/ipc/client.hpp"
waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
: bar_(bar), config_(std::move(config))
: ALabel(std::move(config)), bar_(bar)
{
label_.set_name("window");
std::string socketPath = getSocketPath();
@ -59,7 +59,3 @@ void waybar::modules::sway::Window::getFocusedWindow()
std::cerr << e.what() << std::endl;
}
}
waybar::modules::sway::Window::operator Gtk::Widget &() {
return label_;
}