From ef398c9900f01e76f5a39a7fb2adf01838fdfd65 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Sat, 14 May 2022 10:50:03 -0400 Subject: add multiple tui dependencies - this should fix build --- Cargo.lock | 16 ++---------- Cargo.toml | 3 ++- src/ui/widgets/tui_file_preview.rs | 52 +++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c808f7a..1e6c048 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "432bad6a85e6443b2ba2296fb43c6d843594f2f0daa55a8b86202dd3c1dc0a99" dependencies = [ - "tui 0.17.0", + "tui", ] [[package]] @@ -359,7 +359,7 @@ dependencies = [ "termion", "toml", "trash", - "tui 0.18.0", + "tui", "unicode-segmentation", "unicode-width", "users", @@ -919,18 +919,6 @@ name = "tui" version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23ed0a32c88b039b73f1b6c5acbd0554bfa5b6be94467375fd947c4de3a02271" -dependencies = [ - "bitflags", - "cassowary", - "unicode-segmentation", - "unicode-width", -] - -[[package]] -name = "tui" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96fe69244ec2af261bced1d9046a6fee6c8c2a6b0228e59e5ba39bc8ba4ed729" dependencies = [ "bitflags", "cassowary", diff --git a/Cargo.toml b/Cargo.toml index 627df1f..d5df6da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,8 @@ structopt = "^0" termion = "^1" toml = "^0" trash = { version = "^1", optional = true } -tui = { version = "^0", default-features = false, features = ["termion"] } +tui_old = { package = "tui", version = "0.17", default-features = false, features = ["termion"] } +tui = { version = "0.18", default-features = false, features = ["termion"] } unicode-segmentation = "^1" unicode-width = "^0" users = "^0" 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 = 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); } } -- cgit v1.2.3