From 09be0362d42034e343b64de08618c995b63c90fe Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 17 Nov 2022 23:12:10 -0600 Subject: feat: get intitial finder working --- src/finder/config.rs | 53 +++++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'src/finder/config.rs') diff --git a/src/finder/config.rs b/src/finder/config.rs index d0a0570..916637f 100644 --- a/src/finder/config.rs +++ b/src/finder/config.rs @@ -2,32 +2,37 @@ use figment::{providers::Serialized, value, Figment, Metadata, Profile, Provider use serde::{Deserialize, Serialize}; #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(default)] pub struct Config { pub program: String, pub args: Vec, } impl Config { - pub fn from(provider: T) -> figment::error::Result { + // Extract the configuration from any `Provider` + pub fn extract(provider: T) -> figment::error::Result { Figment::from(provider).extract() } + + // Provide a default provider, a `Figment`. + pub fn figment() -> Figment { + Figment::from(Config::default()) + } } impl Default for Config { fn default() -> Self { Self { - program: "fzf-tmux".into(), - args: vec![ - "--", + program: "fzf".into(), + args: [ "--multi", "--print-query", "-d/", - "--preview-window='right,75%,<80(up,75%,border-bottom)'", + "--preview-window=right,75%,<80(up,75%,border-bottom)", "--preview='sel={}; less ${sel:-{q}} 2>/dev/null'", ] - .into_iter() .map(Into::into) - .collect(), + .to_vec(), } } } @@ -38,7 +43,7 @@ impl Provider for Config { } fn data(&self) -> figment::error::Result> { - Serialized::defaults(Config::default()).data() + Serialized::defaults(Self::default()).data() } } @@ -48,35 +53,7 @@ mod tests { use figment::providers::{Format, Serialized, Toml}; #[test] - fn defaults() { - figment::Jail::expect_with(|jail| { - jail.create_file( - "tmuxr.toml", - r#" - program = "fzf-tmux" - args = [ - "--", - "--multi", - "--print-query", - "-d/", - "--preview-window='right,75%,<80(up,75%,border-bottom)'", - "--preview='sel={}; less ${sel:-{q}} 2>/dev/null'", - ] - "#, - )?; - - let config: Config = Figment::from(Serialized::defaults(Config::default())) - .merge(Toml::file("tmuxr.toml")) - .extract()?; - - assert_eq!(config, Config::default()); - - Ok(()) - }); - } - - #[test] - fn custom() { + fn test_extract() { figment::Jail::expect_with(|jail| { jail.create_file( "tmuxr.toml", @@ -94,7 +71,7 @@ mod tests { config, Config { program: "fzf".into(), - args: vec!["-0".into(), "-1".into(), "--preview='cat'".into()], + args: ["-0", "-1", "--preview='cat'"].map(Into::into).to_vec(), } ); -- cgit v1.2.3-70-g09d2