|
2 | 2 | #include <optional> |
3 | 3 | #include <string> |
4 | 4 | #include <vector> |
| 5 | +#include "utils/logging_utils.h" |
5 | 6 |
|
6 | 7 | namespace cli_selection_utils { |
7 | | -inline void PrintMenu(const std::vector<std::string>& options) { |
8 | | - auto index{1}; |
| 8 | +const std::string indent = std::string(4, ' '); |
| 9 | +inline void PrintMenu( |
| 10 | + const std::vector<std::string>& options, |
| 11 | + const std::optional<std::string> default_option = std::nullopt, |
| 12 | + const int start_index = 1) { |
| 13 | + auto index{start_index}; |
9 | 14 | for (const auto& option : options) { |
10 | | - std::cout << index << ". " << option << "\n"; |
| 15 | + bool is_default = false; |
| 16 | + if (default_option.has_value() && option == default_option.value()) { |
| 17 | + is_default = true; |
| 18 | + } |
| 19 | + std::string selection{std::to_string(index) + ". " + option + |
| 20 | + (is_default ? " (default)" : "") + "\n"}; |
| 21 | + std::cout << indent << selection; |
11 | 22 | index++; |
12 | 23 | } |
13 | 24 | std::endl(std::cout); |
14 | 25 | } |
15 | 26 |
|
| 27 | +inline std::optional<std::string> PrintModelSelection( |
| 28 | + const std::vector<std::string>& downloaded, |
| 29 | + const std::vector<std::string>& availables, |
| 30 | + const std::optional<std::string> default_selection = std::nullopt) { |
| 31 | + |
| 32 | + std::string selection{""}; |
| 33 | + if (!downloaded.empty()) { |
| 34 | + std::cout << "Downloaded models:\n"; |
| 35 | + for (const auto& option : downloaded) { |
| 36 | + std::cout << indent << option << "\n"; |
| 37 | + } |
| 38 | + std::endl(std::cout); |
| 39 | + } |
| 40 | + |
| 41 | + if (!availables.empty()) { |
| 42 | + std::cout << "Available to download:\n"; |
| 43 | + PrintMenu(availables, default_selection, 1); |
| 44 | + } |
| 45 | + |
| 46 | + std::cout << "Select a model (" << 1 << "-" << availables.size() << "): "; |
| 47 | + std::getline(std::cin, selection); |
| 48 | + |
| 49 | + // if selection is empty and default selection is inside availables, return default_selection |
| 50 | + if (selection.empty()) { |
| 51 | + if (default_selection.has_value() && |
| 52 | + std::find(availables.begin(), availables.end(), |
| 53 | + default_selection.value()) != availables.end()) { |
| 54 | + return default_selection; |
| 55 | + } |
| 56 | + return std::nullopt; |
| 57 | + } |
| 58 | + |
| 59 | + if (std::stoi(selection) > availables.size() || std::stoi(selection) < 1) { |
| 60 | + return std::nullopt; |
| 61 | + } |
| 62 | + |
| 63 | + return availables[std::stoi(selection) - 1]; |
| 64 | +} |
| 65 | + |
16 | 66 | inline std::optional<std::string> PrintSelection( |
17 | 67 | const std::vector<std::string>& options, |
18 | 68 | const std::string& title = "Select an option") { |
|
0 commit comments