From 4d0b83b601974d42d209007a6e30c043fefaaa82 Mon Sep 17 00:00:00 2001 From: Theodore Chang Date: Thu, 30 May 2024 03:52:09 +0200 Subject: [PATCH] Store file commands in history --- Toolbox/argument.cpp | 9 +-------- Toolbox/command.cpp | 25 ++++++++++++++++++++++++- Toolbox/command.h | 2 ++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Toolbox/argument.cpp b/Toolbox/argument.cpp index d31001c8d..a801303bb 100644 --- a/Toolbox/argument.cpp +++ b/Toolbox/argument.cpp @@ -355,14 +355,7 @@ void print_helper() { } void cli_mode(const shared_ptr& model) { -#ifdef SUANPAN_WIN - // ReSharper disable once CppDeprecatedEntity - auto history_path = fs::path(getenv("USERPROFILE")); // NOLINT(concurrency-mt-unsafe, clang-diagnostic-deprecated-declarations) -#else - auto history_path = fs::path(getenv("HOME")); -#endif - - history_path.append(".suanpan-history.sp"); + const auto history_path = get_history_path(); if(!exists(history_path)) { suanpan_info("It appears that this is the first time you run "); diff --git a/Toolbox/command.cpp b/Toolbox/command.cpp index 7973a580e..a81efe0b0 100644 --- a/Toolbox/command.cpp +++ b/Toolbox/command.cpp @@ -63,6 +63,7 @@ #endif using std::ifstream; +using std::ofstream; using std::string; using std::vector; @@ -1577,13 +1578,22 @@ int process_file(const shared_ptr& model, const char* file_name) { return SUANPAN_EXIT; } + ofstream output_file(get_history_path(), std::ios_base::app | std::ios_base::out); + if(output_file.is_open()) output_file << "### start processing --> " << file_name << '\n'; + string all_line, command_line; while(!getline(input_file, command_line).fail()) { if(!normalise_command(all_line, command_line)) continue; // now process the command - if(istringstream tmp_str(all_line); process_command(model, tmp_str) == SUANPAN_EXIT) return SUANPAN_EXIT; + if(output_file.is_open()) output_file << all_line << '\n'; + if(istringstream tmp_str(all_line); process_command(model, tmp_str) == SUANPAN_EXIT) { + if(output_file.is_open()) output_file << "### Finish processing --> " << file_name << '\n'; + return SUANPAN_EXIT; + } all_line.clear(); } + + if(output_file.is_open()) output_file << "### finish processing --> " << file_name << '\n'; return SUANPAN_SUCCESS; } @@ -1600,3 +1610,16 @@ int execute_command(istringstream& command) { return code; } + +fs::path get_history_path() { +#ifdef SUANPAN_WIN + // ReSharper disable once CppDeprecatedEntity + auto history_path = fs::path(getenv("USERPROFILE")); // NOLINT(concurrency-mt-unsafe, clang-diagnostic-deprecated-declarations) +#else + auto history_path = fs::path(getenv("HOME")); +#endif + + history_path.append(".suanpan-history.sp"); + + return history_path; +} diff --git a/Toolbox/command.h b/Toolbox/command.h index a855c9fec..20ec57bae 100644 --- a/Toolbox/command.h +++ b/Toolbox/command.h @@ -41,6 +41,8 @@ int process_file(const shared_ptr&, const char*); int execute_command(istringstream&); +fs::path get_history_path(); + #endif //! @}