From 0354400b66b5a0463c2cf7adbab691ab746adedb Mon Sep 17 00:00:00 2001 From: rabite Date: Thu, 13 Feb 2020 00:08:36 +0100 Subject: fix pdf previews --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/preview.rs | 56 ++++++++++++++++++++++++++++++-------------------------- src/textview.rs | 33 ++++++++++++++++++++------------- 5 files changed, 77 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a90b3e..3d4e69b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -627,6 +627,7 @@ dependencies = [ "signal-notify 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "sixel 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "sixel-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "strum 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "strum_macros 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "systemstat 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1404,6 +1405,14 @@ dependencies = [ "maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strip-ansi-escapes" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "vte 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.8.0" @@ -1588,6 +1597,11 @@ name = "utf8-ranges" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "utf8parse" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "vec_map" version = "0.8.1" @@ -1603,6 +1617,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vte" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "utf8parse 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "walkdir" version = "2.3.1" @@ -1822,6 +1844,7 @@ dependencies = [ "checksum sixel-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fb46e0cd5569bf910390844174a5a99d52dd40681fff92228d221d9f8bf87dea" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" +"checksum strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d63676e2abafa709460982ddc02a3bb586b6d15a49b75c212e06edd3933acee" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum strum 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e5d1c33039533f051704951680f1adfd468fd37ac46816ded0d9ee068e60f05f" "checksum strum_macros 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "47cd23f5c7dee395a00fa20135e2ec0fffcdfa151c56182966d7a3261343432e" @@ -1844,9 +1867,11 @@ dependencies = [ "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum users 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fed7d0912567d35f88010c23dbaf865e9da8b5227295e8dc0f2fdd109155ab7" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" +"checksum utf8parse 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8772a4ccbb4e89959023bc5b7cb8623a795caa7092d99f3aa9501b9484d4557d" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum vte 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4f42f536e22f7fcbb407639765c8fd78707a33109301f834a594758bedd6e8cf" "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/Cargo.toml b/Cargo.toml index 2ae304a..bf4a32f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ rust-ini = "0.13" derivative = "1.0.3" itertools = "0.8" nix = "0.17" +strip-ansi-escapes = "0.1" image = { version = "0.21.1", optional = true } diff --git a/src/main.rs b/src/main.rs index 77a9e47..1823428 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ extern crate strum_macros; #[macro_use] extern crate derivative; extern crate nix; +extern crate strip_ansi_escapes; extern crate osstrtools; extern crate pathbuftools; diff --git a/src/preview.rs b/src/preview.rs index 1770ac4..2f1eebf 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -222,14 +222,6 @@ fn find_previewer(file: &File, g_mode: bool) -> HResult { let path = crate::paths::previewers_path()?; let ext = file.path.extension()?; - // Special case to highlight text files that aren't text/plain - if file.is_text() { - let mut previewer = PathBuf::from(&path); - previewer.push("definitions/"); - previewer.push("text"); - return Ok(ExtPreviewer::Text(previewer)); - } - // Try to find a graphical previewer first if g_mode { let g_previewer = path.read_dir()? @@ -250,10 +242,22 @@ fn find_previewer(file: &File, g_mode: bool) -> HResult { // Look for previewers matching the file extension let previewer = path.read_dir()? - .find(|previewer| previewer.as_ref() + .find(|previewer| previewer.as_ref() .and_then(|p| Ok(p.file_name() == ext )) .unwrap_or(false)) - .map(|p| p.map(|p| p.path())); + .map(|p| p.map(|p| p.path())); + match previewer { + Some(Ok(p)) => return Ok(ExtPreviewer::Text(p)), + _ => { + // Special case to highlight text files that aren't text/* + if file.is_text() { + let mut previewer = PathBuf::from(&path); + previewer.push("definitions/"); + previewer.push("text"); + return Ok(ExtPreviewer::Text(previewer)); + } + } + } Ok(ExtPreviewer::Text(previewer??)) } @@ -415,24 +419,24 @@ impl Previewer { &stale, &animator)?); } - _ => {} + _ => { + let preview = Previewer::preview_external(&file, + &core, + &stale, + &animator); + if preview.is_ok() { + return Ok(preview?); + } + } } } - let preview = Previewer::preview_external(&file, - &core, - &stale, - &animator); - if preview.is_ok() { - return Ok(preview?); - } - else { - let mut blank = TextView::new_blank(&core); - blank.set_coordinates(&coordinates).log(); - blank.refresh().log(); - blank.animate_slide_up(Some(&animator)).log(); - return Ok(PreviewWidget::TextView(blank)) - } + let mut blank = TextView::new_blank(&core); + blank.set_coordinates(&coordinates).log(); + blank.refresh().log(); + blank.animate_slide_up(Some(&animator)).log(); + return Ok(PreviewWidget::TextView(blank)) + }))) } @@ -551,8 +555,8 @@ impl Previewer { match previewer { ExtPreviewer::Text(previewer) => { + if stale.is_stale()? { return Previewer::preview_failed(&file) } let lines = Previewer::run_external(previewer, file, stale); - if stale.is_stale()? { return Previewer::preview_failed(&file) } let mut textview = TextView { diff --git a/src/textview.rs b/src/textview.rs index fa8a475..cd9a815 100644 --- a/src/textview.rs +++ b/src/textview.rs @@ -1,11 +1,14 @@ use std::io::{BufRead, BufReader}; +use strip_ansi_escapes::strip; + use crate::files::File; use crate::term::sized_string_u; use crate::widget::{Widget, WidgetCore}; -use crate::fail::HResult; +use crate::fail::{HResult, HError}; use crate::dirty::Dirtyable; + #[derive(Debug, PartialEq)] pub struct TextView { pub lines: Vec, @@ -26,12 +29,14 @@ impl TextView { pub fn new_from_file(core: &WidgetCore, file: &File) -> HResult { let file = std::fs::File::open(&file.path)?; let file = std::io::BufReader::new(file); - let lines = file.lines().map(|line| - Ok(line? - .replace("\t", " "))) - .filter_map(|l: HResult| l.ok()) - .collect(); - + let lines = file.lines() + .map(|line| line + .and_then(|l| strip(l)) + .map_err(HError::from) + .and_then(|s| std::str::from_utf8(&s) + .map(|s| s.to_string()) + .map_err(HError::from))) + .collect::>()?; Ok(TextView { lines: lines, core: core.clone(), @@ -39,6 +44,7 @@ impl TextView { offset: 0, }) } + pub fn new_from_file_limit_lines(core: &WidgetCore, file: &File, num: usize) -> HResult { @@ -46,12 +52,13 @@ impl TextView { let file = BufReader::new(file); let lines = file.lines() .take(num) - .map(|line| - Ok(line? - .replace("\t", " "))) - .filter_map(|l: HResult| l.ok()) - .collect(); - + .map(|line| line + .and_then(|l| strip(l)) + .map_err(HError::from) + .and_then(|s| std::str::from_utf8(&s) + .map(|s| s.to_string()) + .map_err(HError::from))) + .collect::>()?; Ok(TextView { lines: lines, core: core.clone(), -- cgit v1.2.3