refactor: config names

This commit is contained in:
graelo 2021-03-21 08:51:45 +01:00
parent d558c81183
commit 0ee29303c6
6 changed files with 23 additions and 26 deletions

View file

@ -3,10 +3,10 @@ use std::fs::OpenOptions;
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, Read}; use std::io::{self, Read};
use copyrat::{config::CliOpt, run, ui::Selection}; use copyrat::{config::basic, run, ui::Selection};
fn main() { fn main() {
let opt = CliOpt::parse(); let opt = basic::Config::parse();
// Copy the pane contents (piped in via stdin) into a buffer, and split lines. // Copy the pane contents (piped in via stdin) into a buffer, and split lines.
let stdin = io::stdin(); let stdin = io::stdin();

View file

@ -3,20 +3,20 @@ use std::collections::HashMap;
use copyrat::{ use copyrat::{
comm::{tmux, OutputDestination}, comm::{tmux, OutputDestination},
config::BridgeOpt, config::tmux_bridge::Config,
error, error,
ui::Selection, ui::Selection,
}; };
/// ///
fn main() -> Result<(), error::ParseError> { fn main() -> Result<(), error::ParseError> {
let mut opt = BridgeOpt::parse(); let mut config = Config::parse();
if !opt.ignore_options_from_tmux { if !config.ignore_options_from_tmux {
let tmux_options: HashMap<String, String> = tmux::get_options("@copyrat-")?; let tmux_options: HashMap<String, String> = tmux::get_options("@copyrat-")?;
// Override default values with those coming from tmux. // Override default values with those coming from tmux.
opt.merge_map(&tmux_options)?; config.merge_map(&tmux_options)?;
} }
// Identify active pane and capture its content. // Identify active pane and capture its content.
@ -27,15 +27,15 @@ fn main() -> Result<(), error::ParseError> {
.find(|p| p.is_active) .find(|p| p.is_active)
.expect("Exactly one tmux pane should be active in the current window."); .expect("Exactly one tmux pane should be active in the current window.");
let buffer = tmux::capture_pane(&active_pane, &opt.capture_region)?; let buffer = tmux::capture_pane(&active_pane, &config.capture_region)?;
// We have to dance a little with Panes, because this process' i/o streams // We have to dance a little with Panes, because this process' i/o streams
// are connected to the pane in the window newly created for us, instead // are connected to the pane in the window newly created for us, instead
// of the active current pane. // of the active current pane.
let temp_pane_spec = format!("{}.0", opt.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, &opt.cli_options); let selection = copyrat::run(buffer, &config.cli_options);
tmux::swap_pane_with(&temp_pane_spec)?; tmux::swap_pane_with(&temp_pane_spec)?;
@ -59,7 +59,7 @@ fn main() -> Result<(), error::ParseError> {
} }
OutputDestination::Clipboard => { OutputDestination::Clipboard => {
duct::cmd!("echo", "-n", &text) duct::cmd!("echo", "-n", &text)
.pipe(duct::cmd!(opt.clipboard_exe)) .pipe(duct::cmd!(config.clipboard_exe))
.read()?; .read()?;
} }
} }

View file

@ -12,7 +12,7 @@ use crate::{
/// Main configuration, parsed from command line. /// Main configuration, parsed from command line.
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
#[clap(author, about, version)] #[clap(author, about, version)]
pub struct CliOpt { pub struct Config {
/// Alphabet to draw hints from. /// Alphabet to draw hints from.
/// ///
/// Possible values are "{A}", "{A}-homerow", "{A}-left-hand", /// Possible values are "{A}", "{A}-homerow", "{A}-left-hand",
@ -108,7 +108,7 @@ fn parse_chars(src: &str) -> Result<(char, char), error::ParseError> {
Ok((chars[0], chars[1])) Ok((chars[0], chars[1]))
} }
impl CliOpt { impl Config {
/// Try parsing provided options, and update self with the valid values. /// Try parsing provided options, and update self with the valid values.
pub fn merge_map( pub fn merge_map(
&mut self, &mut self,

View file

@ -1,5 +1,2 @@
mod basic; pub mod basic;
mod bridge; pub mod tmux_bridge;
pub use self::basic::{CliOpt, HintStyleArg};
pub use self::bridge::BridgeOpt;

View file

@ -2,14 +2,14 @@ use clap::Clap;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use super::CliOpt; use super::basic;
use crate::comm::tmux; use crate::comm::tmux;
use crate::error; use crate::error;
/// Main configuration, parsed from command line. /// Main configuration, parsed from command line.
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
#[clap(author, about, version)] #[clap(author, about, version)]
pub struct BridgeOpt { pub struct Config {
/// Don't read options from Tmux. /// Don't read options from Tmux.
/// ///
/// By default, options formatted like `copyrat-*` are read from tmux. /// By default, options formatted like `copyrat-*` are read from tmux.
@ -40,10 +40,10 @@ pub struct BridgeOpt {
// Include CLI Options // Include CLI Options
#[clap(flatten)] #[clap(flatten)]
pub cli_options: CliOpt, pub cli_options: basic::Config,
} }
impl BridgeOpt { impl 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(

View file

@ -9,7 +9,7 @@ pub mod ui;
/// # Note /// # Note
/// ///
/// Maybe the decision to take ownership of the buffer is a bit bold. /// Maybe the decision to take ownership of the buffer is a bit bold.
pub fn run(buffer: String, opt: &config::CliOpt) -> Option<ui::Selection> { pub fn run(buffer: String, opt: &config::basic::Config) -> Option<ui::Selection> {
let mut model = textbuf::Model::new( let mut model = textbuf::Model::new(
&buffer, &buffer,
&opt.alphabet, &opt.alphabet,
@ -22,10 +22,10 @@ pub fn run(buffer: String, opt: &config::CliOpt) -> Option<ui::Selection> {
let hint_style = match &opt.hint_style { let hint_style = match &opt.hint_style {
None => None, None => None,
Some(style) => match style { Some(style) => match style {
config::HintStyleArg::Bold => Some(ui::HintStyle::Bold), config::basic::HintStyleArg::Bold => Some(ui::HintStyle::Bold),
config::HintStyleArg::Italic => Some(ui::HintStyle::Italic), config::basic::HintStyleArg::Italic => Some(ui::HintStyle::Italic),
config::HintStyleArg::Underline => Some(ui::HintStyle::Underline), config::basic::HintStyleArg::Underline => Some(ui::HintStyle::Underline),
config::HintStyleArg::Surround => { config::basic::HintStyleArg::Surround => {
let (open, close) = opt.hint_surroundings; let (open, close) = opt.hint_surroundings;
Some(ui::HintStyle::Surround(open, close)) Some(ui::HintStyle::Surround(open, close))
} }