summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-03-05 19:08:58 -0500
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-03-05 19:08:58 -0500
commit996d1543b1291a4ae72aded64ec847bacf4c4e7f (patch)
treeb746e974f9e97fd4d49baa833fa13f199c340396
parent8ea29a05bc8937e9177ae5cf2c112b88cdd3a68a (diff)
make ansi-to-tui optional
-rw-r--r--Cargo.toml3
-rw-r--r--src/ui/widgets/tui_file_preview.rs32
2 files changed, 27 insertions, 8 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e4a26b5..7276e79 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -35,7 +35,7 @@ xdg = "^2"
phf = { version = "^0", features = ["macros"], optional = true }
trash = { version = "^1", optional = true }
unicode-segmentation = "^1"
-ansi-to-tui = { git = "https://github.com/uttarayan21/ansi-to-tui" } # "^0"
+ansi-to-tui = { git = "https://github.com/uttarayan21/ansi-to-tui", optional = true } # "^0"
notify = "5.0.0-pre.13"
[features]
@@ -43,4 +43,5 @@ devicons = [ "phf" ]
file_mimetype = []
mouse = []
recycle_bin = [ "trash" ]
+syntax_highlight = [ "ansi-to-tui" ]
default = [ "devicons", "recycle_bin" ]
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);
+ }
+}
+