From 39177bd9b426e53bbb22a45f1447766f16bb0b6d Mon Sep 17 00:00:00 2001 From: nekosu <liao.hy@outlook.com> Date: Mon, 20 Jan 2025 01:44:14 +0800 Subject: [PATCH] feat: abort cli if EOF detected fix #383 --- source/MaaToolkit/ProjectInterface/CLI/interactor.cpp | 5 +++++ source/MaaToolkit/ProjectInterface/CLI/interactor.h | 5 +++++ source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp | 7 ++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/source/MaaToolkit/ProjectInterface/CLI/interactor.cpp b/source/MaaToolkit/ProjectInterface/CLI/interactor.cpp index 2b0525ebc..12db9a519 100644 --- a/source/MaaToolkit/ProjectInterface/CLI/interactor.cpp +++ b/source/MaaToolkit/ProjectInterface/CLI/interactor.cpp @@ -1,6 +1,7 @@ #include "interactor.h" #include <algorithm> +#include <exception> #include <format> #include <functional> #include <ranges> @@ -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; diff --git a/source/MaaToolkit/ProjectInterface/CLI/interactor.h b/source/MaaToolkit/ProjectInterface/CLI/interactor.h index 873b5cd48..914369430 100644 --- a/source/MaaToolkit/ProjectInterface/CLI/interactor.h +++ b/source/MaaToolkit/ProjectInterface/CLI/interactor.h @@ -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"; } +}; diff --git a/source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp b/source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp index 12094dd9f..2c3567cf7 100644 --- a/source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp +++ b/source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp @@ -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; }