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
use std::iter;
use std::cmp::min;
use unicode_width::UnicodeWidthStr;
use buffer::Buffer;
use widgets::{Widget, Block};
use layout::Rect;
use style::Style;
pub struct List<'a, T>
where T: AsRef<str> + 'a
{
block: Option<Block<'a>>,
items: &'a [(T, &'a Style)],
style: Style,
}
impl<'a, T> Default for List<'a, T>
where T: AsRef<str> + 'a
{
fn default() -> List<'a, T> {
List {
block: None,
items: &[],
style: Default::default(),
}
}
}
impl<'a, T> List<'a, T>
where T: AsRef<str> + 'a
{
pub fn block(&'a mut self, block: Block<'a>) -> &mut List<'a, T> {
self.block = Some(block);
self
}
pub fn items(&'a mut self, items: &'a [(T, &'a Style)]) -> &mut List<'a, T> {
self.items = items;
self
}
pub fn style(&'a mut self, style: Style) -> &mut List<'a, T> {
self.style = style;
self
}
}
impl<'a, T> Widget for List<'a, T>
where T: AsRef<str> + 'a
{
fn draw(&self, area: &Rect, buf: &mut Buffer) {
let list_area = match self.block {
Some(ref b) => {
b.draw(area, buf);
b.inner(area)
}
None => *area,
};
if list_area.width < 1 || list_area.height < 1 {
return;
}
self.background(&list_area, buf, self.style.bg);
let max_index = min(self.items.len(), list_area.height as usize);
for (i, &(ref item, style)) in self.items.iter().enumerate().take(max_index) {
buf.set_stringn(list_area.left(),
list_area.top() + i as u16,
item.as_ref(),
list_area.width as usize,
style);
}
}
}
pub struct SelectableList<'a> {
block: Option<Block<'a>>,
items: &'a [&'a str],
selected: Option<usize>,
style: Style,
highlight_style: Style,
highlight_symbol: Option<&'a str>,
}
impl<'a> Default for SelectableList<'a> {
fn default() -> SelectableList<'a> {
SelectableList {
block: None,
items: &[],
selected: None,
style: Default::default(),
highlight_style: Default::default(),
highlight_symbol: None,
}
}
}
impl<'a> SelectableList<'a> {
pub fn block(&'a mut self, block: Block<'a>) -> &mut SelectableList<'a> {
self.block = Some(block);
self
}
pub fn items(&'a mut self, items: &'a [&'a str]) -> &mut SelectableList<'a> {
self.items = items;
self
}
pub fn style(&'a mut self, style: Style) -> &mut SelectableList<'a> {
self.style = style;
self
}
pub fn highlight_symbol(&'a mut self, highlight_symbol: &'a str) -> &mut SelectableList<'a> {
self.highlight_symbol = Some(highlight_symbol);
self
}
pub fn highlight_style(&'a mut self, highlight_style: Style) -> &mut SelectableList<'a> {
self.highlight_style = highlight_style;
self
}
pub fn select(&'a mut self, index: usize) -> &'a mut SelectableList<'a> {
self.selected = Some(index);
self
}
}
impl<'a> Widget for SelectableList<'a> {
fn draw(&self, area: &Rect, buf: &mut Buffer) {
let list_area = match self.block {
Some(ref b) => b.inner(area),
None => *area,
};
let list_height = list_area.height as usize;
let (selected, highlight_style) = match self.selected {
Some(i) => (i, &self.highlight_style),
None => (0, &self.style),
};
let highlight_symbol = self.highlight_symbol.unwrap_or("");
let blank_symbol = iter::repeat(" ").take(highlight_symbol.width()).collect::<String>();
let offset = if selected >= list_height {
selected - list_height + 1
} else {
0
};
let items = self.items
.iter()
.cloned()
.enumerate()
.map(|(i, item)| if i == selected {
(format!("{} {}", highlight_symbol, item), highlight_style)
} else {
(format!("{} {}", blank_symbol, item), &self.style)
})
.skip(offset as usize)
.collect::<Vec<(String, &Style)>>();
List::default()
.block(self.block.unwrap_or_default())
.items(&items)
.draw(area, buf);
}
}