2021-03-21 07:59:31 +01:00
|
|
|
use clap::Clap;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
2021-03-21 08:51:45 +01:00
|
|
|
use super::basic;
|
2021-03-21 07:59:31 +01:00
|
|
|
use crate::comm::tmux;
|
|
|
|
|
use crate::error;
|
|
|
|
|
|
|
|
|
|
/// Main configuration, parsed from command line.
|
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
|
#[clap(author, about, version)]
|
2021-03-21 08:51:45 +01:00
|
|
|
pub struct Config {
|
2021-03-21 07:59:31 +01:00
|
|
|
/// Don't read options from Tmux.
|
|
|
|
|
///
|
|
|
|
|
/// By default, options formatted like `copyrat-*` are read from tmux.
|
|
|
|
|
/// However, you should consider reading them from the config file (the
|
|
|
|
|
/// default option) as this saves both a command call (about 10ms) and a
|
|
|
|
|
/// Regex compilation.
|
|
|
|
|
#[clap(long)]
|
|
|
|
|
pub ignore_options_from_tmux: bool,
|
|
|
|
|
|
|
|
|
|
/// Name of the copyrat temporary window.
|
|
|
|
|
///
|
|
|
|
|
/// Copyrat is launched in a temporary window of that name. The only pane
|
|
|
|
|
/// in this temp window gets swapped with the current active one for
|
|
|
|
|
/// in-place searching, then swapped back and killed after we exit.
|
|
|
|
|
#[clap(long, default_value = "[copyrat]")]
|
|
|
|
|
pub window_name: String,
|
|
|
|
|
|
|
|
|
|
/// Capture visible area or entire pane history.
|
|
|
|
|
#[clap(long, arg_enum, default_value = "visible-area")]
|
|
|
|
|
pub capture_region: tmux::CaptureRegion,
|
|
|
|
|
|
|
|
|
|
/// Name of the copy-to-clipboard executable.
|
|
|
|
|
///
|
|
|
|
|
/// If during execution, the output destination is set to be clipboard,
|
|
|
|
|
/// then copyrat will pipe the selected text to this executable.
|
|
|
|
|
#[clap(long, default_value = "pbcopy")]
|
|
|
|
|
pub clipboard_exe: String,
|
|
|
|
|
|
2021-03-21 09:03:22 +01:00
|
|
|
// Include fields from the basic config
|
2021-03-21 07:59:31 +01:00
|
|
|
#[clap(flatten)]
|
2021-03-21 09:03:22 +01:00
|
|
|
pub basic_config: basic::Config,
|
2021-03-21 07:59:31 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-21 08:51:45 +01:00
|
|
|
impl Config {
|
2021-03-21 09:03:22 +01:00
|
|
|
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)
|
|
|
|
|
}
|
2021-03-21 07:59:31 +01:00
|
|
|
/// Try parsing provided options, and update self with the valid values.
|
|
|
|
|
/// Unknown options are simply ignored.
|
|
|
|
|
pub fn merge_map(
|
|
|
|
|
&mut self,
|
|
|
|
|
options: &HashMap<String, String>,
|
|
|
|
|
) -> Result<(), error::ParseError> {
|
|
|
|
|
for (name, value) in options {
|
|
|
|
|
if let "@copyrat-capture" = name.as_ref() {
|
|
|
|
|
self.capture_region = tmux::CaptureRegion::from_str(&value)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pass the call to cli_options.
|
2021-03-21 09:03:22 +01:00
|
|
|
self.basic_config.merge_map(options)?;
|
2021-03-21 07:59:31 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|