|
| 1 | +#include <cpp-terminal/base.hpp> |
1 | 2 | #include <cpp-terminal/input.hpp> |
2 | 3 | #include <cpp-terminal/prompt.hpp> |
3 | 4 | #include <iostream> |
4 | 5 | #include "private/conversion.hpp" |
5 | 6 | #include "private/platform.hpp" |
6 | 7 |
|
| 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) { |
| 13 | + Terminal term(false, true, true); |
| 14 | + std::cout << message << " [" << first_option << '/' << second_option << ']' |
| 15 | + << prompt_indicator << ' ' << std::flush; |
| 16 | + |
| 17 | + if (!Term::is_stdin_a_tty()) { |
| 18 | + Term::write("\n"); |
| 19 | + return Result::ERROR; |
| 20 | + } |
| 21 | + |
| 22 | + int key; |
| 23 | + |
| 24 | + if (immediate) { |
| 25 | + while (true) { |
| 26 | + key = Term::read_key(); |
| 27 | + if (key == 'y' || key == 'Y') { |
| 28 | + Term::write("\n"); |
| 29 | + return Result::YES; |
| 30 | + } else if (key == 'n' || key == 'N') { |
| 31 | + Term::write("\n"); |
| 32 | + return Result::NO; |
| 33 | + } else if (key == Term::Key::CTRL + 'c' || |
| 34 | + key == Term::Key::CTRL + 'd') { |
| 35 | + Term::write("\n"); |
| 36 | + return Result::ABORT; |
| 37 | + } else if (key == Term::Key::ENTER) { |
| 38 | + Term::write("\n"); |
| 39 | + return Result::NONE; |
| 40 | + } else { |
| 41 | + Term::write("\n"); |
| 42 | + return Result::INVALID; |
| 43 | + } |
| 44 | + } |
| 45 | + } else { |
| 46 | + std::vector<char> input; |
| 47 | + unsigned short int length = 0; |
| 48 | + while (true) { |
| 49 | + key = Term::read_key(); |
| 50 | + if (key >= 'a' && key <= 'z') { |
| 51 | + std::cout << (char)key << std::flush; |
| 52 | + length++; |
| 53 | + input.push_back(static_cast<char>(key)); |
| 54 | + } else if (key >= 'A' && key <= 'Z') { |
| 55 | + std::cout << (char)key << std::flush; |
| 56 | + length++; |
| 57 | + input.push_back(static_cast<char>( |
| 58 | + key + 32)); // convert upper case to lowercase |
| 59 | + } else if (key == Term::Key::CTRL + 'c' || |
| 60 | + key == Term::Key::CTRL + 'd') { |
| 61 | + std::cout << '\n'; |
| 62 | + return Result::ABORT; |
| 63 | + } else if (key == Term::Key::BACKSPACE) { |
| 64 | + if (length != 0) { |
| 65 | + std::cout << "\033[D \033[D" |
| 66 | + << std::flush; // erase last line and move the |
| 67 | + // cursor back |
| 68 | + length--; |
| 69 | + input.pop_back(); |
| 70 | + } |
| 71 | + } else if (key == Term::Key::ENTER) { |
| 72 | + if (Private::vector_to_string(input) == "y" || |
| 73 | + Private::vector_to_string(input) == "yes") { |
| 74 | + Term::write("\n"); |
| 75 | + return Result::YES; |
| 76 | + } else if (Private::vector_to_string(input) == "n" || |
| 77 | + Private::vector_to_string(input) == "no") { |
| 78 | + Term::write("\n"); |
| 79 | + return Result::NO; |
| 80 | + } else if (length == 0) { |
| 81 | + Term::write("\n"); |
| 82 | + return Result::NONE; |
| 83 | + } else { |
| 84 | + Term::write("\n"); |
| 85 | + return Result::INVALID; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +Term::Result_simple Term::prompt_simple(const std::string& message) { |
| 93 | + switch (prompt(message, "y", "N", ":", false)) { |
| 94 | + case Result::YES: |
| 95 | + return Result_simple::YES; |
| 96 | + case Result::ABORT: |
| 97 | + return Result_simple::ABORT; |
| 98 | + case Result::NO: // falls through |
| 99 | + case Result::ERROR: // falls through |
| 100 | + case Result::NONE: // falls through |
| 101 | + case Result::INVALID: |
| 102 | + return Result_simple::NO; |
| 103 | + } |
| 104 | + // shouldn't be reached |
| 105 | + return Result_simple::NO; |
| 106 | +} |
| 107 | + |
7 | 108 | std::string Term::concat(const std::vector<std::string>& lines) { |
8 | 109 | std::string s; |
9 | 110 | for (auto& line : lines) { |
@@ -72,10 +173,11 @@ void Term::render(Term::Window& scr, const Model& m, size_t cols) { |
72 | 173 | scr.set_cursor_pos(m.prompt_string.size() + m.cursor_col, m.cursor_row); |
73 | 174 | } |
74 | 175 |
|
75 | | -std::string Term::prompt(Terminal& term, |
76 | | - const std::string& prompt_string, |
77 | | - std::vector<std::string>& history, |
78 | | - std::function<bool(std::string)>& iscomplete) { |
| 176 | +std::string Term::prompt_multiline( |
| 177 | + Terminal& term, |
| 178 | + const std::string& prompt_string, |
| 179 | + std::vector<std::string>& history, |
| 180 | + std::function<bool(std::string)>& iscomplete) { |
79 | 181 | int row, col; |
80 | 182 | bool term_attached = Private::is_stdin_a_tty(); |
81 | 183 | if (term_attached) { |
|
0 commit comments