From 67f0ab5d19f875f04cd849a8b8385a035452040c Mon Sep 17 00:00:00 2001 From: Timofey Khoruzhii Date: Sun, 11 Jun 2023 09:39:34 +0300 Subject: [PATCH] add remove --- include/projects/project.hpp | 4 ++++ include/projects/project_list.hpp | 1 + src/main.cpp | 3 +++ src/projects/project_list.cpp | 12 ++++++++++++ 4 files changed, 20 insertions(+) diff --git a/include/projects/project.hpp b/include/projects/project.hpp index 6c1c763..c44b0a3 100644 --- a/include/projects/project.hpp +++ b/include/projects/project.hpp @@ -8,9 +8,13 @@ class Project { public: Project(const std::string& path, const std::string& path_to_scripts, const std::string& name); + Project(const Project&) = delete; Project(Project&&) = default; + Project& operator=(const Project&) = delete; + Project& operator=(Project&&) = default; + const std::filesystem::path& GetPath() const; const std::filesystem::path& GetPathToScripts() const; const std::string& GetName() const; diff --git a/include/projects/project_list.hpp b/include/projects/project_list.hpp index dac6f4e..6dae7bf 100644 --- a/include/projects/project_list.hpp +++ b/include/projects/project_list.hpp @@ -13,6 +13,7 @@ class ProjectList { const std::deque& GetProjects(); void AddProject(Project&&); + void RemoveProjectByName(const std::string& name); const Project& GetProject( const std::filesystem::path& path = std::filesystem::current_path()) const; diff --git a/src/main.cpp b/src/main.cpp index 1a830d4..0ec11b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,6 +92,9 @@ int main(int argc, char* argv[]) { std::cout << "try open: " << project.GetName() << std::endl; }); + + parser.AddRule(MakePattern({".", "remove", project.GetName().data()}), + [&projects](auto& args) { projects.RemoveProjectByName(args[3]); }); } parser.AddRule(MakePattern({"help"}), [](auto& args) { std::cout << "It's help" << std::endl; }); diff --git a/src/projects/project_list.cpp b/src/projects/project_list.cpp index d13e5b5..1a05454 100644 --- a/src/projects/project_list.cpp +++ b/src/projects/project_list.cpp @@ -21,6 +21,18 @@ void ProjectList::AddProject(Project&& project) { projects_.push_back(std::move(project)); } +void ProjectList::RemoveProjectByName(const std::string& name) { + for (auto it = projects_.begin(); it < projects_.end(); ++it) { + auto& project = *it; + if (project.GetName() == name) { + projects_.erase(it); + return; + } + } + + throw std::logic_error("Project not found"); +} + const Project& ProjectList::GetProject(const std::filesystem::path& path) const { size_t index = 0, prefix = 0; for (size_t i = 0; i < projects_.size(); ++i) {