-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tony Gorez <[email protected]>
- Loading branch information
Showing
8 changed files
with
255 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <termios.h> | ||
|
||
namespace qupp::io { | ||
struct TerminalInterface { | ||
virtual ~TerminalInterface() = default; | ||
|
||
virtual void print(const std::string &message, bool break_line) = 0; | ||
virtual std::string read() = 0; | ||
virtual void print_with_prefix(const std::string &prefix, | ||
const std::string &message, | ||
bool break_line) = 0; | ||
virtual std::string read_from_console() = 0; | ||
virtual int read_in_list(int list_size) = 0; | ||
virtual void move_cursor_left(int n) = 0; | ||
virtual void move_cursor_right(int n) = 0; | ||
virtual void move_cursor_up(int n) = 0; | ||
virtual void move_cursor_down(int n) = 0; | ||
virtual void clear_line() = 0; | ||
virtual void enable_raw_mode() = 0; | ||
virtual void disable_raw_mode() = 0; | ||
}; | ||
|
||
struct Terminal : public TerminalInterface { | ||
Terminal() noexcept; // Declare the default constructor explicitly | ||
~Terminal(); // Declare the destructor explicitly | ||
|
||
static Terminal &get_instance(); | ||
|
||
void print(const std::string &message, bool break_line); | ||
std::string read(); | ||
void print_with_prefix(const std::string &prefix, const std::string &message, | ||
bool break_line); | ||
std::string read_from_console(); | ||
int read_in_list(int list_size); | ||
void move_cursor_left(int n); | ||
void move_cursor_right(int n); | ||
void move_cursor_up(int n); | ||
void move_cursor_down(int n); | ||
void clear_line(); | ||
void enable_raw_mode(); | ||
void disable_raw_mode(); | ||
|
||
private: | ||
// This is a key part of the Singleton Pattern; it prevents the creation of | ||
// additional instances. | ||
Terminal() = default; | ||
Terminal(const Terminal &) = delete; | ||
Terminal &operator=(const Terminal &) = delete; | ||
|
||
void select_below(); | ||
void select_above(); | ||
void clear_list(int list_size); | ||
|
||
// Properties. | ||
struct termios original_termios_; | ||
}; | ||
} // namespace qupp::io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,47 @@ | ||
#include "qupp/prompt/prompt.h" | ||
|
||
#include <iostream> // std::cout, std::endl | ||
#include <string> // std::string | ||
|
||
namespace qupp::prompt { | ||
Prompt::Prompt(qupp::io::TerminalInterface &terminal) : terminal_(terminal) {} | ||
|
||
StringResult Prompt::ask_for_input(const std::string &question) { | ||
std::optional<std::string> Prompt::ask_for_input(const std::string &question) { | ||
if (question.empty()) { | ||
return std::nullopt; | ||
} | ||
terminal_.print(question, false); | ||
terminal_.move_cursor_right(1); | ||
return terminal_.read(); | ||
return terminal_.read_from_console(); | ||
} | ||
|
||
std::optional<std::string> | ||
Prompt::select_from_list(const std::string &question, | ||
const std::vector<std::string> &options) { | ||
if (question.empty() || options.empty()) { | ||
return std::nullopt; | ||
} | ||
|
||
terminal_.print(question, true); | ||
terminal_.enable_raw_mode(); | ||
|
||
for (const auto &option : options) { | ||
bool is_first_option = options.front() == option; | ||
if (is_first_option) { | ||
terminal_.print_with_prefix("[*]", option, true); | ||
} else { | ||
terminal_.print_with_prefix("[ ]", option, true); | ||
} | ||
|
||
bool is_last_option = options.back() == option; | ||
if (is_last_option) { | ||
// Move cursor back to the first option. | ||
terminal_.move_cursor_up(options.size()); | ||
} | ||
} | ||
|
||
int selected_items = terminal_.read_in_list(options.size()); | ||
|
||
terminal_.disable_raw_mode(); | ||
return options[selected_items]; | ||
} | ||
} // namespace qupp::prompt |
Oops, something went wrong.