Browse Source

Manager: Add index to dotfile iteration loop

master
Riyyi 3 years ago
parent
commit
e7d1515fca
  1. 8
      src/dotfile.cpp
  2. 2
      src/dotfile.h

8
src/dotfile.cpp

@ -161,16 +161,18 @@ void Dotfile::list(const std::vector<std::string>& targets)
return; return;
} }
forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t workingDirectory) { forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t, size_t workingDirectory) {
printf("%s\n", path.path().c_str() + workingDirectory + 1); printf("%s\n", path.path().c_str() + workingDirectory + 1);
}); });
} }
// ----------------------------------------- // -----------------------------------------
void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t)>& callback) void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t, size_t)>& callback)
{ {
size_t workingDirectory = s_workingDirectory.string().size(); size_t workingDirectory = s_workingDirectory.string().size();
size_t index = 0;
for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) { for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) {
if (path.is_directory() || filter(path)) { if (path.is_directory() || filter(path)) {
continue; continue;
@ -178,7 +180,7 @@ void Dotfile::forEachDotfile(const std::vector<std::string>& targets, const std:
if (!targets.empty() && !include(path.path().string(), targets)) { if (!targets.empty() && !include(path.path().string(), targets)) {
continue; continue;
} }
callback(path, workingDirectory); callback(path, index++, workingDirectory);
} }
} }

2
src/dotfile.h

@ -39,7 +39,7 @@ public:
static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; } static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; }
private: private:
void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t)>& callback); void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t, size_t)>& callback);
bool filter(const std::filesystem::path& path); bool filter(const std::filesystem::path& path);
bool include(const std::filesystem::path& path, const std::vector<std::string>& targets); bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);

Loading…
Cancel
Save