-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
219 lines (191 loc) · 7.31 KB
/
Copy pathmenu.cpp
File metadata and controls
219 lines (191 loc) · 7.31 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @file menu.cpp
* @brief Console-based interactive menu.
*
* This utility implements a navigable console menu system with support
* for keyboard navigation (arrows, hotkeys), color-coded text, and
* cursor visibility management. Designed to integrate into larger
* applications (e.g. raster rendering, toolchains).
*
* @author BO$ <https://github.com/bos-8>
* @date 2025-05-11
* @license GNU Affero General Public License v3.0 (AGPL-3.0)
*
* @details
* Features:
* - Highlighted selection with arrow or key shortcut.
* - Customizable colors via MenuColor enum.
* - Support for padding, visual cursor management.
* - Modular structure suitable for embedding into other tools.
*/
#include <conio.h>
#include <windows.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
constexpr short padding_top = 0;
constexpr short padding_bottom = 0;
constexpr u_short MENU_INVALID_INDEX = 65535;
enum MenuColor {
Default = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
Highlight = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // Yellow
Selected = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, // Magenta
KeyChar = FOREGROUND_BLUE | FOREGROUND_INTENSITY, // Light blue
ValueText = FOREGROUND_GREEN | FOREGROUND_INTENSITY, // Light green
DescriptionText = FOREGROUND_INTENSITY // Grey
};
enum Key : int {
Enter = 13,
ArrowPrefix1 = 0,
ArrowPrefix2 = 224,
ArrowUp = 72,
ArrowDown = 80,
ArrowLeft = 75,
ArrowRight = 77
};
struct Option {
u_char Value[255];
u_short Key;
u_char Description[1024];
};
static HANDLE hConsole;
u_short menu(const std::vector<Option>& options);
void printWithColor(const char* text, MenuColor color);
void setCursorPosition(short x, short y);
short getCurrentCursorY();
void setCursorVisibility(bool visible);
u_short getMaxValueLength(const std::vector<Option>& options);
void drawMenuLine(const Option& opt, size_t maxLen, bool highlight);
void drawAllMenuLines(const std::vector<Option>& options, size_t maxLen, short baseY);
void handleUserInput(const std::vector<Option>& options, u_short& index, short baseY);
void drawSelectionMarker(short y, MenuColor color);
void printWithColor(const char* text, MenuColor color) {
SetConsoleTextAttribute(hConsole, static_cast<WORD>(color));
std::cout << text;
SetConsoleTextAttribute(hConsole, static_cast<WORD>(MenuColor::Default));
}
void setCursorPosition(short x, short y) {
COORD coord = {x, y};
SetConsoleCursorPosition(hConsole, coord);
}
short getCurrentCursorY() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
return csbi.dwCursorPosition.Y;
}
void setCursorVisibility(bool visible) {
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo(hConsole, &ci);
if (static_cast<bool>(ci.bVisible) != visible) {
ci.bVisible = visible ? TRUE : FALSE;
SetConsoleCursorInfo(hConsole, &ci);
}
}
u_short getMaxValueLength(const std::vector<Option>& options) {
size_t maxLen = 0;
for (const auto& opt : options)
maxLen = max(maxLen, strlen((char*)opt.Value));
return maxLen;
}
void drawMenuLine(const Option& opt, size_t maxLen, bool highlight) {
std::cout << (highlight ? "> " : " ");
const char* val = (char*)opt.Value;
for (size_t i = 0; val[i] != '\0'; ++i) {
MenuColor color = (i == opt.Key) ? MenuColor::KeyChar : MenuColor::ValueText;
printWithColor(std::string(1, val[i]).c_str(), color);
}
std::cout << std::setw((int)(maxLen - strlen(val) + 2)) << " ";
printWithColor((char*)opt.Description, MenuColor::DescriptionText);
std::cout << "\n";
}
void drawAllMenuLines(const std::vector<Option>& options, size_t maxLen, short baseY) {
for (size_t i = 0; i < options.size(); ++i) {
setCursorPosition(0, baseY + i);
drawMenuLine(options[i], maxLen, false);
}
}
void handleUserInput(const std::vector<Option>& options, u_short& index, short baseY) {
while (true) {
int ch = _getch();
if (ch == Key::ArrowPrefix1 || ch == Key::ArrowPrefix2) {
int key = _getch();
setCursorPosition(0, baseY + index);
std::cout << " ";
if ((key == Key::ArrowUp || key == Key::ArrowLeft) && index > 0) {
--index;
} else if ((key == Key::ArrowDown || key == Key::ArrowRight) && index < options.size() - 1) {
++index;
}
drawSelectionMarker(baseY + index, MenuColor::Highlight);
} else {
ch = std::tolower(ch);
for (size_t i = 0; i < options.size(); ++i) {
char keyChar = std::tolower(options[i].Value[options[i].Key]);
if (ch == keyChar) {
setCursorPosition(0, baseY + index);
std::cout << " ";
index = static_cast<u_short>(i);
drawSelectionMarker(baseY + index, MenuColor::Highlight);
break;
}
}
if (ch == Key::Enter)
break;
}
}
}
void drawSelectionMarker(short y, MenuColor color) {
setCursorPosition(0, y);
printWithColor(">", color);
}
short getConsoleHeight() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
/**
* @brief Displays an interactive menu and returns selected option index.
*
* This function renders all menu entries with optional highlighted characters.
* It supports navigation using arrow keys (↑↓←→) or a defined character shortcut.
* Once the user confirms selection (Enter key), the selected index is returned.
*
* @param options A vector of Option structs containing the label, hotkey index, and description.
* Example:
* @code
* std::vector<Option> options = {
* {"Option1", 0, "Description1"},
* {"Option2", 0, "Description2"}
* };
* u_short result = menu(options);
* @endcode
*
* @return u_short Index of the selected option, or MENU_INVALID_INDEX if menu is empty or failed.
*
* @note The function hides the console cursor during interaction and restores it upon exit.
* @warning Assumes console window and output encoding support for ASCII.
*/
u_short menu(const std::vector<Option>& options) {
if (options.empty()) return MENU_INVALID_INDEX;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole == INVALID_HANDLE_VALUE) return MENU_INVALID_INDEX;
setCursorVisibility(false);
u_short index = 0;
size_t maxValLen = getMaxValueLength(options);
drawAllMenuLines(options, maxValLen, getCurrentCursorY() + padding_top);
short YMenuEnd = getCurrentCursorY();
short YMenuStart = YMenuEnd - options.size();
drawSelectionMarker(YMenuStart + index, MenuColor::Highlight);
handleUserInput(options, index, YMenuStart);
drawSelectionMarker(YMenuStart + index, MenuColor::Selected);
std::cout << " ";
printWithColor((char*)options[index].Value, MenuColor::Selected);
setCursorPosition(0, YMenuEnd);
for (short i = 0; i <= padding_bottom; ++i)
std::cout << '\n';
SetConsoleTextAttribute(hConsole, static_cast<WORD>(MenuColor::Default));
setCursorVisibility(true);
return index;
}
// EOF