refactor: move command execution into their own file

This commit is contained in:
Alexis 2018-08-18 17:54:20 +02:00
parent b794ca63d1
commit ce50a627be
5 changed files with 124 additions and 120 deletions

View File

@ -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 {

View File

@ -8,29 +8,29 @@
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
{
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
{
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 {
struct SleeperThread {
SleeperThread() = default;
SleeperThread(std::function<void()> func)
@ -81,12 +81,12 @@ namespace waybar::util {
}
}
private:
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
View 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 };
}
}

View File

@ -4,7 +4,7 @@
namespace waybar::util {
struct JsonParser {
struct JsonParser {
JsonParser()
: _reader(_builder.newCharReader())
@ -26,9 +26,9 @@ namespace waybar::util {
delete _reader;
}
private:
private:
Json::CharReaderBuilder _builder;
Json::CharReader *_reader;
};
};
}

View File

@ -10,22 +10,8 @@ waybar::modules::Custom::Custom(std::string name, Json::Value config)
thread_ = [this, interval] {
bool can_update = true;
if (config_["exec-if"]) {
auto pid = fork();
int res = 0;
if (pid == 0) {
std::istringstream iss(config_["exec-if"].asString());
std::vector<char*> av;
for (std::string s; iss >> s;) {
// Need to copy otherwise values are the same
char *str = new char[s.size() + 1];
memcpy(str, s.c_str(), s.size() + 1);
av.push_back(str);
}
av.push_back(0);
execvp(av.front(), av.data());
_exit(127);
} else if (pid > 0 && waitpid(pid, &res, 0) != -1
&& WEXITSTATUS(res) != 0) {
auto res = waybar::util::command::exec(config_["exec-if"].asString());
if (res.exit_code != 0) {
can_update = false;
}
}
@ -38,33 +24,16 @@ waybar::modules::Custom::Custom(std::string name, Json::Value config)
auto waybar::modules::Custom::update() -> void
{
std::array<char, 128> buffer = {0};
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()) == 0) {
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);
}
auto res = waybar::util::command::exec(config_["exec"].asString());
// Hide label if output is empty
if (output.empty()) {
if (res.out.empty() || res.exit_code != 0) {
label_.hide();
label_.set_name("");
} else {
label_.set_name("custom-" + name_);
auto format = config_["format"] ? config_["format"].asString() : "{}";
auto str = fmt::format(format, output);
auto str = fmt::format(format, res.out);
label_.set_text(str);
label_.set_tooltip_text(str);
label_.show();