diff --git a/include/utils/file.hpp b/include/utils/file.hpp index 2fc37355..3949e2b8 100644 --- a/include/utils/file.hpp +++ b/include/utils/file.hpp @@ -109,6 +109,7 @@ namespace file_util { vector glob(string pattern); const string expand(const string& path); string get_config_path(); + vector list_files(const string& dirname); template decltype(auto) make_file_descriptor(Args&&... args) { diff --git a/src/components/config_parser.cpp b/src/components/config_parser.cpp index eb3e0676..b422584e 100644 --- a/src/components/config_parser.cpp +++ b/src/components/config_parser.cpp @@ -144,6 +144,13 @@ void config_parser::parse_file(const string& file, file_list path) { if (!line.is_header && line.key == "include-file") { parse_file(file_util::expand(line.value), path); + } else if (!line.is_header && line.key == "include-directory") { + const string expanded_path = file_util::expand(line.value); + vector file_list = file_util::list_files(expanded_path); + sort(file_list.begin(), file_list.end()); + for (const auto& filename : file_list) { + parse_file(expanded_path + "/" + filename, path); + } } else { m_lines.push_back(line); } diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 97fc27d5..0a4c6955 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -1,5 +1,6 @@ #include "utils/file.hpp" +#include #include #include #include @@ -294,6 +295,27 @@ namespace file_util { } return ""; } + + /** + * Return a list of file names in a directory. + */ + vector list_files(const string& dirname) { + vector files; + DIR* dir; + if ((dir = opendir(dirname.c_str())) != NULL) { + struct dirent* ent; + while ((ent = readdir(dir)) != NULL) { + // Type can be unknown for filesystems that do not support d_type + if ((ent->d_type & DT_REG) || + ((ent->d_type & DT_UNKNOWN) && strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0)) { + files.push_back(ent->d_name); + } + } + closedir(dir); + return files; + } + throw system_error("Failed to open directory stream for " + dirname); + } } // namespace file_util POLYBAR_NS_END