refactor: refactor config

This commit is contained in:
graelo 2021-03-21 09:03:22 +01:00
parent 0ee29303c6
commit d598d5962e
2 changed files with 17 additions and 15 deletions

View file

@ -1,6 +1,3 @@
use clap::Clap;
use std::collections::HashMap;
use copyrat::{ use copyrat::{
comm::{tmux, OutputDestination}, comm::{tmux, OutputDestination},
config::tmux_bridge::Config, config::tmux_bridge::Config,
@ -10,14 +7,7 @@ use copyrat::{
/// ///
fn main() -> Result<(), error::ParseError> { fn main() -> Result<(), error::ParseError> {
let mut config = Config::parse(); let config = Config::initialize()?;
if !config.ignore_options_from_tmux {
let tmux_options: HashMap<String, String> = tmux::get_options("@copyrat-")?;
// Override default values with those coming from tmux.
config.merge_map(&tmux_options)?;
}
// Identify active pane and capture its content. // Identify active pane and capture its content.
let panes: Vec<tmux::Pane> = tmux::list_panes()?; let panes: Vec<tmux::Pane> = tmux::list_panes()?;
@ -35,7 +25,7 @@ fn main() -> Result<(), error::ParseError> {
let temp_pane_spec = format!("{}.0", config.window_name); let temp_pane_spec = format!("{}.0", config.window_name);
tmux::swap_pane_with(&temp_pane_spec)?; 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)?; tmux::swap_pane_with(&temp_pane_spec)?;

View file

@ -38,12 +38,24 @@ pub struct Config {
#[clap(long, default_value = "pbcopy")] #[clap(long, default_value = "pbcopy")]
pub clipboard_exe: String, pub clipboard_exe: String,
// Include CLI Options // Include fields from the basic config
#[clap(flatten)] #[clap(flatten)]
pub cli_options: basic::Config, pub basic_config: basic::Config,
} }
impl Config { impl Config {
pub fn initialize() -> Result<Config, error::ParseError> {
let mut config = Config::parse();
if !config.ignore_options_from_tmux {
let tmux_options: HashMap<String, String> = 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. /// Try parsing provided options, and update self with the valid values.
/// Unknown options are simply ignored. /// Unknown options are simply ignored.
pub fn merge_map( pub fn merge_map(
@ -57,7 +69,7 @@ impl Config {
} }
// Pass the call to cli_options. // Pass the call to cli_options.
self.cli_options.merge_map(options)?; self.basic_config.merge_map(options)?;
Ok(()) Ok(())
} }