diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-14 10:50:03 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-14 10:50:03 -0400 |
commit | ef398c9900f01e76f5a39a7fb2adf01838fdfd65 (patch) | |
tree | 1198100c7ed6cef2595b5fdaf476ce07b978fa4b | |
parent | 2693f56086f8bcaf4e06151933cf16a0d63cb091 (diff) |
add multiple tui dependencies
- this should fix build
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/ui/widgets/tui_file_preview.rs | 52 |
3 files changed, 55 insertions, 16 deletions
@@ -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", @@ -922,18 +922,6 @@ 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", "termion", "unicode-segmentation", "unicode-width", @@ -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<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); } } |