-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxes.cpp
More file actions
240 lines (210 loc) · 6.94 KB
/
boxes.cpp
File metadata and controls
240 lines (210 loc) · 6.94 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "../coords.hpp"
#include "../input.hpp"
#include "../tui.hpp"
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <thread>
#include <vector>
using Box = std::pair<Coord, Coord>;
struct AppState {
Input input;
bool quit = false;
bool new_input = true;
Coord size = Coord::screen_size();
} state;
// make `x` be good for `counter_box`
std::string count(const uint64_t& x) {
// unsigned r = 0;
std::string print;
if (x % 100 == 0) {
print = std::to_string(x / 100);
print = tui::string(print[0]).on_red().black();
} else if (x % 10 == 0) {
print = std::to_string(x / 10 % 10);
print = tui::string(print[0]).on_blue().black();
} else {
print = std::to_string(x);
print = print.back();
}
return print;
}
void counter_box(Coord start, Coord end) {
assert(start.row <= end.row && start.col <= end.col);
// do rows
// from top to down
for (auto row = start.row + 1; row < end.row; ++row) {
// left row
start.with_row(row).print(count(row));
// right row
end.with_row(row).print(count(row));
}
// do columns
// top left
start.set_cursor();
for (auto col = start.col; col <= end.col; ++col) {
std::cout << count(col);
}
// bottom left
end.with_col(start.col).set_cursor();
for (auto col = start.col; col <= end.col; ++col) {
std::cout << count(col);
}
}
enum class Kind : std::uint8_t {
Empty = 0,
Basic = 1,
Bold = 2,
Rounded = 3,
};
const std::vector<std::vector<std::string>> KINDS = {{{" ", " ", " ", " ", " ", " "}},
{{"┌", "┐", "└", "┘", "│", "─"}},
{{"┏", "┓", "┗", "┛", "┃", "━"}},
{{"╭", "╮", "╰", "╯", "│", "─"}}};
// start.row ------------ start.col
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// end.row ---------------- end.col
void draw_box(Box box, Kind with) {
auto start = box.first;
auto end = box.second;
assert(start.row <= end.row && start.col <= end.col);
const auto& draw = KINDS.at(static_cast<size_t>(with));
// do rows
for (auto row = start.row + 1; row < end.row; ++row) {
// left row
start.with_row(row).print(draw[4]);
// right row
end.with_row(row).print(draw[4]);
}
// do columns
// top left
start.print(draw[0]);
for (auto i = start.col + 1; i < end.col; ++i) {
std::cout << draw[5];
}
// top right
std::cout << draw[1];
// bottom left
end.with_col(start.col).print(draw[2]);
for (auto i = start.col + 1; i < end.col; ++i) {
std::cout << draw[5];
}
// bottom right
std::cout << draw[3];
}
void handle_keys(std::vector<Box>& boxes, unsigned& cnt_box_ix) {
auto* cnt_box = &boxes[cnt_box_ix];
if (state.input == 'n' || state.input == SpecKey::Tab) {
if (cnt_box_ix++ == boxes.size() - 1) {
cnt_box_ix = 0;
}
} else if (state.input == 'p' /* || state.input == SpecKey::ShiftTab */) {
if (cnt_box_ix-- == 0) {
cnt_box_ix = static_cast<int>(boxes.size()) - 1;
}
} else if (state.input == 'j' || state.input == Arrow::Down) {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.row++;
cnt_box->second.row++;
} else if (state.input == 'k' || state.input == Arrow::Up) {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.row--;
cnt_box->second.row--;
} else if (state.input == 'h' || state.input == Arrow::Left) {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.col--;
cnt_box->second.col--;
} else if (state.input == 'l' || state.input == Arrow::Right) {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.col++;
cnt_box->second.col++;
} else if (state.input == '-') {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.row++;
cnt_box->first.col++;
cnt_box->second.row--;
cnt_box->second.col--;
} else if (state.input == '+') {
draw_box(*cnt_box, Kind::Empty);
cnt_box->first.row--;
cnt_box->first.col--;
cnt_box->second.row++;
cnt_box->second.col++;
} else if (state.input == 'd' || state.input == SpecKey::Backspace) {
draw_box(*cnt_box, Kind::Empty);
boxes.erase(boxes.begin() + cnt_box_ix);
}
}
void run() {
const auto msg = tui::string("Szia Csongi!");
auto msg_coord = [msg](bool left) {
return Coord{(state.size.row / 2) + 1,
static_cast<unsigned int>((state.size.col / 2) + ((left ? -msg.size() : +msg.size()) / 2))};
};
std::vector<Box> boxes = {
{{6, 6}, {12, 12}},
{{8, 10}, {19, 41}},
{
{20, 8},
{30, 12},
},
};
unsigned cnt_box_ix = 0;
Coord prev_size;
do {
state.size = Coord::screen_size();
if (prev_size != state.size) {
prev_size = state.size;
tui::screen::clear();
} else if (!state.new_input) {
continue; // if there's no new input, don't draw anything
}
counter_box({1, 1}, state.size);
handle_keys(boxes, cnt_box_ix);
auto msg_start = msg_coord(true);
auto msg_end = msg_coord(false);
Box msg_box = {{msg_start.row - 1, msg_start.col - 1}, {msg_end.row + 1, msg_end.col}};
if (!std::any_of(boxes.begin(), boxes.end(), [msg_box](Box item) { return item == msg_box; })) {
boxes.push_back(msg_box);
}
for (auto box : boxes) {
if (box == boxes[cnt_box_ix]) {
std::cout << tui::text::color::cyan_fg();
draw_box(box, Kind::Rounded);
std::cout << tui::text::style::reset_style();
} else {
draw_box(box, Kind::Basic);
}
}
msg_start.print(msg.bold().italic().inverted().blue());
Coord(state.size.row / 3 * 2, state.size.col / 3 * 2)
.print(tui::string("tui.hpp").blue().link("https://github.com/csboo/cpptui").on_magenta());
state.new_input = false;
std::cout.flush();
// 120fps
std::this_thread::sleep_for(std::chrono::milliseconds(8));
} while (!state.quit);
}
void handle_read() {
while (state.input != 'q' && state.input != SpecKey::CtrlC) {
state.input = Input::read();
state.new_input = true;
std::this_thread::sleep_for(std::chrono::milliseconds(8));
}
state.quit = true;
}
int main() {
tui::init();
std::thread reader(handle_read);
run();
reader.join();
tui::reset();
return 0;
}