55#include " private/conversion.hpp"
66#include " private/platform.hpp"
77
8- Term::Result Term::prompt_blocking (const std::string& message,
8+
9+
10+ Term::Result Term::prompt (const std::string& message,
911 const std::string& first_option,
1012 const std::string& second_option,
11- const std::string& prompt_indicator) {
13+ const std::string& prompt_indicator,
14+ bool immediate) {
1215 Terminal term (false , true , true );
1316 std::cout << message << " [" << first_option << ' /' << second_option << ' ]'
1417 << prompt_indicator << ' ' << std::flush;
@@ -18,9 +21,32 @@ Term::Result Term::prompt_blocking(const std::string& message,
1821 return Result::ERROR;
1922 }
2023
24+ int key;
25+
26+ if (immediate) {
27+ while (true ) {
28+ key = Term::read_key ();
29+ if (key == ' y' || key == ' Y' ) {
30+ Term::write (" \n " );
31+ return Result::YES;
32+ } else if (key == ' n' || key == ' N' ) {
33+ Term::write (" \n " );
34+ return Result::NO;
35+ } else if (key == Term::Key::CTRL + ' c' ) {
36+ Term::write (" \n " );
37+ return Result::ABORT;
38+ } else if (key == Term::Key::ENTER) {
39+ Term::write (" \n " );
40+ return Result::NONE;
41+ } else {
42+ Term::write (" \n " );
43+ return Result::INVALID;
44+ }
45+ }
46+ }
47+ else {
2148 std::vector<char > input;
2249 unsigned short int length = 0 ;
23- int key;
2450 while (true ) {
2551 key = Term::read_key ();
2652 if (key >= ' a' && key <= ' z' ) {
@@ -61,47 +87,11 @@ Term::Result Term::prompt_blocking(const std::string& message,
6187 }
6288 }
6389 }
64- // should be unreachable
65- return Result::ERROR;
66- }
67-
68- Term::Result Term::prompt_non_blocking (const std::string& message,
69- const std::string& first_option,
70- const std::string& second_option,
71- const std::string& prompt_indicator) {
72- Terminal term (false , true , true );
73- std::cout << message << " [" << first_option << ' /' << second_option << ' ]'
74- << prompt_indicator << ' ' << std::flush;
75-
76- if (!Term::is_stdin_a_tty ()) {
77- Term::write (" \n " );
78- return Result::ERROR;
79- }
80-
81- int key;
82- while (true ) {
83- key = Term::read_key ();
84- if (key == ' y' || key == ' Y' ) {
85- Term::write (" \n " );
86- return Result::YES;
87- } else if (key == ' n' || key == ' N' ) {
88- Term::write (" \n " );
89- return Result::NO;
90- } else if (key == Term::Key::CTRL + ' c' ) {
91- Term::write (" \n " );
92- return Result::ABORT;
93- } else if (key == Term::Key::ENTER) {
94- Term::write (" \n " );
95- return Result::NONE;
96- } else {
97- Term::write (" \n " );
98- return Result::INVALID;
99- }
10090 }
10191}
10292
103- Term::Result_simple Term::prompt_simple (std::string message) {
104- switch (prompt_blocking (message, " Y" , " N" , " :" )) {
93+ Term::Result_simple Term::prompt_simple (const std::string& message) {
94+ switch (prompt (message, " Y" , " N" , " :" , false )) {
10595 case Result::YES:
10696 return Result_simple::YES;
10797 case Result::ABORT:
0 commit comments