mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat: add custom module to allow execution of external script
This commit is contained in:
parent
e5fcbe8017
commit
e16cce646b
@ -8,6 +8,7 @@
|
||||
#include "modules/cpu.hpp"
|
||||
#include "modules/network.hpp"
|
||||
#include "modules/pulseaudio.hpp"
|
||||
#include "modules/custom.hpp"
|
||||
|
||||
namespace waybar {
|
||||
|
||||
|
24
include/modules/custom.hpp
Normal file
24
include/modules/custom.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <json/json.h>
|
||||
#include <gtkmm.h>
|
||||
#include <fmt/format.h>
|
||||
#include <thread>
|
||||
#include "util/chrono.hpp"
|
||||
#include "IModule.hpp"
|
||||
|
||||
namespace waybar::modules {
|
||||
|
||||
class Custom : public IModule {
|
||||
public:
|
||||
Custom(std::string name, Json::Value config);
|
||||
auto update() -> void;
|
||||
operator Gtk::Widget &();
|
||||
private:
|
||||
Gtk::Label _label;
|
||||
waybar::util::SleeperThread _thread;
|
||||
const std::string _name;
|
||||
Json::Value _config;
|
||||
};
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"modules-left": ["workspaces"],
|
||||
"modules-left": ["workspaces", "custom/spotify"],
|
||||
"modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock"],
|
||||
"cpu": {
|
||||
"format": "{}% "
|
||||
@ -17,5 +17,9 @@
|
||||
"pulseaudio": {
|
||||
"format": "{}% ",
|
||||
"format-muted": ""
|
||||
},
|
||||
"custom/spotify": {
|
||||
"format": " {}",
|
||||
"exec": "$HOME/.bin/mediaplayer.sh"
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ window {
|
||||
border-bottom: 3px solid white;
|
||||
}
|
||||
|
||||
.clock, .battery, .cpu, .memory, .network, .pulseaudio {
|
||||
.clock, .battery, .cpu, .memory, .network, .pulseaudio, .custom-spotify {
|
||||
padding: 0 10px;
|
||||
margin: 0 5px;
|
||||
}
|
||||
@ -64,3 +64,8 @@ window {
|
||||
background: #90b1b1;
|
||||
color: #2a5c45;
|
||||
}
|
||||
|
||||
.custom-spotify {
|
||||
background: #66cc99;
|
||||
color: #2a5c45;
|
||||
}
|
||||
|
@ -20,5 +20,7 @@ waybar::IModule &waybar::Factory::makeModule(std::string name)
|
||||
return *new waybar::modules::Network(_config[name]);
|
||||
if (name == "pulseaudio")
|
||||
return *new waybar::modules::Pulseaudio(_config[name]);
|
||||
if (!name.compare(0, 7, "custom/") && name.size() > 7)
|
||||
return *new waybar::modules::Custom(name.substr(7), _config[name]);
|
||||
throw std::runtime_error("Unknown module: " + name);
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "modules/cpu.hpp"
|
||||
#include <iostream>
|
||||
|
||||
waybar::modules::Cpu::Cpu(Json::Value config)
|
||||
: _config(config)
|
||||
|
50
src/modules/custom.cpp
Normal file
50
src/modules/custom.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "modules/custom.hpp"
|
||||
#include <iostream>
|
||||
|
||||
waybar::modules::Custom::Custom(std::string name, Json::Value config)
|
||||
: _name(name), _config(config)
|
||||
{
|
||||
_label.get_style_context()->add_class("custom-" + name);
|
||||
if (!_config["exec"]) {
|
||||
std::cerr << name + " has no exec path." << std::endl;
|
||||
return;
|
||||
}
|
||||
_thread = [this] {
|
||||
update();
|
||||
int interval = _config["interval"] ? _config["inveral"].asInt() : 30;
|
||||
_thread.sleep_for(chrono::seconds(interval));
|
||||
};
|
||||
};
|
||||
|
||||
auto waybar::modules::Custom::update() -> void
|
||||
{
|
||||
std::array<char, 128> buffer;
|
||||
std::string output;
|
||||
std::shared_ptr<FILE> fp(popen(_config["exec"].asCString(), "r"), pclose);
|
||||
if (!fp) {
|
||||
std::cerr << _name + " can't exec " + _config["exec"].asString() << std::endl;
|
||||
return;
|
||||
}
|
||||
while (!feof(fp.get())) {
|
||||
if (fgets(buffer.data(), 128, fp.get()) != nullptr)
|
||||
output += buffer.data();
|
||||
}
|
||||
|
||||
// Remove last newline
|
||||
if (!output.empty() && output[output.length()-1] == '\n') {
|
||||
output.erase(output.length()-1);
|
||||
}
|
||||
|
||||
// Hide label if output is empty
|
||||
if (output.empty())
|
||||
_label.hide();
|
||||
else {
|
||||
auto format = _config["format"] ? _config["format"].asString() : "{}";
|
||||
_label.set_text(fmt::format(format, output));
|
||||
_label.show();
|
||||
}
|
||||
}
|
||||
|
||||
waybar::modules::Custom::operator Gtk::Widget &() {
|
||||
return _label;
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
#include "modules/network.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
waybar::modules::Network::Network(Json::Value config)
|
||||
: _config(config), _ifid(if_nametoindex(config["interface"].asString().c_str()))
|
||||
{
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "modules/pulseaudio.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
waybar::modules::Pulseaudio::Pulseaudio(Json::Value config)
|
||||
: _config(config), _mainloop(nullptr), _mainloop_api(nullptr),
|
||||
_context(nullptr), _sinkIdx(0), _volume(0), _muted(false)
|
||||
|
Loading…
Reference in New Issue
Block a user