forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_selection_utils.h
More file actions
116 lines (100 loc) · 3.53 KB
/
cli_selection_utils.h
File metadata and controls
116 lines (100 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <optional>
#include <string>
#include <vector>
#include "utils/string_utils.h"
namespace cli_selection_utils {
const std::string indent = std::string(4, ' ');
inline void PrintMenu(
const std::vector<std::string>& options,
const std::optional<std::string> default_option = std::nullopt,
const int start_index = 1) {
auto index{start_index};
for (const auto& option : options) {
bool is_default = false;
if (default_option.has_value() &&
string_utils::EqualsIgnoreCase(option, default_option.value())) {
is_default = true;
}
std::string selection{std::to_string(index) + ". " + option +
(is_default ? " (default)" : "") + "\n"};
std::cout << indent << selection;
index++;
}
std::endl(std::cout);
}
inline std::optional<int> GetNumericValue(const std::string& sval) {
try {
return std::stoi(sval);
} catch (const std::invalid_argument&) {
// Not a valid number
return std::nullopt;
} catch (const std::out_of_range&) {
// Number out of range
return std::nullopt;
}
}
inline std::optional<std::string> PrintModelSelection(
const std::vector<std::string>& downloaded,
const std::vector<std::string>& availables,
const std::optional<std::string> default_selection = std::nullopt) {
std::string selection;
if (!downloaded.empty()) {
std::cout << "Downloaded models:\n";
for (const auto& option : downloaded) {
std::cout << indent << option << "\n";
}
std::endl(std::cout);
}
if (!availables.empty()) {
std::cout << "Available to download:\n";
PrintMenu(availables, default_selection, 1);
}
std::cout << "Select a model (" << 1 << "-" << availables.size() << "): ";
std::getline(std::cin, selection);
// if selection is empty and default selection is inside availables, return default_selection
if (selection.empty()) {
if (default_selection.has_value()) {
for (const auto& available : availables) {
if (string_utils::EqualsIgnoreCase(available,
default_selection.value())) {
return available;
}
}
}
return std::nullopt;
}
// Validate if the selection consists solely of numeric characters
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
return std::nullopt;
}
// deal with out of range numeric values
std::optional<int> numeric_value = GetNumericValue(selection);
if (!numeric_value.has_value() || (unsigned) numeric_value.value() > availables.size() || numeric_value.value() < 1) {
return std::nullopt;
}
return availables[std::stoi(selection) - 1];
}
inline std::optional<std::string> PrintSelection(
const std::vector<std::string>& options,
const std::string& title = "Select an option") {
std::cout << title << "\n";
std::string selection;
PrintMenu(options);
std::cout << "Select an option (" << 1 << "-" << options.size() << "): ";
std::getline(std::cin, selection);
if (selection.empty()) {
return std::nullopt;
}
// Validate if the selection consists solely of numeric characters
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
return std::nullopt;
}
// deal with out of range numeric values
std::optional<int> numeric_value = GetNumericValue(selection);
if (!numeric_value.has_value() ||(unsigned) numeric_value.value() > options.size() || numeric_value.value() < 1) {
return std::nullopt;
}
return options[std::stoi(selection) - 1];
}
} // namespace cli_selection_utils