Improve quitting: catch SIGINT and ask for confirmation

This commit is contained in:
Maximilian Keßler 2023-09-19 17:54:17 +02:00
parent 05d348b7e4
commit 71dbc0d473
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -1,4 +1,5 @@
#include <cstdio> #include <cstdio>
#include <csignal>
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <iostream> #include <iostream>
@ -140,7 +141,12 @@ namespace Hanabi {
return true; return true;
} }
void signal_handler(int signal) {
std::cout << "Use 'quit' to exit the interactive shell." << std::endl << "> ";
}
void cli(const std::shared_ptr<HanabiStateIF>& game) { void cli(const std::shared_ptr<HanabiStateIF>& game) {
std::signal(SIGINT, signal_handler);
// Set up GNU readline // Set up GNU readline
rl_attempted_completion_function = cli_command_completion; rl_attempted_completion_function = cli_command_completion;
using_history(); using_history();
@ -162,7 +168,7 @@ namespace Hanabi {
std::cout << "actions: display list of reasonable actions to take and their winning chances." << std::endl; std::cout << "actions: display list of reasonable actions to take and their winning chances." << std::endl;
std::cout << "evaluate: evaluate current game state recursively. Potentially runtime-expensive." << std::endl; std::cout << "evaluate: evaluate current game state recursively. Potentially runtime-expensive." << std::endl;
std::cout << "set-initials <chars>: Set initials for the suits." << std::endl; std::cout << "set-initials <chars>: Set initials for the suits." << std::endl;
std::cout << "quit: Quit this interactive shell." << std::endl; std::cout << "(q)uit: Quit this interactive shell." << std::endl;
std::cout << "id: display id of state. Has no inherent meaning, useful for debugging." << std::endl; std::cout << "id: display id of state. Has no inherent meaning, useful for debugging." << std::endl;
std::cout << "dump-id-parts: Dump parts used to calculate the id of the state as well as the cards associated to them." << std::endl; std::cout << "dump-id-parts: Dump parts used to calculate the id of the state as well as the cards associated to them." << std::endl;
std::cout << "help: Display this help message." << std::endl; std::cout << "help: Display this help message." << std::endl;
@ -181,9 +187,10 @@ namespace Hanabi {
continue; continue;
} }
if (prompt.starts_with("quit")) { if (prompt.starts_with("quit") or prompt == "q") {
std::cout << "Quitting." << std::endl; std::cout << "Quitting." << std::endl;
clear_history(); clear_history();
std::signal(SIGINT, SIG_DFL);
break; break;
} }