From 8c7edfc55e099d6bfd5fd4ae9ede8888c9e87bfc Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 19 Nov 2023 16:36:50 +0100 Subject: [PATCH] add -config edit editor- --- src/main.cpp | 28 ++++++++++++++++++++++++++++ src/settings.cpp | 15 ++++++++------- src/settings.h | 2 ++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d66bb81..58d50f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,6 +123,34 @@ void config(int argc, char *argv[]) std::cout << settings.getSettingsAsString() << std::endl; } + else if (std::string(argv[1]) == "edit") + { + if (argc > 2) + { + if (std::string(argv[2]) == "editor") + { + + std::string editor; + if (argc > 3) + editor = argv[3]; + else + { + std::cout << "Enter editor: "; + std::cin >> editor; + } + + settings.value["editor"] = editor; + settings.writeSettings(); + return; + } + return; + } + else + { + system((std::string(settings.value["editor"]) + " " + settings.filepath).c_str()); + } + } + // if (argv[2] == "editor") // { // std::cout << "Enter editor: "; diff --git a/src/settings.cpp b/src/settings.cpp index 301dee1..e87570d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -7,6 +7,7 @@ Setup setup; Settings::Settings(void) { setup = Setup(); + filepath = setup.home + "/.automconfig.json"; readSettings(); } @@ -16,7 +17,7 @@ Settings::~Settings(void) void Settings::readSettings() { - std::ifstream file(setup.home + "/.automconfig.json"); + std::ifstream file(filepath); if (!file.is_open()) { std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl; @@ -31,12 +32,12 @@ std::string Settings::getSettingsAsString() return this->value.dump(4); } -// void Settings::writeSettings() -// { -// std::ofstream file(setup.home + "/.automconfig.json"); -// file << this->value.dump(4); -// file.close(); -// } +void Settings::writeSettings() +{ + std::ofstream file(setup.home + "/.automconfig.json"); + file << this->value.dump(4); + file.close(); +} // void Settings::set(std::string key, std::string value) // { diff --git a/src/settings.h b/src/settings.h index 8f398e7..39d6f41 100644 --- a/src/settings.h +++ b/src/settings.h @@ -26,9 +26,11 @@ private: public: json value; + std::string filepath; Settings(void); ~Settings(void); void readSettings(); + void writeSettings(); std::string getSettingsAsString(); Settings &operator=(const Settings &) = default; };