Add colored, run scripts

This commit is contained in:
Timofey Khoruzhii 2022-08-11 21:42:53 +03:00
parent 51188e9f8e
commit 6decd7bc4b
7 changed files with 36 additions and 45 deletions

View file

@ -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)

View file

@ -23,6 +23,7 @@ std::unique_ptr<clippy::targets::Target> Config::GetTarget(
std::string current;
std::vector<std::string> target_commands;
target_commands.emplace_back("cd " + initial_directory_);
bool target_begin = false;

View file

@ -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_;
};

View file

@ -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() {

View file

@ -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;

View file

@ -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

View file

@ -10,7 +10,10 @@
#include <cppshell/shell.hpp>
#undef SIGPIPE_ALWAYS_IGNORE
#include <rang.hpp>
#include <iostream>
#include <ranges>
#include <functional>
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;
}
}
}
}