summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-05-14 10:50:03 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-05-14 10:50:03 -0400
commitef398c9900f01e76f5a39a7fb2adf01838fdfd65 (patch)
tree1198100c7ed6cef2595b5fdaf476ce07b978fa4b /src
parent2693f56086f8bcaf4e06151933cf16a0d63cb091 (diff)
add multiple tui dependencies
- this should fix build
Diffstat (limited to 'src')
-rw-r--r--src/ui/widgets/tui_file_preview.rs52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/ui/widgets/tui_file_preview.rs b/src/ui/widgets/tui_file_preview.rs
index d8c8951..a471a64 100644
--- a/src/ui/widgets/tui_file_preview.rs
+++ b/src/ui/widgets/tui_file_preview.rs
@@ -1,10 +1,46 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
-use tui::text::Span;
+use tui::style::{Color, Style};
+use tui::text::{Span, Spans};
use tui::widgets::Widget;
use crate::preview::preview_file::FilePreview;
+fn color_to_color(color: tui_old::style::Color) -> Color {
+ match color {
+ tui_old::style::Color::Reset => Color::Reset,
+ tui_old::style::Color::Black => Color::Black,
+ tui_old::style::Color::Red => Color::Red,
+ tui_old::style::Color::Green => Color::Green,
+ tui_old::style::Color::Yellow => Color::Yellow,
+ tui_old::style::Color::Blue => Color::Blue,
+ tui_old::style::Color::Magenta => Color::Magenta,
+ tui_old::style::Color::Cyan => Color::Cyan,
+ tui_old::style::Color::Gray => Color::Gray,
+ tui_old::style::Color::DarkGray => Color::DarkGray,
+ tui_old::style::Color::LightRed => Color::LightRed,
+ tui_old::style::Color::LightGreen => Color::LightGreen,
+ tui_old::style::Color::LightYellow => Color::LightYellow,
+ tui_old::style::Color::LightBlue => Color::LightBlue,
+ tui_old::style::Color::LightMagenta => Color::LightMagenta,
+ tui_old::style::Color::LightCyan => Color::LightCyan,
+ tui_old::style::Color::White => Color::White,
+ tui_old::style::Color::Rgb(r, g, b) => Color::Rgb(r, g, b),
+ tui_old::style::Color::Indexed(i) => Color::Indexed(i),
+ }
+}
+
+fn style_to_style(style: tui_old::style::Style) -> Style {
+ let mut new_style = Style::default();
+ if let Some(fg) = style.fg {
+ new_style = new_style.fg(color_to_color(fg));
+ }
+ if let Some(bg) = style.bg {
+ new_style = new_style.fg(color_to_color(bg));
+ }
+ new_style
+}
+
pub struct TuiFilePreview<'a> {
preview: &'a FilePreview,
}
@@ -32,6 +68,7 @@ impl<'a> TuiFilePreview<'a> {
use ansi_to_tui::ansi_to_text;
let vec = s.as_bytes().to_vec();
let res = ansi_to_text(vec);
+
match res {
Ok(text) => {
for (line, y) in text
@@ -40,6 +77,19 @@ impl<'a> TuiFilePreview<'a> {
.skip(self.preview.index)
.zip(area.y..area.y + area.height)
{
+ // required to convert between different tui-rs versions.
+ // remove once ansi-to-tui depends on latest tui-rs
+ let span_vec: Vec<Span> = line
+ .0
+ .iter()
+ .map(|s| Span {
+ content: s.content.clone(),
+ style: style_to_style(s.style),
+ })
+ .collect();
+ let spans = Spans(span_vec);
+ let line = &spans;
+
buf.set_spans(area.x, y, line, area.width);
}
}