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
|
pub struct Quad {
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
}
impl Quad {
pub fn new(x: usize, y: usize, width: usize, height: usize) -> Quad {
Quad {
x,
y,
width,
height,
}
}
}
pub struct CLIRenderer {
buffer: Vec<Vec<char>>,
}
impl CLIRenderer {
pub fn new(width: usize) -> CLIRenderer {
CLIRenderer {
buffer: vec![vec![' '; width]; 1],
}
}
pub fn to_string(&self) -> String {
self.buffer
.clone()
.into_iter()
.map(|inner| inner.into_iter().collect::<String>())
.collect::<Vec<String>>()
.join("\n")
}
pub fn place(&mut self, x: usize, y: usize, character: char) {
if y >= self.buffer.len() {
self.buffer.resize(
y + 1,
vec![' '; self.buffer.get(0).map_or(0, |row| row.len())],
);
}
if x >= self.buffer[y].len() {
self.buffer[y].resize(x + 1, ' ');
}
self.buffer[y][x] = character;
}
pub fn get(&mut self, x: usize, y: usize) -> char {
if y >= self.buffer.len() {
self.buffer.resize(
y + 1,
vec![' '; self.buffer.get(0).map_or(0, |row| row.len())],
);
}
if x >= self.buffer[y].len() {
self.buffer[y].resize(x + 1, ' ');
}
self.buffer[y][x]
}
pub fn draw_text(&mut self, x: usize, y: usize, text: &'static str) {
for i in 0..text.len() {
self.place(x + i, y, text.as_bytes()[i].into());
}
}
pub fn draw_vline(&mut self, x: usize, y: usize, height: usize, thick: bool) {
for i in y..y + height {
if self.get(x, i) == '─' {
self.place(x, i, if thick { '╫' } else { '┼' });
} else if self.get(x, i) == '═' {
self.place(x, i, if thick { '║' } else { '╫' });
} else {
self.place(x, i, if thick { '║' } else { '│' })
}
}
}
pub fn draw_hline(&mut self, x: usize, y: usize, width: usize, thick: bool) {
for i in x..x + width {
if self.get(i, y) == '│' {
self.place(i, y, if thick { '╪' } else { '┼' });
} else if self.get(i, y) == '║' {
self.place(i, y, if thick { '║' } else { '╫' });
} else {
self.place(i, y, if thick { '═' } else { '─' })
}
}
}
pub fn draw_quad(&mut self, quad: Quad, thick: bool) {
for x in quad.x..quad.x + quad.width {
for y in quad.y..quad.y + quad.height {
if x == quad.x && y == quad.y {
self.place(x, y, if thick { '╔' } else { '┌' });
} else if x == quad.x + quad.width - 1 && y == quad.y {
self.place(x, y, if thick { '╗' } else { '┐' });
} else if x == quad.x && y == quad.y + quad.height - 1 {
self.place(x, y, if thick { '╚' } else { '└' });
} else if x == quad.x + quad.width - 1 && y == quad.y + quad.height - 1 {
self.place(x, y, if thick { '╝' } else { '┘' });
} else if y == quad.y || y == quad.y + quad.height - 1 {
self.draw_hline(x, y, 1, thick);
} else if x == quad.x || x == quad.x + quad.width - 1 {
self.draw_vline(x, y, 1, thick);
}
}
}
}
}
|