From f5b0821860940782567a0baa17280aef04f845e3 Mon Sep 17 00:00:00 2001 From: Mathis Brossier Date: Sun, 16 Jan 2022 02:55:28 +0100 Subject: Fix panics when resizing (#1408) * Change buffer.get & buffer.get_mut to return Option, Implement Trait Index & IndexMut to panic * Prevent FilePicker from drawing outside buffer (rust panics) * apply suggestion * add function in_bounds to avoid useless calculations Co-authored-by: mathis --- helix-term/src/ui/picker.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'helix-term/src/ui/picker.rs') diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 1ef94df0..a061513c 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -159,6 +159,7 @@ impl Component for FilePicker { // |picker | | | // | | | | // +---------+ +---------+ + let render_preview = area.width > MIN_SCREEN_WIDTH_FOR_PREVIEW; let area = inner_rect(area); // -- Render the frame: @@ -492,10 +493,9 @@ impl Component for Picker { let sep_style = Style::default().fg(Color::Rgb(90, 89, 119)); let borders = BorderType::line_symbols(BorderType::Plain); for x in inner.left()..inner.right() { - surface - .get_mut(x, inner.y + 1) - .set_symbol(borders.horizontal) - .set_style(sep_style); + if let Some(cell) = surface.get_mut(x, inner.y + 1) { + cell.set_symbol(borders.horizontal).set_style(sep_style); + } } // -- Render the contents: @@ -505,7 +505,7 @@ impl Component for Picker { let selected = cx.editor.theme.get("ui.text.focus"); let rows = inner.height; - let offset = self.cursor / (rows as usize) * (rows as usize); + let offset = self.cursor / std::cmp::max(1, (rows as usize) * (rows as usize)); let files = self.matches.iter().skip(offset).map(|(index, _score)| { (index, self.options.get(*index).unwrap()) // get_unchecked @@ -513,7 +513,7 @@ impl Component for Picker { for (i, (_index, option)) in files.take(rows as usize).enumerate() { if i == (self.cursor - offset) { - surface.set_string(inner.x - 2, inner.y + i as u16, ">", selected); + surface.set_string(inner.x.saturating_sub(2), inner.y + i as u16, ">", selected); } surface.set_string_truncated( -- cgit v1.2.3-70-g09d2