diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-03-05 19:08:58 -0500 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-03-05 19:08:58 -0500 |
commit | 996d1543b1291a4ae72aded64ec847bacf4c4e7f (patch) | |
tree | b746e974f9e97fd4d49baa833fa13f199c340396 /src/ui | |
parent | 8ea29a05bc8937e9177ae5cf2c112b88cdd3a68a (diff) |
make ansi-to-tui optional
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/widgets/tui_file_preview.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/ui/widgets/tui_file_preview.rs b/src/ui/widgets/tui_file_preview.rs index b387802..c1e6632 100644 --- a/src/ui/widgets/tui_file_preview.rs +++ b/src/ui/widgets/tui_file_preview.rs @@ -1,4 +1,3 @@ -use ansi_to_tui::ansi_to_text; use tui::buffer::Buffer; use tui::layout::Rect; use tui::text::Span; @@ -14,12 +13,24 @@ impl<'a> TuiFilePreview<'a> { pub fn new(preview: &'a FilePreview) -> Self { Self { preview } } -} -impl<'a> Widget for TuiFilePreview<'a> { - fn render(self, area: Rect, buf: &mut Buffer) { - let vec = self.preview.output.as_str().as_bytes().to_vec(); - let res = ansi_to_text(vec); + #[cfg(not(feature = "syntax_highlight"))] + fn render_text_preview(&self, area: Rect, buf: &mut Buffer, vec: Vec<u8>) { + 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); + } + } + + #[cfg(feature = "syntax_highlight")] + fn render_text_preview(&self, area: Rect, buf: &mut Buffer, vec: Vec<u8>) { + use ansi_to_tui::ansi_to_text; + + let res = ansi_to_text(vec.clone()); match res { Ok(text) => { for (line, y) in text @@ -35,7 +46,6 @@ impl<'a> Widget for TuiFilePreview<'a> { 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) @@ -48,3 +58,11 @@ impl<'a> Widget for TuiFilePreview<'a> { } } } + +impl<'a> Widget for TuiFilePreview<'a> { + fn render(self, area: Rect, buf: &mut Buffer) { + let vec = self.preview.output.as_str().as_bytes().to_vec(); + self.render_text_preview(area, buf, vec); + } +} + |