-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
186 lines (160 loc) · 5.78 KB
/
utils.h
File metadata and controls
186 lines (160 loc) · 5.78 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef utils_H
#define utils_H
#include <iostream>
#include <string>
#include <vector>
template<typename T>
T getValidNumericInput(const std::string prompt) {
T value;
do {
std::cout << prompt;
std::cin >> value;
if (std::cin.fail()) {
std::cout << "Invalid choice, please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
}
else {
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
break; // Exit the loop if input is valid
}
} while (true);
return value;
}
template<typename T>
T getValidRangeInput(const std::string& prompt, const T& minValue, const T& maxValue) {
T value;
do {
std::cout << prompt;
std::cin >> value;
if (std::cin.fail() || value < minValue || value > maxValue) {
std::cout << "Invalid choice, please enter a number between " << minValue << " and " << maxValue << "!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
}
else {
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
break; // Exit the loop if input is valid
}
} while (true);
return value;
}
// Displays paginated list of items
template<typename T>
void displayItemPage(const std::vector<T>& items, int page, int pageSize, std::string menuText) {
int startIndex = (page - 1) * pageSize;
int endIndex = page * pageSize;
/*
- However, if endIndex, is greater than the size of the vector,
then set endIndex to the size of the vector to avoid going over.
*/
if (endIndex > items.size()) {
endIndex = items.size();
}
// Display menu text and the items
std::cout << menuText << std::endl;
for (int i = startIndex; i < endIndex; ++i) {
std::cout << i + 1 << ". " << items[i] << std::endl;
}
}
template<typename T>
T selectPaginatedItems(std::vector<T>& items, int pageSize, std::string menuName, std::string prompt) {
int page = 1;
const int maxPage = static_cast<int>(std::ceil(static_cast<float>(items.size()) / pageSize));
do {
// Construct text for the menu and render the menu for a particular page
std::string itemMenuText = menuName + " (Page " + std::to_string(page) + " / " + std::to_string(maxPage) + ")";
displayItemPage(items, page, pageSize, itemMenuText);
// Prompt for menu selection
int menuChoice;
std::cout << prompt << " (0 to exit, -1 for previous page, -2 for next page): ";
std::cin >> menuChoice;
// Check if the input is valid integer
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a valid number." << std::endl;
std::cin.clear();
std::cin.ignore(64, '\n');
}
else if (menuChoice == 0) {
// Exit loop and return instance created by default constructor
return T();
}
else if (menuChoice == -1) {
// Move to previous page
page -= 1;
if (page < 1) {
page = 1;
}
}
else if (menuChoice == -2) {
// Move to next page
page += 1;
if (page > maxPage) {
page = maxPage;
}
} else if (menuChoice < 1 || menuChoice > items.size()) {
// If out of range value that isn't 0, -1, or -2
std::cout << "Please enter a list value between 1 and " << items.size() << "!" << std::endl;
} else {
// Else, a list value, so index it from items and return it
T item = items[menuChoice - 1];
return item;
}
std::cout << std::endl;
} while (true);
}
// Controls paginated item menu, for viewing purposes only, rather than selecting data
template<typename T>
void navigatePaginatedItems(const std::vector<T>& items, int pageSize, std::string menuName) {
int page = 1;
const int maxPage = static_cast<int>(std::ceil(static_cast<float>(items.size()) / pageSize));
do {
std::string itemMenuText = menuName + " (Page " + std::to_string(page) + " / " + std::to_string(maxPage) + ")";
displayItemPage(items, page, pageSize, itemMenuText);
// Prompt for menu selection
int menuChoice;
std::cout << "Select number to navigate (0 to exit, -1 for previous page, -2 for next page): ";
std::cin >> menuChoice;
// Check if the input is valid
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a valid number." << std::endl;
std::cin.clear();
std::cin.ignore(64, '\n');
}
else if (menuChoice == 0) {
break; // Exit the loop
}
else if (menuChoice == -1) {
// Move to previous page
page -= 1;
if (page < 1) {
page = 1;
}
}
else if (menuChoice == -2) {
// Move to next page
page += 1;
if (page > maxPage) {
page = maxPage;
}
} else {
std::cout << "Invalid numerical choice. Please try again!" << std::endl;
}
std::cout << std::endl;
} while (page <= maxPage);
}
char promptYesOrNo(std::string prompt) {
char choice;
do {
std::cout << prompt;
std::cin >> choice;
// Convert the choice to lowercase
choice = std::tolower(choice);
// Check if the choice is valid
if (choice != 'y' && choice != 'n') {
std::cout << "Invalid choice. Please enter 'y' or 'n'." << std::endl;
}
} while (choice != 'y' && choice != 'n');
return choice;
}
#endif