Skip to content

Commit

Permalink
refactor/doc: tweak api
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <tonygo@tonys-macbook-pro.home>
  • Loading branch information
Tony Gorez committed Feb 27, 2024
1 parent 84f9f30 commit 1e3f3df
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,40 @@
- 🌍 Cross-platform (work in progress).
- 🛠️ CMake-friendly integration (work in progress)

## 📖 Usage
## 📖 Usage (API)

### `std::optional<std::string> Prompt::ask_for_input(std::string question)`

```cpp
#include <qupp/prompt/prompt.h>

int main() {
qupp::prompt::Prompt prompt;

auto result = prompt.ask_for_input("What's your name? ");
std::cout << "Hello, " << result.value() << "!" << std::endl;

return 0;
}
```

### `std::optional<std::string> Prompt::ask_from_list(std::string question, std::vector<std::string> options)`

```cpp
#include <qupp/prompt/prompt.h>

int main() {
qupp::prompt::Prompt prompt;

auto selected = prompt.select_from_list(
"Select an option:", {"Option 1", "Option 2", "Option 3 "});
if (selected.has_value()) {
std::cout << "You selected: " << selected.value() << "\n";
} else {
std::cout << "You did not select anything."
<< "\n";
}

return 0;
}
```
Expand Down
15 changes: 8 additions & 7 deletions examples/simple_prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
int main() {
qupp::prompt::Prompt prompt;

auto result = prompt.ask_for_input("What is your name?");
if (result.has_value()) {
std::cout << "Hello " << result.value() << "\n";
} else {
std::cout << "Hello, I did not get your name."
<< "\n";
}

auto selected = prompt.select_from_list(
"Select an option:", {"Option 1", "Option 2", "Option 3 "});
if (selected.has_value()) {
Expand All @@ -13,11 +21,4 @@ int main() {
std::cout << "You did not select anything."
<< "\n";
}

auto result = prompt.ask_for_input("This is a question:");
if (result.has_value()) {
std::cout << "You answered: " << result.value() << std::endl;
} else {
std::cout << "You did not answer." << std::endl;
}
}
9 changes: 4 additions & 5 deletions src/prompt/include/qupp/prompt/prompt.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

namespace qupp::prompt {

using StringResult = std::optional<std::string>;

struct Prompt {
// The explicit keyword is used to avoid implicit conversions.
Prompt(qupp::io::TerminalInterface &terminal =
qupp::io::Terminal::get_instance());

StringResult ask_for_input(const std::string &message);
std::optional<std::string> ask_for_input(const std::string &message);

StringResult select_from_list(const std::string &message,
const std::vector<std::string> &options);
std::optional<std::string>
select_from_list(const std::string &message,
const std::vector<std::string> &options);

private:
qupp::io::TerminalInterface &terminal_;
Expand Down
7 changes: 4 additions & 3 deletions src/prompt/unix/prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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;
}
Expand All @@ -14,8 +14,9 @@ StringResult Prompt::ask_for_input(const std::string &question) {
return terminal_.read_from_console();
}

StringResult Prompt::select_from_list(const std::string &question,
const std::vector<std::string> &options) {
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;
}
Expand Down

0 comments on commit 1e3f3df

Please sign in to comment.