refactor: format code

This commit is contained in:
Alex
2019-04-18 17:52:00 +02:00
parent 807ef32357
commit 6ed8f94dab
47 changed files with 871 additions and 960 deletions

View File

@ -1,25 +1,24 @@
#pragma once
#include <sys/wait.h>
#include <giomm.h>
#include <sys/wait.h>
#include <unistd.h>
namespace waybar::util::command {
struct res {
int exit_code;
int exit_code;
std::string out;
};
inline struct res exec(const std::string cmd)
{
inline struct res exec(const std::string cmd) {
FILE* fp(popen(cmd.c_str(), "r"));
if (!fp) {
return { -1, "" };
return {-1, ""};
}
std::array<char, 128> buffer = {0};
std::string output;
std::string output;
while (feof(fp) == 0) {
if (fgets(buffer.data(), 128, fp) != nullptr) {
output += buffer.data();
@ -27,8 +26,8 @@ inline struct res exec(const std::string cmd)
}
// Remove last newline
if (!output.empty() && output[output.length()-1] == '\n') {
output.erase(output.length()-1);
if (!output.empty() && output[output.length() - 1] == '\n') {
output.erase(output.length() - 1);
}
int exit_code = WEXITSTATUS(pclose(fp));
return {exit_code, output};

View File

@ -5,30 +5,24 @@
namespace waybar::util {
struct JsonParser {
JsonParser() : reader_(builder_.newCharReader()) {}
JsonParser()
: reader_(builder_.newCharReader())
{}
const Json::Value parse(const std::string& data) const
{
const Json::Value parse(const std::string& data) const {
Json::Value root;
std::string err;
if (data.empty()) {
return root;
}
bool res =
reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
if (!res)
throw std::runtime_error(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() = default;
private:
Json::CharReaderBuilder builder_;
private:
Json::CharReaderBuilder builder_;
std::unique_ptr<Json::CharReader> const reader_;
};
}
} // namespace waybar::util

View File

@ -1,25 +1,23 @@
#pragma once
#include <chrono>
#include <condition_variable>
#include <ctime>
#include <functional>
#include <condition_variable>
#include <thread>
namespace waybar::util {
class SleeperThread {
public:
public:
SleeperThread() = default;
SleeperThread(std::function<void()> func)
: thread_{[this, func] {
while (do_run_) func();
}}
{}
: thread_{[this, func] {
while (do_run_) func();
}} {}
SleeperThread& operator=(std::function<void()> func)
{
SleeperThread& operator=(std::function<void()> func) {
thread_ = std::thread([this, func] {
while (do_run_) {
signal_ = false;
@ -29,32 +27,26 @@ public:
return *this;
}
bool isRunning() const
{
return do_run_;
}
bool isRunning() const { return do_run_; }
auto sleep_for(std::chrono::system_clock::duration dur)
{
auto sleep_for(std::chrono::system_clock::duration dur) {
std::unique_lock lk(mutex_);
return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
}
auto sleep_until(std::chrono::time_point<std::chrono::system_clock,
std::chrono::system_clock::duration> time_point)
{
auto sleep_until(
std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::duration>
time_point) {
std::unique_lock lk(mutex_);
return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; });
}
auto wake_up()
{
auto wake_up() {
signal_ = true;
condvar_.notify_all();
}
auto stop()
{
auto stop() {
{
std::lock_guard<std::mutex> lck(mutex_);
signal_ = true;
@ -63,20 +55,19 @@ public:
condvar_.notify_all();
}
~SleeperThread()
{
~SleeperThread() {
stop();
if (thread_.joinable()) {
thread_.join();
}
}
private:
std::thread thread_;
private:
std::thread thread_;
std::condition_variable condvar_;
std::mutex mutex_;
bool do_run_ = true;
bool signal_ = false;
std::mutex mutex_;
bool do_run_ = true;
bool signal_ = false;
};
}
} // namespace waybar::util