Skip to content

Commit

Permalink
fix: add missing header, doc and rename funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <gorez.tony@gmail.com>
  • Loading branch information
tony-go committed Mar 3, 2024
1 parent 341e973 commit e0c23c5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## 📖 Usage (API)

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

```cpp
#include <qupp/prompt/prompt.h>
Expand All @@ -27,15 +27,15 @@ int main() {
}
```

### `std::optional<std::string> Prompt::ask_from_list(std::string question, std::vector<std::string> options)`
### std::optional<std::string> Prompt::select(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(
auto selected = prompt.select(
"Select an option:", {"Option 1", "Option 2", "Option 3 "});
if (selected.has_value()) {
std::cout << "You selected: " << selected.value() << "\n";
Expand All @@ -48,6 +48,28 @@ int main() {
}
```

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

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

int main() {
auto choices = prompt.select_multiple("Select multiple options:",
{"Option 1", "Option 2", "Option 3 "});
if (choices.has_value()) {
std::cout << "You selected: ";
for (const auto &choice : choices.value()) {
std::cout << choice << " ";
}
std::cout << "\n";
} else {
std::cout << "You did not select anything."
<< "\n";
}
}
```


## 🤝 Contributing

### Building
Expand Down
8 changes: 4 additions & 4 deletions examples/simple_prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ int main() {
<< "\n";
}

auto selected = prompt.select_from_list(
"Select an option:", {"Option 1", "Option 2", "Option 3 "});
auto selected =
prompt.select("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";
}

auto choices = prompt.select_multiple_from_list(
"Select multiple options:", {"Option 1", "Option 2", "Option 3 "});
auto choices = prompt.select_multiple("Select multiple options:",
{"Option 1", "Option 2", "Option 3 "});
if (choices.has_value()) {
std::cout << "You selected: ";
for (const auto &choice : choices.value()) {
Expand Down
1 change: 1 addition & 0 deletions src/io/unix/io.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "qupp/io/io.h"

#include <algorithm> // std::find
#include <iostream> // std::cout, std::endl
#include <termios.h> // tcgetattr, tcsetattr
#include <unistd.h> // STDIN_FILENO
Expand Down
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 @@ -14,12 +14,11 @@ struct Prompt {
qupp::io::Terminal::get_instance());

std::optional<std::string> ask_for_input(const std::string &message);
std::optional<std::string>
select_from_list(const std::string &message,
const std::vector<std::string> &options);
std::optional<std::string> select(const std::string &message,
const std::vector<std::string> &options);
std::optional<std::vector<std::string>>
select_multiple_from_list(const std::string &message,
const std::vector<std::string> &options);
select_multiple(const std::string &message,
const std::vector<std::string> &options);

private:
qupp::io::TerminalInterface &terminal_;
Expand Down
8 changes: 4 additions & 4 deletions src/prompt/unix/prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ std::optional<std::string> Prompt::ask_for_input(const std::string &question) {
}

std::optional<std::string>
Prompt::select_from_list(const std::string &question,
const std::vector<std::string> &options) {
Prompt::select(const std::string &question,
const std::vector<std::string> &options) {
if (question.empty() || options.empty()) {
return std::nullopt;
}
Expand Down Expand Up @@ -45,8 +45,8 @@ Prompt::select_from_list(const std::string &question,
}

std::optional<std::vector<std::string>>
Prompt::select_multiple_from_list(const std::string &question,
const std::vector<std::string> &options) {
Prompt::select_multiple(const std::string &question,
const std::vector<std::string> &options) {
if (question.empty() || options.empty()) {
return std::nullopt;
}
Expand Down
16 changes: 8 additions & 8 deletions test/test_prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ TEST(ask_for_input, print) {
}

/*
* select_from_list
* select
*/

TEST(select_from_list, empty_question) {
Expand All @@ -84,7 +84,7 @@ TEST(select_from_list, empty_question) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_from_list(empty_question, options);
auto result = prompt.select(empty_question, options);

EXPECT_FALSE(result.has_value());
EXPECT_FALSE(mock_terminal.has_printed());
Expand All @@ -95,7 +95,7 @@ TEST(select_from_list, empty_options) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_from_list("question", no_options);
auto result = prompt.select("question", no_options);

EXPECT_FALSE(result.has_value());
EXPECT_FALSE(mock_terminal.has_printed());
Expand All @@ -107,7 +107,7 @@ TEST(select_from_list, print) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_from_list(question, options);
auto result = prompt.select(question, options);

EXPECT_TRUE(mock_terminal.has_printed(question));
for (const auto &option : options) {
Expand All @@ -116,7 +116,7 @@ TEST(select_from_list, print) {
}

/*
* select_multiple_from_list
* select_multiple
*/

TEST(select_multiple_from_list, empty_question) {
Expand All @@ -125,7 +125,7 @@ TEST(select_multiple_from_list, empty_question) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_multiple_from_list(empty_question, options);
auto result = prompt.select_multiple(empty_question, options);

EXPECT_FALSE(result.has_value());
EXPECT_FALSE(mock_terminal.has_printed());
Expand All @@ -136,7 +136,7 @@ TEST(select_multiple_from_list, empty_options) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_multiple_from_list("question", no_options);
auto result = prompt.select_multiple("question", no_options);

EXPECT_FALSE(result.has_value());
EXPECT_FALSE(mock_terminal.has_printed());
Expand All @@ -148,7 +148,7 @@ TEST(select_multiple_from_list, print) {
MockTerminal mock_terminal;

qupp::prompt::Prompt prompt(mock_terminal);
auto result = prompt.select_multiple_from_list(question, options);
auto result = prompt.select_multiple(question, options);

EXPECT_TRUE(mock_terminal.has_printed(question));
for (const auto &option : options) {
Expand Down

0 comments on commit e0c23c5

Please sign in to comment.