55#include " private/conversion.hpp"
66#include " private/platform.hpp"
77
8- Term::Result Term::prompt_blocking (const std::string& message,
9- const std::string& first_option,
10- const std::string& second_option,
11- const std::string& prompt_indicator) {
8+ Term::Result Term::prompt (const std::string& message,
9+ const std::string& first_option,
10+ const std::string& second_option,
11+ const std::string& prompt_indicator,
12+ bool immediate) {
1213 Terminal term (false , true , true );
1314 std::cout << message << " [" << first_option << ' /' << second_option << ' ]'
1415 << prompt_indicator << ' ' << std::flush;
@@ -18,90 +19,76 @@ Term::Result Term::prompt_blocking(const std::string& message,
1819 return Result::ERROR;
1920 }
2021
21- std::vector<char > input;
22- unsigned short int length = 0 ;
2322 int key;
24- while (true ) {
25- key = Term::read_key ();
26- if (key >= ' a' && key <= ' z' ) {
27- std::cout << (char )key << std::flush;
28- length++;
29- input.push_back (static_cast <char >(key));
30- } else if (key >= ' A' && key <= ' Z' ) {
31- std::cout << (char )key << std::flush;
32- length++;
33- input.push_back (static_cast <char >(
34- key + 32 )); // convert upper case to lowercase
35- } else if (key == Term::Key::CTRL + ' c' ) {
36- std::cout << ' \n ' ;
37- return Result::ABORT;
38- } else if (key == Term::Key::BACKSPACE) {
39- if (length != 0 ) {
40- std::cout
41- << " \033 [D \033 [D"
42- << std::flush; // erase last line and move the cursor back
43- length--;
44- input.pop_back ();
45- }
46- } else if (key == Term::Key::ENTER) {
47- if (Private::vector_to_string (input) == " y" ||
48- Private::vector_to_string (input) == " yes" ) {
23+
24+ if (immediate) {
25+ while (true ) {
26+ key = Term::read_key ();
27+ if (key == ' y' || key == ' Y' ) {
4928 Term::write (" \n " );
5029 return Result::YES;
51- } else if (Private::vector_to_string (input) == " n" ||
52- Private::vector_to_string (input) == " no" ) {
30+ } else if (key == ' n' || key == ' N' ) {
5331 Term::write (" \n " );
5432 return Result::NO;
55- } else if (length == 0 ) {
33+ } else if (key == Term::Key::CTRL + ' c' ) {
34+ Term::write (" \n " );
35+ return Result::ABORT;
36+ } else if (key == Term::Key::ENTER) {
5637 Term::write (" \n " );
5738 return Result::NONE;
5839 } else {
5940 Term::write (" \n " );
6041 return Result::INVALID;
6142 }
6243 }
63- }
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;
44+ } else {
45+ std::vector<char > input;
46+ unsigned short int length = 0 ;
47+ while (true ) {
48+ key = Term::read_key ();
49+ if (key >= ' a' && key <= ' z' ) {
50+ std::cout << (char )key << std::flush;
51+ length++;
52+ input.push_back (static_cast <char >(key));
53+ } else if (key >= ' A' && key <= ' Z' ) {
54+ std::cout << (char )key << std::flush;
55+ length++;
56+ input.push_back (static_cast <char >(
57+ key + 32 )); // convert upper case to lowercase
58+ } else if (key == Term::Key::CTRL + ' c' ) {
59+ std::cout << ' \n ' ;
60+ return Result::ABORT;
61+ } else if (key == Term::Key::BACKSPACE) {
62+ if (length != 0 ) {
63+ std::cout << " \033 [D \033 [D"
64+ << std::flush; // erase last line and move the
65+ // cursor back
66+ length--;
67+ input.pop_back ();
68+ }
69+ } else if (key == Term::Key::ENTER) {
70+ if (Private::vector_to_string (input) == " y" ||
71+ Private::vector_to_string (input) == " yes" ) {
72+ Term::write (" \n " );
73+ return Result::YES;
74+ } else if (Private::vector_to_string (input) == " n" ||
75+ Private::vector_to_string (input) == " no" ) {
76+ Term::write (" \n " );
77+ return Result::NO;
78+ } else if (length == 0 ) {
79+ Term::write (" \n " );
80+ return Result::NONE;
81+ } else {
82+ Term::write (" \n " );
83+ return Result::INVALID;
84+ }
85+ }
9986 }
10087 }
10188}
10289
103- Term::Result_simple Term::prompt_simple (std::string message) {
104- switch (prompt_blocking (message, " Y" , " N" , " :" )) {
90+ Term::Result_simple Term::prompt_simple (const std::string& message) {
91+ switch (prompt (message, " Y" , " N" , " :" , false )) {
10592 case Result::YES:
10693 return Result_simple::YES;
10794 case Result::ABORT:
0 commit comments