diff options
author | Mathis Brossier | 2022-01-16 01:55:28 +0000 |
---|---|---|
committer | GitHub | 2022-01-16 01:55:28 +0000 |
commit | f5b0821860940782567a0baa17280aef04f845e3 (patch) | |
tree | 40b72ef97945fb535fd7e3f29898c1b59a1e4a07 /helix-term/src/ui/picker.rs | |
parent | 9da0abaa5d0744d6480c6e6f23a3c5ddb5ae0bce (diff) |
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 <mathis.brossier@universite-paris-saclay.fr>
Diffstat (limited to 'helix-term/src/ui/picker.rs')
-rw-r--r-- | helix-term/src/ui/picker.rs | 12 |
1 files changed, 6 insertions, 6 deletions
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<T: 'static> Component for FilePicker<T> { // |picker | | | // | | | | // +---------+ +---------+ + let render_preview = area.width > MIN_SCREEN_WIDTH_FOR_PREVIEW; let area = inner_rect(area); // -- Render the frame: @@ -492,10 +493,9 @@ impl<T: 'static> Component for Picker<T> { 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<T: 'static> Component for Picker<T> { 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<T: 'static> Component for Picker<T> { 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( |