refactor(client): lambda to method

This commit is contained in:
Alexis 2018-11-24 11:04:56 +01:00
parent 2c2a0473f4
commit 087de4e956
2 changed files with 21 additions and 18 deletions

View File

@ -29,6 +29,7 @@ class Client {
private: private:
void bindInterfaces(); void bindInterfaces();
auto setupCss(); auto setupCss();
const std::string getValidPath(std::vector<std::string> paths);
static void handleGlobal(void *data, struct wl_registry *registry, static void handleGlobal(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version); uint32_t name, const char *interface, uint32_t version);

View File

@ -12,30 +12,14 @@ waybar::Client::Client(int argc, char* argv[])
throw std::runtime_error("Bar need to run under Wayland"); throw std::runtime_error("Bar need to run under Wayland");
} }
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj()); wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
auto getFirstValidPath = [] (std::vector<std::string> possiblePaths) {
wordexp_t p;
for (const std::string &path: possiblePaths) { config_file = getValidPath({
if (wordexp(path.c_str(), &p, 0) == 0) {
if (access(*p.we_wordv, F_OK) == 0) {
std::string result = *p.we_wordv;
wordfree(&p);
return result;
}
wordfree(&p);
}
}
return std::string();
};
config_file = getFirstValidPath({
"$XDG_CONFIG_HOME/waybar/config", "$XDG_CONFIG_HOME/waybar/config",
"$HOME/waybar/config", "$HOME/waybar/config",
"/etc/xdg/waybar/config", "/etc/xdg/waybar/config",
"./resources/config", "./resources/config",
}); });
css_file = getFirstValidPath({ css_file = getValidPath({
"$XDG_CONFIG_HOME/waybar/style.css", "$XDG_CONFIG_HOME/waybar/style.css",
"$HOME/waybar/style.css", "$HOME/waybar/style.css",
"/etc/xdg/waybar/style.css", "/etc/xdg/waybar/style.css",
@ -47,6 +31,24 @@ waybar::Client::Client(int argc, char* argv[])
std::cout << "Resources files: " + config_file + ", " + css_file << std::endl; std::cout << "Resources files: " + config_file + ", " + css_file << std::endl;
} }
const std::string waybar::Client::getValidPath(std::vector<std::string> paths)
{
wordexp_t p;
for (const std::string &path: paths) {
if (wordexp(path.c_str(), &p, 0) == 0) {
if (access(*p.we_wordv, F_OK) == 0) {
std::string result = *p.we_wordv;
wordfree(&p);
return result;
}
wordfree(&p);
}
}
return std::string();
}
void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, void waybar::Client::handleGlobal(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) uint32_t name, const char *interface, uint32_t version)
{ {