Skip to content

Commit 35f4555

Browse files
author
Stephan Dilly
committed
add some more safety checks in help rendering
1 parent f894ffb commit 35f4555

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/components/help.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ pub struct HelpComponent {
2727
impl DrawableComponent for HelpComponent {
2828
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _rect: Rect) {
2929
if self.visible {
30-
let (txt, selected_line) = self.get_text();
31-
32-
let height = 24;
33-
let scroll_threshold = height / 3;
34-
35-
let scroll = if selected_line > scroll_threshold {
36-
self.selection - scroll_threshold
37-
} else {
38-
0
39-
};
30+
const SIZE: (u16, u16) = (65, 24);
31+
let scroll_threshold = SIZE.1 / 3;
32+
let scroll =
33+
self.selection.saturating_sub(scroll_threshold);
4034

4135
let area =
42-
ui::centered_rect_absolute(65, height, f.size());
36+
ui::centered_rect_absolute(SIZE.0, SIZE.1, f.size());
4337

4438
f.render_widget(Clear, area);
4539
f.render_widget(
@@ -60,7 +54,7 @@ impl DrawableComponent for HelpComponent {
6054
.split(area);
6155

6256
f.render_widget(
63-
Paragraph::new(txt.iter())
57+
Paragraph::new(self.get_text().iter())
6458
.scroll(scroll)
6559
.alignment(Alignment::Left),
6660
chunks[0],
@@ -182,16 +176,17 @@ impl HelpComponent {
182176
};
183177
new_selection = cmp::max(new_selection, 0);
184178

185-
if let Ok(max) = u16::try_from(self.cmds.len() - 1) {
179+
if let Ok(max) =
180+
u16::try_from(self.cmds.len().saturating_sub(1))
181+
{
186182
self.selection = cmp::min(new_selection, max);
187183
}
188184
}
189185

190-
fn get_text<'a>(&self) -> (Vec<Text<'a>>, u16) {
186+
fn get_text<'a>(&self) -> Vec<Text<'a>> {
191187
let mut txt = Vec::new();
192188

193189
let mut processed = 0_u16;
194-
let mut selected_line = 0_u16;
195190

196191
for (key, group) in
197192
&self.cmds.iter().group_by(|e| e.text.group)
@@ -206,9 +201,7 @@ impl HelpComponent {
206201
.sorted_by_key(|e| e.order)
207202
.map(|e| {
208203
let is_selected = self.selection == processed;
209-
if is_selected {
210-
selected_line = processed;
211-
}
204+
212205
processed += 1;
213206

214207
let mut out = String::from(if is_selected {
@@ -236,6 +229,6 @@ impl HelpComponent {
236229
);
237230
}
238231

239-
(txt, selected_line)
232+
txt
240233
}
241234
}

0 commit comments

Comments
 (0)