Skip to content

Commit

Permalink
Store file commands in history
Browse files Browse the repository at this point in the history
  • Loading branch information
TLCFEM committed May 30, 2024
1 parent 9daf863 commit 4d0b83b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
9 changes: 1 addition & 8 deletions Toolbox/argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,7 @@ void print_helper() {
}

void cli_mode(const shared_ptr<Bead>& 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 ");
Expand Down
25 changes: 24 additions & 1 deletion Toolbox/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#endif

using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;

Expand Down Expand Up @@ -1577,13 +1578,22 @@ int process_file(const shared_ptr<Bead>& 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;
}

Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions Toolbox/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ int process_file(const shared_ptr<Bead>&, const char*);

int execute_command(istringstream&);

fs::path get_history_path();

#endif

//! @}

0 comments on commit 4d0b83b

Please sign in to comment.