From 71dbc0d473b9df1a2bbe0e617e726a5174502d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 19 Sep 2023 17:54:17 +0200 Subject: [PATCH] Improve quitting: catch SIGINT and ask for confirmation --- src/cli_interface.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cli_interface.cpp b/src/cli_interface.cpp index 22d5c92..e51bc07 100644 --- a/src/cli_interface.cpp +++ b/src/cli_interface.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -140,7 +141,12 @@ namespace Hanabi { return true; } + void signal_handler(int signal) { + std::cout << "Use 'quit' to exit the interactive shell." << std::endl << "> "; + } + void cli(const std::shared_ptr& game) { + std::signal(SIGINT, signal_handler); // Set up GNU readline rl_attempted_completion_function = cli_command_completion; 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 << "evaluate: evaluate current game state recursively. Potentially runtime-expensive." << std::endl; std::cout << "set-initials : 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 << "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; @@ -181,9 +187,10 @@ namespace Hanabi { continue; } - if (prompt.starts_with("quit")) { + if (prompt.starts_with("quit") or prompt == "q") { std::cout << "Quitting." << std::endl; clear_history(); + std::signal(SIGINT, SIG_DFL); break; }