-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
116 lines (107 loc) · 3.39 KB
/
menu.cpp
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 "cpp-terminal/base.hpp"
#include "cpp-terminal/exception.hpp"
#include "cpp-terminal/input.hpp"
#include "cpp-terminal/terminal.hpp"
#include <iostream>
void render(int rows, int cols, int menuheight, int menuwidth, int menupos)
{
std::string scr;
scr.reserve(16 * 1024);
scr.append(Term::cursor_move(1, 1));
int menux0 = (cols - menuwidth) / 2;
int menuy0 = (rows - menuheight) / 2;
for(int j = 1; j <= menuy0; j++) { scr.append("\n"); }
for(int j = 1; j <= menux0; j++) { scr.append(" "); }
scr.append("┌");
for(int j = 1; j <= menuwidth; j++) { scr.append("─"); }
scr.append("┐");
scr.append(" \n");
for(int i = 1; i <= menuheight; i++)
{
for(int j = 1; j <= menux0; j++) { scr.append(" "); }
scr.append("│");
if(i == menupos)
{
scr.append(Term::color_fg(Term::Color4::RED));
scr.append(Term::color_bg(Term::Color4::GRAY));
scr.append(Term::style(Term::Style::BOLD));
}
else
{
scr.append(Term::color_fg(Term::Color4::BLUE));
scr.append(Term::color_bg(Term::Color4::GREEN));
}
std::string s = std::to_string(i) + ": item";
scr.append(s);
for(std::size_t j = 1; j <= menuwidth - s.size(); j++) { scr.append(" "); }
scr.append(Term::color_bg(Term::Color4::DEFAULT));
scr.append(Term::color_fg(Term::Color4::DEFAULT));
scr.append(Term::style(Term::Style::RESET));
scr.append("│");
scr.append(" \n");
}
for(int j = 1; j <= menux0; j++) { scr.append(" "); }
scr.append("└");
for(int j = 1; j <= menuwidth; j++) { scr.append("─"); }
scr.append("┘");
scr.append(" \n");
scr.append(Term::cursor_move(menuy0 + menuheight + 5, 1));
scr.append("Selected item: " + std::to_string(menupos) + " \n");
scr.append("Menu width: " + std::to_string(menuwidth) + " \n");
scr.append("Menu height: " + std::to_string(menuheight) + " \n");
std::cout << scr << std::flush;
}
int main()
{
try
{
// check if the terminal is capable of handling input
if(!Term::stdin_connected())
{
std::cout << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
return 1;
}
Term::Terminal term(true, true, true, true);
std::pair<std::size_t, std::size_t> term_size = Term::get_size();
int pos = 5;
int h = 10;
std::size_t w{10};
bool on = true;
while(on)
{
render(std::get<0>(term_size), std::get<1>(term_size), h, w, pos);
int key = Term::read_key();
switch(key)
{
case Term::Key::ARROW_LEFT:
if(w > 10) w--;
break;
case Term::Key::ARROW_RIGHT:
if(w < std::get<1>(term_size) - 5) w++;
break;
case Term::Key::ARROW_UP:
if(pos > 1) pos--;
break;
case Term::Key::ARROW_DOWN:
if(pos < h) pos++;
break;
case Term::Key::HOME: pos = 1; break;
case Term::Key::END: pos = h; break;
case 'q':
case Term::Key::ESC:
case Term::Key::CTRL + 'c': on = false; break;
}
}
}
catch(const Term::Exception& re)
{
std::cerr << "cpp-terminal error: " << re.what() << std::endl;
return 2;
}
catch(...)
{
std::cerr << "Unknown error." << std::endl;
return 1;
}
return 0;
}