Skip to content

Commit

Permalink
perf: another impl to handle input exceptions for PiCLI
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 20, 2025
1 parent 7869740 commit f979aad
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
16 changes: 16 additions & 0 deletions source/MaaProjectInterface/CLI/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#include <csignal>
#include <format>
#include <iostream>

#include "Utils/Platform.h"
#include "Utils/Runtime.h"

#include "MaaToolkit/ProjectInterface/MaaToolkitProjectInterface.h"

extern "C" void sig_handler(int sig)
{
std::cout << std::format("\nsignal {} received, exit\n", sig);

exit(0);
}

int main(int argc, char** argv)
{
std::signal(SIGTERM, sig_handler);
std::signal(SIGSEGV, sig_handler);
std::signal(SIGINT, sig_handler);
std::signal(SIGABRT, sig_handler);

std::string user_path = MAA_NS::path_to_utf8_string(MAA_NS::library_dir());
std::string resource_path = MAA_NS::path_to_utf8_string(MAA_NS::library_dir());

Expand Down
2 changes: 1 addition & 1 deletion source/MaaToolkit/ProjectInterface/CLI/interactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::vector<int> input_multi_impl(size_t size, std::string_view prompt)
std::getline(std::cin, buffer);

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

if (buffer.empty()) {
Expand Down
7 changes: 0 additions & 7 deletions source/MaaToolkit/ProjectInterface/CLI/interactor.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <exception>

#include "MaaFramework/MaaDef.h"
#include "ProjectInterface/Configurator.h"

Expand Down Expand Up @@ -56,8 +54,3 @@ 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: 1 addition & 6 deletions source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ bool ProjectInterfaceMgr::run_cli(
return interactor.run();
}

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

Expand Down

0 comments on commit f979aad

Please sign in to comment.