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
use euclid::{Rect, Size2D};
use crate::{event::Event, unit::Cell, Printer, View as ViewTrait};
pub struct View<V> {
inner: V,
top_left: char,
top_right: char,
bottom_left: char,
bottom_right: char,
v_line: char,
h_line: char,
}
impl<V> View<V> {
pub fn new(view: V) -> Self {
Self {
inner: view,
top_left: '┌',
top_right: '┐',
bottom_left: '└',
bottom_right: '┘',
v_line: '│',
h_line: '─',
}
}
}
pub fn new<V>(view: V) -> View<V> {
View::new(view)
}
impl<T, M, V> ViewTrait<T, M> for View<V>
where
V: ViewTrait<T, M>,
{
fn draw(&self, printer: &Printer, focused: bool) {
let p_size = printer.size();
let top_left = (0, 0);
let bottom_left = (0, p_size.height - 1);
let gen_line = |l: char, c, r| {
let mut line = l.to_string();
for _ in 0..(p_size.width - 2) {
line.push(c);
}
line.push(r);
line
};
printer
.print(
&gen_line(self.top_left, self.h_line, self.top_right),
top_left,
)
.unwrap();
printer
.print(
&gen_line(self.bottom_left, self.h_line, self.bottom_right),
bottom_left,
)
.unwrap();
let mut buf = [0; 8];
let v_line = self.v_line.encode_utf8(&mut buf);
for y in 1..(p_size.height - 1) {
printer.print(v_line, (0, y)).unwrap();
printer.print(v_line, (p_size.width - 1, y)).unwrap();
}
let sub = printer
.to_sub_area(Rect::new(
(1, 1).into(),
(p_size.width - 2, p_size.height - 2).into(),
))
.unwrap();
self.inner.draw(&sub, focused);
}
fn width(&self) -> Size2D<u16, Cell> {
let (width, height) = self.inner.width().to_tuple();
(width.saturating_add(2), height.saturating_add(2)).into()
}
fn height(&self) -> Size2D<u16, Cell> {
let (width, height) = self.inner.height().to_tuple();
(width.saturating_add(2), height.saturating_add(2)).into()
}
fn layout(&self, constraint: Size2D<u16, Cell>) -> Size2D<u16, Cell> {
let inner = self.inner.layout(
(
constraint.width.saturating_sub(2),
constraint.height.saturating_sub(2),
)
.into(),
);
(inner.width.saturating_add(2), inner.height.saturating_add(2)).into()
}
fn event(
&mut self,
event: &Event<T>,
focused: bool,
) -> Box<dyn Iterator<Item = M>> {
self.inner.event(event, focused)
}
fn interactive(&self) -> bool {
self.inner.interactive()
}
}
pub trait ViewExt {
fn border(self) -> View<Self>
where
Self: Sized,
{
View::new(self)
}
}
impl<V> ViewExt for V {}