diff --git a/include/utils/file.hpp b/include/utils/file.hpp index 439c41ee..2fc37355 100644 --- a/include/utils/file.hpp +++ b/include/utils/file.hpp @@ -108,6 +108,7 @@ namespace file_util { bool is_fifo(const string& filename); vector glob(string pattern); const string expand(const string& path); + string get_config_path(); template decltype(auto) make_file_descriptor(Args&&... args) { diff --git a/src/main.cpp b/src/main.cpp index f0048310..c3f1d441 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,11 +106,10 @@ int main(int argc, char** argv) { if (cli->has("config")) { confpath = cli->get("config"); - } else if (env_util::has("XDG_CONFIG_HOME")) { - confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config"; - } else if (env_util::has("HOME")) { - confpath = env_util::get("HOME") + "/.config/polybar/config"; } else { + confpath = file_util::get_config_path(); + } + if (confpath.empty()) { throw application_error("Define configuration using --config=PATH"); } diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 56f6b552..97fc27d5 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -274,6 +274,26 @@ namespace file_util { } return ret; } + + /* + * Search for polybar config and returns the path if found + */ + string get_config_path() { + string confpath; + if (env_util::has("XDG_CONFIG_HOME")) { + confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config"; + if (exists(confpath)) { + return confpath; + } + } + if (env_util::has("HOME")) { + confpath = env_util::get("HOME") + "/.config/polybar/config"; + if (exists(confpath)) { + return confpath; + } + } + return ""; + } } // namespace file_util POLYBAR_NS_END