diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-11-21 14:26:02 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-11-21 14:26:02 -0600 |
commit | 72e9765d58b87125bdd5a2664bbc58202bdedff7 (patch) | |
tree | 656f7e2c46f8ca936439538d306f84409690946d /src/finder.rs | |
parent | fe92d86fbe5f73bc2662a128b6431ec2089d05b8 (diff) |
chore: split out projectr from tmuxr
Diffstat (limited to 'src/finder.rs')
-rw-r--r-- | src/finder.rs | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/src/finder.rs b/src/finder.rs deleted file mode 100644 index 3c74417..0000000 --- a/src/finder.rs +++ /dev/null @@ -1,111 +0,0 @@ -use figment::Provider; -use std::{ - ffi::OsStr, - io::Write, - ops::{Deref, DerefMut}, - os::unix::prelude::OsStrExt, - process::{Child, Command, Output, Stdio}, -}; - -pub(crate) use config::Config; -pub(crate) use error::{Error, Result}; - -mod config; -mod error; - -pub struct Finder { - program: String, - args: Vec<String>, -} - -impl Finder { - pub fn new() -> Result<Self> { - Self::from_provider(Config::figment()) - } - - /// Extract `Config` from `provider` to construct new `Finder` - pub fn from_provider<T: Provider>(provider: T) -> Result<Self> { - Config::extract(&provider) - .map(|config| Finder { - program: config.program, - args: config.args, - }) - .map_err(Into::into) - } - - pub fn spawn(self) -> Result<FinderChild> { - Command::new(self.program) - .args(self.args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .map(FinderChild) - .map_err(Into::into) - } -} - -pub struct FinderChild(Child); - -impl FinderChild { - pub fn find<V, I>(mut self, items: V) -> Result<Output> - where - V: IntoIterator<Item = I>, - I: AsRef<OsStr>, - { - let stdin = self.stdin.as_mut().ok_or(Error::Stdin)?; - - items.into_iter().try_for_each(|item| -> Result<()> { - stdin.write_all(item.as_ref().as_bytes())?; - stdin.write_all("\n".as_bytes()).map_err(From::from) - })?; - - self.0.wait_with_output().map_err(Into::into) - } -} - -impl Deref for FinderChild { - type Target = Child; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for FinderChild { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn test_output() { - let items = Vec::from(["item1"]); - - let finder = Finder { - program: "fzf".into(), - args: ["-1".into()].into(), - }; - - let selected = finder.spawn().unwrap().find(items).unwrap().stdout; - assert_eq!(selected.as_slice(), "item1\n".as_bytes()) - } - - #[test] - #[ignore] - fn test_selection() { - let items = Vec::from(["item1", "item2", "item3", "item4"]); - - let finder = Finder { - program: "fzf".into(), - args: [].into(), - }; - - let selected = finder.spawn().unwrap().find(items).unwrap().stdout; - assert_eq!(selected.as_slice(), "item1\n".as_bytes()) - } -} |