From 55a482219d81c1180abf9de217e262aa8e3874e4 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Tue, 18 Jul 2023 15:28:32 +0200 Subject: [PATCH 1/2] add command alias function --- src/command.cpp | 11 +++++++ src/command.h | 3 +- src/main.cpp | 87 +++++++++++++++++++++++++++++++++++-------------- src/main.h | 2 +- 4 files changed, 77 insertions(+), 26 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 3e8be7f..82f3004 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -4,6 +4,17 @@ Command::Command() {} Command::~Command() {} + +// add a command alias to the command map +void Command::addCommandAlias(std::string name, std::string alias) +{ + CommandInfo mycommand{ + alias, + commands[name].description, + commands[name].func}; + commands[alias] = mycommand; +} + // add a command to the command map void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) { diff --git a/src/command.h b/src/command.h index a6bff41..ed97960 100644 --- a/src/command.h +++ b/src/command.h @@ -5,7 +5,6 @@ #include #include - struct CommandInfo { std::string name; @@ -19,6 +18,8 @@ public: Command(void); ~Command(void); + // add a command alias to the command map + void addCommandAlias(std::string name, std::string alias); // add a command to the command map void addCommand(std::string name, void (*func)(int argc, char *argv[])); void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); diff --git a/src/main.cpp b/src/main.cpp index 277490d..ecb9404 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) Settings &settings = Settings::Instance(); command = Command(); - dir = settings.getSetting("autom_home_dir"); + home_dir = settings.getSetting("autom_home_dir"); input(argc, argv); return 0; @@ -20,13 +20,17 @@ void input(int argc, char *argv[]) // std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; command.addCommand("run", "[script] - Runs a script", runScript); command.addCommand("help", "- Shows this help message", help); + command.addCommandAlias("help", "h"); command.addCommand("ls", "- Lists all scripts ", listScripts); - // command.addCommand("config", "open configure dialog", removeScript); + command.addCommandAlias("ls", "l"); command.addCommand("add", "[script] - Adds a script", addScript); - command.addCommand("new", "[script] - Adds a script", addScript); + command.addCommandAlias("add", "a"); command.addCommand("edit", "[script] - Edits a script", editScript); + command.addCommandAlias("edit", "e"); command.addCommand("remove", "[script] - Remove a script", removeScript); + command.addCommandAlias("remove", "r"); command.addCommand("show", "[script] - Shows a script", showScript); + command.addCommandAlias("show", "s"); command.addDefaultCommand(runScript); command.runCommand(argv[1], argc, argv); } @@ -35,32 +39,66 @@ void input(int argc, char *argv[]) void runScript(int argc, char *argv[]) { - // std::cout << "Running script: " << argv[1] << std::endl; + std::map dir_options; + std::string dir = home_dir; for (auto search_dir : settings.getSetting>("search_dirs")) { - std::string script = search_dir + "/" + argv[1]; - if (std::filesystem::exists(script)) + if (std::filesystem::exists(search_dir + "/" + argv[1])) { - std::string pre_script = "cd " + search_dir + " && "; - std::string args = ""; - for (int i = 2; i < argc; i++) - { - args += argv[i]; - args += " "; - } - std::string script = pre_script + search_dir + "/" + argv[1] + " " + args; - std::cout << "executing: " << (search_dir + "/" + argv[1] + " " + args) << std::endl; - system(script.c_str()); - return; + dir_options[dir_options.size()] = search_dir; } } + + if (dir_options.size() == 0) + { + std::cout << "Script " << argv[1] << " does not exist" << std::endl; + return; + } + + if (dir_options.size() == 1) + { + dir = dir_options[0]; + } + + if (dir_options.size() > 1) + { + std::cout << "Which script do you want to run?" << std::endl; + for (auto &option : dir_options) + { + std::cout << option.first << " " << option.second << std::endl; + } + std::cout << "Enter number: "; + int num; + std::cin >> num; + dir = dir_options[num]; + } + + // for (auto search_dir : settings.getSetting>("search_dirs")) + // { + std::string script = dir + "/" + argv[1]; + if (std::filesystem::exists(script)) + { + std::string pre_script = "cd " + dir + " && "; + std::string args = ""; + for (int i = 2; i < argc; i++) + { + args += argv[i]; + args += " "; + } + std::string script = pre_script + dir + "/" + argv[1] + " " + args; + std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl; + system(script.c_str()); + return; + } + // } } void showScript(int argc, char *argv[]) { std::map dir_options; + std::string dir = ""; for (auto search_dir : settings.getSetting>("search_dirs")) { if (std::filesystem::exists(search_dir + "/" + argv[1])) @@ -137,7 +175,7 @@ void listScripts(int argc, char *argv[]) // add a script in the autom directory void addScript(int argc, char *argv[]) { - std::string add_dir = dir; + std::string dir = home_dir; if (settings.getSetting>("search_dirs").size() > 1) { @@ -150,17 +188,17 @@ void addScript(int argc, char *argv[]) std::cout << "Enter number: "; int num; std::cin >> num; - add_dir = search_dirs[num]; + dir = search_dirs[num]; } - if (std::filesystem::exists(add_dir + "/" + argv[1])) + if (std::filesystem::exists(dir + "/" + argv[1])) { - std::cout << "Script " << argv[1] << " in folder " << add_dir << " already exists" << std::endl; + std::cout << "Script " << argv[1] << " in folder " << dir << " already exists" << std::endl; return; } std::cout << "Adding script: " << argv[1] << std::endl; - std::string script = add_dir + "/" + argv[1]; + std::string script = dir + "/" + argv[1]; std::ofstream file(script); #ifdef _WIN32 @@ -173,7 +211,7 @@ void addScript(int argc, char *argv[]) file.close(); - editScript(argv[1], add_dir); + editScript(argv[1], dir); } // edit a script in the autom directory @@ -181,6 +219,7 @@ void editScript(int argc, char *argv[]) { std::map dir_options; + std::string dir = home_dir; for (auto search_dir : settings.getSetting>("search_dirs")) { @@ -227,8 +266,8 @@ void editScript(std::string name, std::string dir) void removeScript(int argc, char *argv[]) { - std::map dir_options; + std::string dir = home_dir; for (auto search_dir : settings.getSetting>("search_dirs")) { diff --git a/src/main.h b/src/main.h index ed79e7d..6a6d974 100644 --- a/src/main.h +++ b/src/main.h @@ -16,7 +16,7 @@ Settings settings; // directory for autom scripts -std::string dir = ""; +std::string home_dir = ""; Command command; // input function for parsing arguments and creating commands and running them From ef92279a93c7a7fa3dc3638376dc3d10d63147cd Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 6 Aug 2023 17:16:45 +0200 Subject: [PATCH 2/2] add make install --- makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/makefile b/makefile index 2167e33..d77a66b 100644 --- a/makefile +++ b/makefile @@ -9,3 +9,6 @@ $(BINDIR)/autom: $(SRCDIR)/main.cpp $(SRCDIR)/command.cpp $(SRCDIR)/inputparser. .PHONY: clean clean: rm -f $(BINDIR)/autom + +install: + cp $(BINDIR)/autom /usr/local/bin/autom \ No newline at end of file