diff --git a/Cargo.lock b/Cargo.lock index cd54f59..bf2636d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -250,6 +250,7 @@ dependencies = [ "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", "lscolors 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "osstrtools 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "parse-ansi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "signal-notify 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -463,6 +464,11 @@ name = "ordermap" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "osstrtools" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "owning_ref" version = "0.3.3" @@ -903,6 +909,7 @@ dependencies = [ "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" +"checksum osstrtools 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6fc89dd453b26466b5b3122d862d6c6b08082f404b396345793022eb3c626fd6" "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e" "checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa" diff --git a/Cargo.toml b/Cargo.toml index 2f2396b..95cd5a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ notify = "4.0.9" parse-ansi = "0.1.6" signal-notify = "0.1.3" systemstat = "0.1.4" +osstrtools = "0.1.0" #[profile.release] diff --git a/src/files.rs b/src/files.rs index d1a3775..eaf7d37 100644 --- a/src/files.rs +++ b/src/files.rs @@ -20,6 +20,7 @@ use failure::Error; use notify::DebouncedEvent; use rayon::{ThreadPool, ThreadPoolBuilder}; use alphanumeric_sort::compare_str; +use osstrtools::OsStrTools; use crate::fail::{HResult, HError, ErrorLog}; use crate::dirty::{AsyncDirtyBit, DirtyBit, Dirtyable}; @@ -1064,150 +1065,3 @@ impl PathBufExt for PathBuf { OsString::from_vec(quoted) } } - -pub trait OsStrTools { - fn split(&self, pat: &OsStr) -> Vec; - fn replace(&self, from: &OsStr, to: &OsStr) -> OsString; - fn trim_last_space(&self) -> OsString; - fn contains_osstr(&self, pat: &OsStr) -> bool; - fn position(&self, pat: &OsStr) -> Option; - fn splice_quoted(&self, from: &OsStr, to: Vec) -> Vec; - fn splice_with(&self, from: &OsStr, to: Vec) -> Vec; - fn quote(&self) -> OsString; -} - -impl OsStrTools for OsStr { - fn split(&self, pat: &OsStr) -> Vec { - let orig_string = self.as_bytes().to_vec(); - let pat = pat.as_bytes().to_vec(); - let pat_len = pat.len(); - - let split_string = orig_string - .windows(pat_len) - .enumerate() - .fold(Vec::new(), |mut split_pos, (i, chars)| { - if chars == pat.as_slice() { - if split_pos.len() == 0 { - split_pos.push((0, i)); - } else { - let len = split_pos.len(); - let last_split = split_pos[len-1].1; - split_pos.push((last_split, i)); - } - } - split_pos - }).iter() - .map(|(start, end)| { - OsString::from_vec(orig_string[*start..*end] - .to_vec()).replace(&OsString::from_vec(pat.clone()), - &OsString::from("")) - }).collect(); - split_string - } - - - fn quote(&self) -> OsString { - let mut string = self.as_bytes().to_vec(); - let mut quote = "\"".as_bytes().to_vec(); - - let mut quoted = vec![]; - quoted.append(&mut quote.clone()); - quoted.append(&mut string); - quoted.append(&mut quote); - - OsString::from_vec(quoted) - } - - fn splice_quoted(&self, from: &OsStr, to: Vec) -> Vec { - let quoted_to = to.iter() - .map(|to| to.quote()) - .collect(); - self.splice_with(from, quoted_to) - } - - fn splice_with(&self, from: &OsStr, to: Vec) -> Vec { - let pos = self.position(from); - - if pos.is_none() { - return vec![OsString::from(self)]; - } - - let pos = pos.unwrap(); - let string = self.as_bytes().to_vec(); - let from = from.as_bytes().to_vec(); - let fromlen = from.len(); - - let lpart = OsString::from_vec(string[0..pos].to_vec()); - let rpart = OsString::from_vec(string[pos+fromlen..].to_vec()); - - let mut result = vec![ - vec![lpart.trim_last_space()], - to, - vec![rpart] - ].into_iter() - .flatten() - .filter(|part| part.len() != 0) - .collect::>(); - - if result.last() == Some(&OsString::from("")) { - result.pop(); - result - } else { result } - } - - fn replace(&self, from: &OsStr, to: &OsStr) -> OsString { - let orig_string = self.as_bytes().to_vec(); - let from = from.as_bytes(); - let to = to.as_bytes().to_vec(); - let from_len = from.len(); - - let new_string = orig_string - .windows(from_len) - .enumerate() - .fold(Vec::new(), |mut pos, (i, chars)| { - if chars == from { - pos.push(i); - } - pos - }).iter().rev().fold(orig_string.to_vec(), |mut string, pos| { - let pos = *pos; - string.splice(pos..pos+from_len, to.clone()); - string - }); - - OsString::from_vec(new_string) - } - - fn trim_last_space(&self) -> OsString { - let string = self.as_bytes(); - let len = string.len(); - - if len > 0 { - OsString::from_vec(string[..len-1].to_vec()) - } else { - self.to_os_string() - } - } - - fn contains_osstr(&self, pat: &OsStr) -> bool { - let string = self.as_bytes(); - let pat = pat.as_bytes(); - let pat_len = pat.len(); - - string.windows(pat_len) - .find(|chars| - chars == &pat - ).is_some() - } - - fn position(&self, pat: &OsStr) -> Option { - let string = self.as_bytes(); - let pat = pat.as_bytes(); - let pat_len = pat.len(); - - string.windows(pat_len) - .position(|chars| - chars == pat - ) - } -} diff --git a/src/main.rs b/src/main.rs index d91c078..ea3d908 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ extern crate parse_ansi; extern crate signal_notify; extern crate tree_magic; extern crate systemstat; +extern crate osstrtools; use failure::Fail; diff --git a/src/proclist.rs b/src/proclist.rs index eda40c1..f40b46d 100644 --- a/src/proclist.rs +++ b/src/proclist.rs @@ -8,6 +8,7 @@ use std::os::unix::ffi::OsStringExt; use termion::event::Key; use unicode_width::UnicodeWidthStr; +use osstrtools::OsStrTools; use crate::listview::{Listable, ListView}; use crate::textview::TextView; @@ -18,7 +19,7 @@ use crate::dirty::Dirtyable; use crate::hbox::HBox; use crate::fail::{HResult, HError, ErrorLog}; use crate::term; -use crate::files::{File, OsStrTools}; +use crate::files::File; #[derive(Debug)] struct Process {