From f8ccfe79b846f1f1b8960911d78565a0d01d8168 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 20 Sep 2021 18:18:31 +0200 Subject: [PATCH] Manager: Add target filtering to Dotfile::list() --- src/dotfile.cpp | 16 +++++++++++++++- src/dotfile.h | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/dotfile.cpp b/src/dotfile.cpp index cd048cb..ca8a8ff 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -26,7 +26,7 @@ Dotfile::~Dotfile() // ----------------------------------------- -void Dotfile::list() +void Dotfile::list(const std::vector& targets) { if (s_workingDirectory.empty()) { fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); @@ -43,6 +43,9 @@ void Dotfile::list() if (path.is_directory() || filter(path)) { continue; } + if (!targets.empty() && !include(path.path().string(), targets)) { + continue; + } printf("%s\n", path.path().c_str() + workingDirectory); } } @@ -72,4 +75,15 @@ bool Dotfile::filter(const std::filesystem::path& path) return false; } +bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets) +{ + for (const auto& target : targets) { + if (path.string().find(s_workingDirectory / target) == 0) { + return true; + } + } + + return false; +} + } // namespace Util diff --git a/src/dotfile.h b/src/dotfile.h index ae12d37..17dd240 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -29,13 +29,14 @@ public: std::string path; }; - static void list(); + static void list(const std::vector& targets = {}); static void setWorkingDirectory(std::filesystem::path directory) { s_workingDirectory = directory; } static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } private: static bool filter(const std::filesystem::path& path); + static bool include(const std::filesystem::path& path, const std::vector& targets); static std::vector s_excludePaths; static std::filesystem::path s_workingDirectory;