Skip to content

Commit

Permalink
feat: abort cli if EOF detected
Browse files Browse the repository at this point in the history
fix #383
  • Loading branch information
neko-para committed Jan 19, 2025
1 parent ea66ca1 commit 39177bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions source/MaaToolkit/ProjectInterface/CLI/interactor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "interactor.h"

#include <algorithm>
#include <exception>
#include <format>
#include <functional>
#include <ranges>
Expand Down Expand Up @@ -30,6 +31,10 @@ std::vector<int> input_multi_impl(size_t size, std::string_view prompt)
std::string buffer;
std::getline(std::cin, buffer);

if (std::cin.eof()) {
throw InputEOF();
}

if (buffer.empty()) {
fail();
continue;
Expand Down
5 changes: 5 additions & 0 deletions source/MaaToolkit/ProjectInterface/CLI/interactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ class Interactor
std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomRecognitionSession> custom_recognitions_;
std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomActionSession> custom_actions_;
};

struct InputEOF : public std::exception
{
virtual const char* what() const override { return "EOF detected during input"; }
};
7 changes: 6 additions & 1 deletion source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ bool ProjectInterfaceMgr::run_cli(
return interactor.run();
}

interactor.interact();
try {
interactor.interact();
}
catch ([[maybe_unused]] const InputEOF& exc) {
std::cerr << "EOF detected, abort cli!";
}
return true;
}

Expand Down

0 comments on commit 39177bd

Please sign in to comment.