From 6decd7bc4b643065b038bd32bab5169861b9b042 Mon Sep 17 00:00:00 2001 From: Timofey Khoruzhii Date: Thu, 11 Aug 2022 21:42:53 +0300 Subject: [PATCH] Add colored, run scripts --- CMakeLists.txt | 9 ++++++++- src/clippy/config.cpp | 1 + src/clippy/config.hpp | 4 +++- src/clippy/project_list.cpp | 2 +- src/clippy/project_list.hpp | 2 +- src/clippy/target.cpp | 37 ------------------------------------- src/clippy/target.hpp | 26 ++++++++++++++++++++++---- 7 files changed, 36 insertions(+), 45 deletions(-) delete mode 100644 src/clippy/target.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f095a1..ae92603 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ file(GLOB_RECURSE SOURCES_FILES src/*) add_executable(clippy_terminal ${SOURCES_FILES}) target_include_directories(clippy_terminal PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) -target_link_libraries(clippy_terminal cppshell) +target_link_libraries(clippy_terminal cppshell rang) install(TARGETS clippy_terminal DESTINATION bin) @@ -19,3 +19,10 @@ FetchContent_Declare( GIT_TAG origin/main ) FetchContent_MakeAvailable(cppshell) + +FetchContent_Declare( + rang + GIT_REPOSITORY https://github.com/agauniyal/rang.git + GIT_TAG origin/master +) +FetchContent_MakeAvailable(rang) diff --git a/src/clippy/config.cpp b/src/clippy/config.cpp index a966c97..ce58288 100644 --- a/src/clippy/config.cpp +++ b/src/clippy/config.cpp @@ -23,6 +23,7 @@ std::unique_ptr Config::GetTarget( std::string current; std::vector target_commands; + target_commands.emplace_back("cd " + initial_directory_); bool target_begin = false; diff --git a/src/clippy/config.hpp b/src/clippy/config.hpp index e9927a5..e522bee 100644 --- a/src/clippy/config.hpp +++ b/src/clippy/config.hpp @@ -11,7 +11,8 @@ class Target; class Config { public: - Config(std::string path) : path_(std::move(path)) {} + Config(std::string path, std::string initial_directory) + : path_(std::move(path)), initial_directory_(initial_directory) {} void Edit() { utils::OpenEditor(path_); } @@ -19,4 +20,5 @@ class Config { private: std::string path_; + std::string initial_directory_; }; diff --git a/src/clippy/project_list.cpp b/src/clippy/project_list.cpp index b4ed2a9..2e20ec7 100644 --- a/src/clippy/project_list.cpp +++ b/src/clippy/project_list.cpp @@ -59,7 +59,7 @@ Config ProjectList::GetNewConfig( projects_.emplace_back(std::filesystem::current_path(), path_to_config); - return {path_to_config}; + return {path_to_config, std::filesystem::current_path()}; } void ProjectList::Load() { diff --git a/src/clippy/project_list.hpp b/src/clippy/project_list.hpp index 2a49462..203cbfc 100644 --- a/src/clippy/project_list.hpp +++ b/src/clippy/project_list.hpp @@ -10,7 +10,7 @@ struct Project { std::strong_ordering operator<=>(const Project&) const = default; - Config GetConfig() { return Config{configuration_file}; } + Config GetConfig() { return Config{configuration_file, root_project}; } std::filesystem::path root_project; std::filesystem::path configuration_file; diff --git a/src/clippy/target.cpp b/src/clippy/target.cpp deleted file mode 100644 index 8fe34e3..0000000 --- a/src/clippy/target.cpp +++ /dev/null @@ -1,37 +0,0 @@ -namespace clippy::targets { -class Target { - public: - virtual void Execute() = 0; -}; - -class EmptyTarget : public Target { - public: - void Execute() override {} -}; - -class OpenProjectConfig : public Target { - public: - OpenProjectConfig(Config config) : config_(config) {} - - void Execute() override { config_.Edit(); } - - private: - Config config_; -}; - -class CreateProjectConfig : public Target { - public: - CreateProjectConfig(ProjectList& projects) : projects_(projects) {} - - void Execute() override { - auto scripts_path = utils::GetProjectDirectory() / "scripts"; - std::filesystem::create_directories(scripts_path); - - auto config = projects_.GetNewConfig(scripts_path); - config.Edit(); - } - - private: - ProjectList& projects_; -}; -} // namespace clippy::targets diff --git a/src/clippy/target.hpp b/src/clippy/target.hpp index 8ac2308..3aa9933 100644 --- a/src/clippy/target.hpp +++ b/src/clippy/target.hpp @@ -10,7 +10,10 @@ #include #undef SIGPIPE_ALWAYS_IGNORE +#include + #include +#include #include namespace clippy::targets { @@ -58,11 +61,26 @@ class RunShellScript : public Target { void Execute() override { cppshell::Shell s; - for (auto& i : commands_) { - std::cout << "-> " << i << std::endl; - s.Execute(i); + for (auto& command : commands_) { + std::cout << rang::fg::green << rang::style::bold << "-> " + << rang::fg::reset << command << rang::style::reset + << std::endl; + s.Execute(command); if (int err = s.GetExitCodeLastCommand(); err != 0) { - std::cout << "ERROR" << std::endl; + std::cout << rang::fg::red << rang::style::bold + << "Command exit with code " << err << rang::fg::reset + << rang::style::reset << std::endl; + std::cout << rang::fg::blue << rang::style::bold + << "Continue execution? [Y/n] " << rang::fg::reset + << rang::style::reset; + + std::string result; + std::getline(std::cin, result); + if (result == "" || result == "y") { + continue; + } else { + return; + } } } }