-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.cpp
More file actions
56 lines (48 loc) · 1.58 KB
/
Copy pathOptions.cpp
File metadata and controls
56 lines (48 loc) · 1.58 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
#include "Options.h"
#include <iostream>
#include <array>
#include <thread>
#include "string_util.h"
#include "simplified_io.h"
using namespace std::string_literals;
std::string make_option_header(std::vector<std::string> const& options) {
auto longest = find_longest_str(options)->size();
std::string result, top = "+--" + repeat("-", longest) + "+\n";
result.append(top);
for (int i = 0; i < options.size(); i++) {
int spaces = longest - options[i].size();
result.append("|" + std::to_string(i + 1) + "." + options[i] + repeat(" ", spaces) + "|\n");
}
return result.append(top);
}
Options::Options(std::vector<std::string> const& options) : options(options), funcs(options.size()) {}
Options::Options(std::vector<std::string>&& options) : options(options), funcs(options.size()) {}
void Options::chose(int input)
{
if (input > 0 && input < options.size() + 1) {
funcs[input - 1]();
}
else
std::cout << "Invalid Input: Please Enter Valid Option" << std::endl;
}
void Options::ask()
{
std::cout << make_option_header(options);
int input = 0;
while (input == 0) {
std::cout << "Option:";
input = getNumber();
if (input > 0 && input < options.size() + 1) {
funcs[input - 1]();
system("CLS");
ask();
return;
}
std::cout << "Invalid Input: Please Enter Valid Option" << std::endl;
input = 0;
}
}
std::function<void()>& Options::operator[](int index)
{
return funcs[index - 1];
}