diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 3a7b22a..753cc6d 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -21,6 +21,7 @@ std::vector Dotfile::s_excludePaths; std::vector Dotfile::s_systemDirectories; std::filesystem::path Dotfile::s_workingDirectory; +size_t Dotfile::s_workingDirectorySize { 0 }; Dotfile::Dotfile() { @@ -92,9 +93,8 @@ void Dotfile::list(const std::vector& targets) return; } - size_t workingDirectory = s_workingDirectory.string().size(); - forEachDotfile(targets, [&workingDirectory](std::filesystem::directory_entry path, size_t) { - printf("%s\n", path.path().c_str() + workingDirectory + 1); + forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t) { + printf("%s\n", path.path().c_str() + s_workingDirectorySize + 1); }); } @@ -115,19 +115,19 @@ void Dotfile::pull(const std::vector& targets) } }); - size_t workingDirectory = s_workingDirectory.string().size(); sync( dotfiles, homeIndices, systemIndices, - [&workingDirectory](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home// -> /home//dotfiles/ - paths[0] = homeDirectory + homeFile.substr(workingDirectory); + paths[0] = homeDirectory + homeFile.substr(s_workingDirectorySize); paths[1] = homeFile; }, - [&workingDirectory](std::string* paths, const std::string& systemFile) { + [](std::string* paths, const std::string& systemFile) { + printf("system file: %s", systemFile.c_str()); // systemFile = /home//dotfiles/ // copy: -> /home//dotfiles/ - paths[0] = systemFile.substr(workingDirectory); + paths[0] = systemFile.substr(s_workingDirectorySize); paths[1] = systemFile; }); } @@ -149,20 +149,19 @@ void Dotfile::push(const std::vector& targets) } }); - size_t workingDirectory = s_workingDirectory.string().size(); sync( dotfiles, homeIndices, systemIndices, - [&workingDirectory](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home//dotfiles/ -> /home// paths[0] = homeFile; - paths[1] = homeDirectory + homeFile.substr(workingDirectory); + paths[1] = homeDirectory + homeFile.substr(s_workingDirectorySize); }, - [&workingDirectory](std::string* paths, const std::string& systemFile) { + [](std::string* paths, const std::string& systemFile) { // systemFile = /home//dotfiles/ // copy: /home//dotfiles/ -> paths[0] = systemFile; - paths[1] = systemFile.substr(workingDirectory); + paths[1] = systemFile.substr(s_workingDirectorySize); }); } @@ -289,7 +288,7 @@ bool Dotfile::include(const std::filesystem::path& path, const std::vector& targets = {}); void push(const std::vector& targets = {}); - static void setWorkingDirectory(std::filesystem::path directory) { s_workingDirectory = directory; } + static void setWorkingDirectory(std::filesystem::path directory) + { + s_workingDirectory = directory; + s_workingDirectorySize = directory.string().size(); + } static void setSystemDirectories(const std::vector& systemDirectories) { s_systemDirectories = systemDirectories; } static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } @@ -50,6 +54,7 @@ private: static std::vector s_excludePaths; static std::vector s_systemDirectories; static std::filesystem::path s_workingDirectory; + static size_t s_workingDirectorySize; }; #endif // DOTFILE_H