diff options
author | rabite <rabite@posteo.de> | 2019-04-16 01:54:24 +0200 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-04-16 01:54:24 +0200 |
commit | 55606bdd42bb4494e2ba85361124de3e8dd1935f (patch) | |
tree | 29efd0dfb887777083844074d44c90f568e260d5 /src | |
parent | 3149fe72fbffa22e719d080ab23249e28e977159 (diff) |
use libraryized osstrtools
Diffstat (limited to 'src')
-rw-r--r-- | src/files.rs | 148 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/proclist.rs | 3 |
3 files changed, 4 insertions, 148 deletions
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<OsString>; - 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<usize>; - fn splice_quoted(&self, from: &OsStr, to: Vec<OsString>) -> Vec<OsString>; - fn splice_with(&self, from: &OsStr, to: Vec<OsString>) -> Vec<OsString>; - fn quote(&self) -> OsString; -} - -impl OsStrTools for OsStr { - fn split(&self, pat: &OsStr) -> Vec<OsString> { - 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<OsString>) -> Vec<OsString> { - let quoted_to = to.iter() - .map(|to| to.quote()) - .collect(); - self.splice_with(from, quoted_to) - } - - fn splice_with(&self, from: &OsStr, to: Vec<OsString>) -> Vec<OsString> { - 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::<Vec<OsString>>(); - - 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<usize> { - 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 b8e0524..c34f85e 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 { |