mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor: move command execution into their own file
This commit is contained in:
@ -2,8 +2,8 @@
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <sys/wait.h>
|
||||
#include "util/chrono.hpp"
|
||||
#include "util/command.hpp"
|
||||
#include "ALabel.hpp"
|
||||
|
||||
namespace waybar::modules {
|
||||
|
@ -8,85 +8,85 @@
|
||||
|
||||
namespace waybar::chrono {
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace std::chrono;
|
||||
|
||||
using clock = std::chrono::system_clock;
|
||||
using duration = clock::duration;
|
||||
using time_point = std::chrono::time_point<clock, duration>;
|
||||
using clock = std::chrono::system_clock;
|
||||
using duration = clock::duration;
|
||||
using time_point = std::chrono::time_point<clock, duration>;
|
||||
|
||||
inline struct timespec to_timespec(time_point t) noexcept
|
||||
{
|
||||
long secs = duration_cast<seconds>(t.time_since_epoch()).count();
|
||||
long nsc = duration_cast<nanoseconds>(t.time_since_epoch() % seconds(1)).count();
|
||||
return {secs, nsc};
|
||||
}
|
||||
inline struct timespec to_timespec(time_point t) noexcept
|
||||
{
|
||||
long secs = duration_cast<seconds>(t.time_since_epoch()).count();
|
||||
long nsc = duration_cast<nanoseconds>(t.time_since_epoch() % seconds(1)).count();
|
||||
return {secs, nsc};
|
||||
}
|
||||
|
||||
inline time_point to_time_point(struct timespec t) noexcept
|
||||
{
|
||||
return time_point(duration_cast<duration>(seconds(t.tv_sec) + nanoseconds(t.tv_nsec)));
|
||||
}
|
||||
inline time_point to_time_point(struct timespec t) noexcept
|
||||
{
|
||||
return time_point(duration_cast<duration>(seconds(t.tv_sec) + nanoseconds(t.tv_nsec)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
struct SleeperThread {
|
||||
SleeperThread() = default;
|
||||
struct SleeperThread {
|
||||
SleeperThread() = default;
|
||||
|
||||
SleeperThread(std::function<void()> func)
|
||||
: thread{[this, func] {
|
||||
do {
|
||||
func();
|
||||
} while (do_run);
|
||||
}}
|
||||
{
|
||||
defined = true;
|
||||
}
|
||||
|
||||
SleeperThread& operator=(std::function<void()> func)
|
||||
{
|
||||
thread = std::thread([this, func] {
|
||||
SleeperThread(std::function<void()> func)
|
||||
: thread{[this, func] {
|
||||
do {
|
||||
func();
|
||||
} while (do_run);
|
||||
});
|
||||
defined = true;
|
||||
return *this;
|
||||
}
|
||||
}}
|
||||
{
|
||||
defined = true;
|
||||
}
|
||||
|
||||
SleeperThread& operator=(std::function<void()> func)
|
||||
{
|
||||
thread = std::thread([this, func] {
|
||||
do {
|
||||
func();
|
||||
} while (do_run);
|
||||
});
|
||||
defined = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
auto sleep_for(chrono::duration dur)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_for(lock, dur);
|
||||
}
|
||||
auto sleep_for(chrono::duration dur)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_for(lock, dur);
|
||||
}
|
||||
|
||||
auto sleep_until(chrono::time_point time)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_until(lock, time);
|
||||
}
|
||||
auto sleep_until(chrono::time_point time)
|
||||
{
|
||||
auto lock = std::unique_lock(mutex);
|
||||
return condvar.wait_until(lock, time);
|
||||
}
|
||||
|
||||
auto wake_up()
|
||||
{
|
||||
auto wake_up()
|
||||
{
|
||||
condvar.notify_all();
|
||||
}
|
||||
|
||||
~SleeperThread()
|
||||
{
|
||||
do_run = false;
|
||||
if (defined) {
|
||||
condvar.notify_all();
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
~SleeperThread()
|
||||
{
|
||||
do_run = false;
|
||||
if (defined) {
|
||||
condvar.notify_all();
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::thread thread;
|
||||
std::condition_variable condvar;
|
||||
std::mutex mutex;
|
||||
bool defined = false;
|
||||
bool do_run = true;
|
||||
};
|
||||
private:
|
||||
std::thread thread;
|
||||
std::condition_variable condvar;
|
||||
std::mutex mutex;
|
||||
bool defined = false;
|
||||
bool do_run = true;
|
||||
};
|
||||
|
||||
}
|
||||
|
35
include/util/command.hpp
Normal file
35
include/util/command.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
namespace waybar::util::command {
|
||||
|
||||
struct cmd_res {
|
||||
int exit_code;
|
||||
std::string out;
|
||||
};
|
||||
|
||||
inline struct cmd_res exec(const std::string cmd)
|
||||
{
|
||||
FILE* fp(popen(cmd.c_str(), "r"));
|
||||
if (!fp) {
|
||||
return { -1, "" };
|
||||
}
|
||||
|
||||
std::array<char, 128> buffer = {0};
|
||||
std::string output;
|
||||
while (feof(fp) == 0) {
|
||||
if (fgets(buffer.data(), 128, fp) != nullptr) {
|
||||
output += buffer.data();
|
||||
}
|
||||
}
|
||||
|
||||
// Remove last newline
|
||||
if (!output.empty() && output[output.length()-1] == '\n') {
|
||||
output.erase(output.length()-1);
|
||||
}
|
||||
int exit_code = WEXITSTATUS(pclose(fp));
|
||||
return { exit_code, output };
|
||||
}
|
||||
|
||||
}
|
@ -4,31 +4,31 @@
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
struct JsonParser {
|
||||
struct JsonParser {
|
||||
|
||||
JsonParser()
|
||||
: _reader(_builder.newCharReader())
|
||||
{}
|
||||
JsonParser()
|
||||
: _reader(_builder.newCharReader())
|
||||
{}
|
||||
|
||||
Json::Value parse(const std::string data)
|
||||
{
|
||||
Json::Value root;
|
||||
std::string err;
|
||||
bool res =
|
||||
_reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
if (!res)
|
||||
throw std::runtime_error(err);
|
||||
return root;
|
||||
}
|
||||
Json::Value parse(const std::string data)
|
||||
{
|
||||
Json::Value root;
|
||||
std::string err;
|
||||
bool res =
|
||||
_reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
if (!res)
|
||||
throw std::runtime_error(err);
|
||||
return root;
|
||||
}
|
||||
|
||||
~JsonParser()
|
||||
{
|
||||
delete _reader;
|
||||
}
|
||||
~JsonParser()
|
||||
{
|
||||
delete _reader;
|
||||
}
|
||||
|
||||
private:
|
||||
Json::CharReaderBuilder _builder;
|
||||
Json::CharReader *_reader;
|
||||
};
|
||||
private:
|
||||
Json::CharReaderBuilder _builder;
|
||||
Json::CharReader *_reader;
|
||||
};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user