-
Notifications
You must be signed in to change notification settings - Fork 56
/
menu_window.cpp
122 lines (116 loc) · 3.82 KB
/
menu_window.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
117
118
119
120
121
122
/*
* cpp-terminal
* C++ library for writing multi-platform terminal applications.
*
* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
*
* SPDX-License-Identifier: MIT
*/
#include "cpp-terminal/exception.hpp"
#include "cpp-terminal/input.hpp"
#include "cpp-terminal/iostream.hpp"
#include "cpp-terminal/key.hpp"
#include "cpp-terminal/screen.hpp"
#include "cpp-terminal/terminal.hpp"
#include "cpp-terminal/tty.hpp"
#include "cpp-terminal/window.hpp"
static std::string render(Term::Window& scr, const std::size_t& rows, const std::size_t& cols, const std::size_t& menuheight, const std::size_t& menuwidth, const std::size_t& menupos)
{
scr.clear();
const std::size_t menux0 = ((cols - menuwidth) / 2) + 1;
const std::size_t menuy0 = ((rows - menuheight) / 2) + 1;
scr.print_rect(menux0, menuy0, menux0 + menuwidth + 1, menuy0 + menuheight + 1);
for(std::size_t i = 1; i <= menuheight; ++i)
{
const std::string item = std::to_string(i) + ": item";
scr.print_str(menux0 + 1, menuy0 + i, item);
if(i == menupos)
{
scr.fill_fg(menux0 + 1, menuy0 + i, menux0 + item.size(), menuy0 + i, Term::Color::Name::Red); // FG
scr.fill_bg(menux0 + 1, menuy0 + i, menux0 + menuwidth, menuy0 + i, Term::Color::Name::Gray); // BG
scr.fill_style(menux0 + 1, menuy0 + i, menux0 + item.size(), menuy0 + i, Term::Style::Bold);
}
else
{
scr.fill_fg(menux0 + 1, menuy0 + i, menux0 + item.size(), menuy0 + i, Term::Color::Name::Blue);
scr.fill_bg(menux0 + 1, menuy0 + i, menux0 + menuwidth, menuy0 + i, Term::Color::Name::Green);
}
}
const std::size_t y = menuy0 + menuheight + 5;
scr.print_str(1, y, "Selected item: " + std::to_string(menupos));
scr.print_str(1, y + 1, "Menu width: " + std::to_string(menuwidth));
scr.print_str(1, y + 2, "Menu height: " + std::to_string(menuheight));
scr.print_str(1, y + 3, "Unicode test: Ondřej Čertík, ἐξήκοι");
return scr.render(1, 1, false);
}
int main()
{
try
{
// check if the terminal is capable of handling input
if(!Term::is_stdin_a_tty())
{
Term::cout << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
return 1;
}
Term::terminal.setOptions(Term::Option::ClearScreen, Term::Option::NoSignalKeys, Term::Option::NoCursor, Term::Option::Raw);
Term::Screen term_size = Term::screen_size();
std::size_t pos{5};
std::size_t h{10};
std::size_t w{10};
bool on = true;
Term::Window scr(term_size);
bool need_to_render{true};
while(on)
{
if(need_to_render)
{
Term::cout << ::render(scr, term_size.rows(), term_size.columns(), h, w, pos) << std::flush;
need_to_render = false;
}
Term::Key key = Term::read_event();
switch(key)
{
case Term::Key::ArrowLeft:
if(w > 10) { --w; }
need_to_render = true;
break;
case Term::Key::ArrowRight:
if(w < (term_size.columns() - 5)) { ++w; }
need_to_render = true;
break;
case Term::Key::ArrowUp:
if(pos > 1) { --pos; }
need_to_render = true;
break;
case Term::Key::ArrowDown:
if(pos < h) { ++pos; }
need_to_render = true;
break;
case Term::Key::Home:
pos = 1;
need_to_render = true;
break;
case Term::Key::End:
pos = h;
need_to_render = true;
break;
case Term::Key::q:
case Term::Key::Esc:
case Term::Key::Ctrl_C: on = false; break;
default: break;
}
}
}
catch(const Term::Exception& re)
{
Term::cerr << "cpp-terminal error: " << re.what() << std::endl;
return 2;
}
catch(...)
{
Term::cerr << "Unknown error." << std::endl;
return 1;
}
return 0;
}