From d598d5962e2061eb0ebe5bd0cd769a670007e093 Mon Sep 17 00:00:00 2001 From: graelo Date: Sun, 21 Mar 2021 09:03:22 +0100 Subject: [PATCH] refactor: refactor config --- src/bin/tmux_copyrat.rs | 14 ++------------ src/config/tmux_bridge.rs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/bin/tmux_copyrat.rs b/src/bin/tmux_copyrat.rs index 01583f6..caf1200 100644 --- a/src/bin/tmux_copyrat.rs +++ b/src/bin/tmux_copyrat.rs @@ -1,6 +1,3 @@ -use clap::Clap; -use std::collections::HashMap; - use copyrat::{ comm::{tmux, OutputDestination}, config::tmux_bridge::Config, @@ -10,14 +7,7 @@ use copyrat::{ /// fn main() -> Result<(), error::ParseError> { - let mut config = Config::parse(); - - if !config.ignore_options_from_tmux { - let tmux_options: HashMap = tmux::get_options("@copyrat-")?; - - // Override default values with those coming from tmux. - config.merge_map(&tmux_options)?; - } + let config = Config::initialize()?; // Identify active pane and capture its content. let panes: Vec = tmux::list_panes()?; @@ -35,7 +25,7 @@ fn main() -> Result<(), error::ParseError> { let temp_pane_spec = format!("{}.0", config.window_name); tmux::swap_pane_with(&temp_pane_spec)?; - let selection = copyrat::run(buffer, &config.cli_options); + let selection = copyrat::run(buffer, &config.basic_config); tmux::swap_pane_with(&temp_pane_spec)?; diff --git a/src/config/tmux_bridge.rs b/src/config/tmux_bridge.rs index f07bbe7..3b6b8d6 100644 --- a/src/config/tmux_bridge.rs +++ b/src/config/tmux_bridge.rs @@ -38,12 +38,24 @@ pub struct Config { #[clap(long, default_value = "pbcopy")] pub clipboard_exe: String, - // Include CLI Options + // Include fields from the basic config #[clap(flatten)] - pub cli_options: basic::Config, + pub basic_config: basic::Config, } impl Config { + pub fn initialize() -> Result { + let mut config = Config::parse(); + + if !config.ignore_options_from_tmux { + let tmux_options: HashMap = tmux::get_options("@copyrat-")?; + + // Override default values with those coming from tmux. + config.merge_map(&tmux_options)?; + } + + Ok(config) + } /// Try parsing provided options, and update self with the valid values. /// Unknown options are simply ignored. pub fn merge_map( @@ -57,7 +69,7 @@ impl Config { } // Pass the call to cli_options. - self.cli_options.merge_map(options)?; + self.basic_config.merge_map(options)?; Ok(()) }