diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-12-26 11:37:24 -0500 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-12-27 22:06:00 -0500 |
commit | 8bae4a1ceda7dd7f4c603b736403322a0c37c3a0 (patch) | |
tree | 5e545b5df56ed653873dca49c1089f1497b2f26d /src/ui | |
parent | fee0ce69e528ad157c86405e98f4906d07de3409 (diff) |
fix crash if ansi to tui fails
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/widgets/tui_file_preview.rs | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/ui/widgets/tui_file_preview.rs b/src/ui/widgets/tui_file_preview.rs index de0d3ec..c0c6858 100644 --- a/src/ui/widgets/tui_file_preview.rs +++ b/src/ui/widgets/tui_file_preview.rs @@ -1,7 +1,7 @@ use ansi_to_tui::ansi_to_text; use tui::buffer::Buffer; use tui::layout::Rect; -use tui::text::Text; +use tui::text::{Span, Text}; use tui::widgets::Widget; use crate::preview::preview_file::FilePreview; @@ -18,14 +18,33 @@ impl<'a> TuiFilePreview<'a> { impl<'a> Widget for TuiFilePreview<'a> { fn render(self, area: Rect, buf: &mut Buffer) { - let text: Text = ansi_to_text(self.preview.output.as_str().as_bytes().to_vec()).unwrap(); - for (line, y) in text - .lines - .iter() - .skip(self.preview.index) - .zip(area.y..area.y + area.height) - { - buf.set_spans(area.x, y, line, area.width); + let vec = self.preview.output.as_str().as_bytes().to_vec(); + let res = ansi_to_text(vec); + match res { + Ok(text) => { + for (line, y) in text + .lines + .iter() + .skip(self.preview.index) + .zip(area.y..area.y + area.height) + { + buf.set_spans(area.x, y, line, area.width); + } + } + Err(e) => { + let span = Span::raw(format!("Failed to parse ansi colors: {}", e)); + buf.set_span(area.x, area.y, &span, area.width); + + let vec = self.preview.output.as_str().as_bytes().to_vec(); + for (line, y) in vec + .iter() + .skip(self.preview.index) + .zip(area.y+1..area.y + area.height) + { + let span = Span::raw(line.to_string()); + buf.set_span(area.x, y, &span, area.width); + } + } } } } |