Repository navigation
Expand file tree
/
Copy pathMenu.cpp
More file actions
121 lines (98 loc) · 3.29 KB
/
Copy pathMenu.cpp
File metadata and controls
121 lines (98 loc) · 3.29 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
//
// Created by Oliver Mrovcak on 04/01/2024.
//
#include "Menu.h"
Menu::Menu(const std::string &title, const std::vector<std::string> &options) : title(title), options(options) {
highlight = 0;
}
Menu::~Menu() {
}
void Menu::display () {
while (true) {
clear();
mvprintw(0, 10, title.c_str());
for (size_t i = 0; i < options.size(); ++i) {
if (i == highlight) {
attron(COLOR_PAIR(1));
if (inputs.find(i) != inputs.end() && !inputs[i].empty()) {
// Display the option with the user's input in the specified format.
printw("-> %s: \"%s\"\n", options[i].c_str(), inputs[i].c_str());
} else {
// Display the option without input.
printw("-> %s\n", options[i].c_str());
}
attroff(COLOR_PAIR(1));
} else {
if (inputs.find(i) != inputs.end() && !inputs[i].empty()) {
// Display the option with the user's input in the specified format.
printw(" %s: \"%s\"\n", options[i].c_str(), inputs[i].c_str());
} else {
// Display the option without input.
printw(" %s\n", options[i].c_str());
}
}
}
refresh();
int choice = getch();
switch (choice) {
case KEY_UP:
highlight = (highlight - 1 + options.size()) % options.size();
break;
case KEY_DOWN:
highlight = (highlight + 1) % options.size();
break;
case 10: // Enter key pressed
if (inputs.find(highlight) != inputs.end()) {
getInput(highlight);
} else if (actions.find(highlight) != actions.end()) {
actions[highlight]();
}
break;
}
}
}
void Menu::setAction(int optionIndex, std::function<void()> action) {
actions[optionIndex] = action;
}
void Menu::executeAction() {
if (actions.find(highlight) != actions.end()) {
actions[highlight]();
}
}
void Menu::setInput(int optionIndex, const std::string &inputPrompt) {
inputs[optionIndex] = "";
options[optionIndex] = inputPrompt + ": ";
}
std::string Menu::getInput(int optionIndex) {
if (inputs.find(optionIndex) != inputs.end()) {
echo();
// Move the cursor to the position where the input will begin.
mvprintw(2 + optionIndex, options[optionIndex].length() + 3, "");
char buf[100];
getnstr(buf, sizeof(buf) - 1);
// Save the input.
inputs[optionIndex] = buf;
noecho();
// Redraw the menu to display the updated option text with the input.
display();
}
return inputs[optionIndex];
}
void Menu::setDownloadProgress(int progress) {
downloadProgress = progress;
}
std::string Menu::getOption(int optionIndex) {
return options[optionIndex];
}
std::string Menu::getInputAsString(int optionIndex) {
return inputs[optionIndex];
}
std::string Menu::getTitle() {
return title;
}
int Menu::getHighlight() {
return highlight;
}
void Menu::setOptions(const std::vector<std::string> &options) {
this->options = options;
}