Skip to content

Commit 3177a27

Browse files
committed
unified blocking and non-blocking prompt and added the "immediate" toggle to chose between both versions
1 parent 1de25b1 commit 3177a27

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
lines changed

cpp-terminal/prompt.cpp

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
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:

cpp-terminal/prompt.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,19 @@ enum class Result_simple { YES, NO, ABORT };
2323

2424
// A simple yes/no prompt, requires the user to press the ENTER key to continue
2525
// The arguments are used like this: 1 [2/3]4 <user Input>
26-
Result prompt_blocking(const std::string& message,
26+
// the immediate switch indicates toggles wether pressing enter for
27+
// confirming the input is required or not
28+
Result prompt(const std::string& message,
2729
const std::string& first_option,
2830
const std::string& second_option,
29-
const std::string& prompt_indicator);
30-
31-
// A simple yes/no prompt, returns immediately after the first key press
32-
// The arguments are used like this: 1 [2/3]4 <user Input>
33-
Result prompt_non_blocking(const std::string& message,
34-
const std::string& first_option,
35-
const std::string& second_option,
36-
const std::string& prompt_indicator);
31+
const std::string& prompt_indicator,
32+
bool);
3733

3834
// The most simple prompt possible, requires the user to press enter to continue
3935
// The arguments are used like this: 1 [y/N]:
4036
// Invalid input, errors (like no attached terminal) all result in 'no' as
4137
// default
42-
Result_simple prompt_simple(std::string message);
38+
Result_simple prompt_simple(const std::string& message);
4339

4440
/* Multiline prompt */
4541

0 commit comments

Comments
 (0)