diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 56077e2..67eed1c 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -17,6 +17,7 @@ #include "dotfile.h" #include "machine.h" +#include "util/file.h" std::vector Dotfile::s_excludePaths; std::vector Dotfile::s_systemDirectories; @@ -70,7 +71,7 @@ void Dotfile::add(const std::vector& targets) } sync( - targets, homeIndices, systemIndices, + SyncType::Pull, targets, homeIndices, systemIndices, [](std::string* paths, const std::string& homePath, const std::string& homeDirectory) { paths[0] = homePath; paths[1] = homePath.substr(homeDirectory.size() + 1); @@ -129,7 +130,7 @@ void Dotfile::pullOrPush(SyncType type, const std::vector& targets) if (type == SyncType::Pull) { sync( - dotfiles, homeIndices, systemIndices, + type, dotfiles, homeIndices, systemIndices, [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home// -> /home//dotfiles/ @@ -145,7 +146,7 @@ void Dotfile::pullOrPush(SyncType type, const std::vector& targets) } else { sync( - dotfiles, homeIndices, systemIndices, + type, dotfiles, homeIndices, systemIndices, [](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { // homeFile = /home//dotfiles/ // copy: /home//dotfiles/ -> /home// @@ -161,7 +162,8 @@ void Dotfile::pullOrPush(SyncType type, const std::vector& targets) } } -void Dotfile::sync(const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, +void Dotfile::sync(SyncType type, + const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, const std::function& generateHomePaths, const std::function& generateSystemPaths) { @@ -223,13 +225,116 @@ void Dotfile::sync(const std::vector& paths, const std::vector>>") != std::string::npos) { + // Find machine info + size_t find = 0; + for (size_t i = 0; i < 3; ++i) { + find = line.find(search[i]) + search[i].size(); + if (find < search[i].size()) { + continue; + } + filter[i] = line.substr(find, line.find_first_of(' ', find) - find); + } + + // Get the characters used for commenting in this file-type + commentCharacter = line.substr(0, line.find_first_of(">>>")); + for (size_t i = commentCharacter.size() - 1; i != std::string::npos; --i) { + if (commentCharacter.at(i) == ' ' || commentCharacter.at(i) == '\t') { + commentCharacter.erase(i, 1); + } + } + + isFiltering = true; + continue; + } + + if (line.find("<<<") != std::string::npos) { + isFiltering = false; + filter[0] = ""; + filter[1] = ""; + filter[2] = ""; + commentCharacter.clear(); + continue; + } + + if (!isFiltering) { + continue; + } + + if (filter[0] != Machine::the().distroId() && !filter[0].empty()) { + commentOrUncommentLine(line, true); + continue; + } + else if (filter[1] != Machine::the().hostname() && !filter[1].empty()) { + commentOrUncommentLine(line, true); + continue; + } + else if (filter[2] != Machine::the().username() && !filter[2].empty()) { + commentOrUncommentLine(line, true); + continue; + } + + commentOrUncommentLine(line, false); } + + dotfile.flush(); } void Dotfile::forEachDotfile(const std::vector& targets, const std::function& callback) diff --git a/src/dotfile.h b/src/dotfile.h index 042c696..82f09f5 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -49,9 +49,11 @@ public: private: void pullOrPush(SyncType type, const std::vector& targets = {}); - void sync(const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, + void sync(SyncType type, + const std::vector& paths, const std::vector& homeIndices, const std::vector& systemIndices, const std::function& generateHomePaths, const std::function& generateSystemPaths); + void selectivelyCommentOrUncomment(const std::string& path); void forEachDotfile(const std::vector& targets, const std::function& callback); bool filter(const std::filesystem::path& path); diff --git a/src/util/file.cpp b/src/util/file.cpp index eca16b5..583f643 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Riyyi + * Copyright (C) 2021-2022 Riyyi * * SPDX-License-Identifier: MIT */ @@ -55,6 +55,13 @@ File& File::append(std::string data) return *this; } +File& File::replace(size_t index, size_t length, const std::string& data) +{ + m_data.replace(index, length, data); + + return *this; +} + File& File::flush() { // Create output stream object and open file diff --git a/src/util/file.h b/src/util/file.h index 2672294..62599c2 100644 --- a/src/util/file.h +++ b/src/util/file.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Riyyi + * Copyright (C) 2021-2022 Riyyi * * SPDX-License-Identifier: MIT */ @@ -18,6 +18,7 @@ public: void clear(); File& append(std::string data); + File& replace(size_t index, size_t length, const std::string& data); File& flush(); const char* c_str() const { return m_data.c_str(); }