From 2241347bdecac675a92ce8621f9b96d4ea83ee71 Mon Sep 17 00:00:00 2001 From: graelo Date: Mon, 7 Nov 2022 14:01:29 +0100 Subject: [PATCH 1/3] chore(deps): update dependencies --- Cargo.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51e9148..d9388c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,9 +36,9 @@ checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574" [[package]] name = "clap" -version = "4.0.19" +version = "4.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e67816e006b17427c9b4386915109b494fec2d929c63e3bd3561234cbf1bf1e" +checksum = "426eed9136e68a14d9de937db20cfd79fcc25c09709872e8005897c618a8365e" dependencies = [ "atty", "bitflags", @@ -52,9 +52,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.0.18" +version = "4.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16a1b0f6422af32d5da0c58e2703320f379216ee70198241c84173a8c5ac28f3" +checksum = "685fbc59da060ed2cd3d79c86970ee95386b5e5fc69d9cad881912dca0c18807" dependencies = [ "heck", "proc-macro-error", From df661e25f15182e816e3eb1713773a3bfdc8bd44 Mon Sep 17 00:00:00 2001 From: graelo Date: Mon, 7 Nov 2022 14:10:33 +0100 Subject: [PATCH 2/3] chore(clap): use more idiomatic attributes --- src/config/extended.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/config/extended.rs b/src/config/extended.rs index fde3a63..fb78ee2 100644 --- a/src/config/extended.rs +++ b/src/config/extended.rs @@ -18,7 +18,7 @@ pub struct ConfigExt { /// 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(short = 'n', long)] + #[arg(short = 'n', long)] pub ignore_tmux_options: bool, /// Name of the copyrat temporary Tmux window. @@ -26,11 +26,11 @@ pub struct ConfigExt { /// 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(short = 'W', long, default_value = "[copyrat]")] + #[arg(short = 'W', long, default_value = "[copyrat]")] pub window_name: String, /// Capture visible area or entire pane history. - #[clap( + #[arg( value_enum, long, rename_all = "kebab-case", @@ -43,11 +43,11 @@ pub struct ConfigExt { /// If during execution, the output destination is set to be clipboard, /// then copyrat will pipe the selected text to this executable. /// On macOS, this is `pbcopy`, on Linux, this is `xclip`. - #[clap(long, default_value = "pbcopy")] + #[arg(long, default_value = "pbcopy")] pub clipboard_exe: String, // Include fields from the basic config - #[clap(flatten)] + #[command(flatten)] pub basic_config: basic::Config, } From 421c74b7920fe78c50ae575d85475c7458d23a45 Mon Sep 17 00:00:00 2001 From: graelo Date: Mon, 7 Nov 2022 14:40:49 +0100 Subject: [PATCH 3/3] feat(command): add init command --- src/bin/tmux_copyrat.rs | 21 +++++++++++++++++++-- src/config/extended.rs | 30 ++++++++++++++++++++---------- copyrat.tmux => tmux-copyrat.tmux | 31 +++++++++++++------------------ 3 files changed, 52 insertions(+), 30 deletions(-) rename copyrat.tmux => tmux-copyrat.tmux (85%) diff --git a/src/bin/tmux_copyrat.rs b/src/bin/tmux_copyrat.rs index a8fbaf0..6dbc1a1 100644 --- a/src/bin/tmux_copyrat.rs +++ b/src/bin/tmux_copyrat.rs @@ -1,13 +1,30 @@ +use clap::Parser; use copyrat::{ - config::extended::{ConfigExt, OutputDestination}, + config::extended::{ConfigExt, MainConfig, OutputDestination}, tmux, ui::Selection, Result, }; fn main() -> Result<()> { - let config = ConfigExt::initialize()?; + let main_config = MainConfig::parse(); + match main_config { + MainConfig::Init => init(), + MainConfig::Run { config_ext } => { + let config = config_ext.build()?; + run(config) + } + } +} + +fn init() -> Result<()> { + let text = std::include_str!("../../tmux-copyrat.tmux"); + println!("{text}"); + Ok(()) +} + +fn run(config: ConfigExt) -> Result<()> { // Identify active pane and capture its content. let panes: Vec = tmux::available_panes()?; diff --git a/src/config/extended.rs b/src/config/extended.rs index fb78ee2..4e95364 100644 --- a/src/config/extended.rs +++ b/src/config/extended.rs @@ -1,16 +1,27 @@ use std::collections::HashMap; use std::fmt; -use clap::{Parser, ValueEnum}; +use clap::{Args, Parser, ValueEnum}; use super::basic; use crate::{textbuf::alphabet, tmux, ui, Error, Result}; +#[derive(Parser, Debug)] +#[clap(author, about, version)] +pub enum MainConfig { + /// Run tmux-copyrat. + Run { + #[command(flatten)] + config_ext: ConfigExt, + }, + /// Print the tmux plugin config file for initial configuration. + Init, +} + /// Extended configuration for handling Tmux-specific configuration (options /// and outputs). This is only used by `tmux-copyrat` and parsed from command /// line. -#[derive(Parser, Debug)] -#[clap(author, about, version)] +#[derive(Args, Debug)] pub struct ConfigExt { /// Don't read options from Tmux. /// @@ -52,20 +63,19 @@ pub struct ConfigExt { } impl ConfigExt { - pub fn initialize() -> Result { - let mut config_ext = ConfigExt::parse(); - - if !config_ext.ignore_tmux_options { + /// Finalize the ConfigExt by merging the tmux options. + pub fn build(mut self) -> Result { + if !self.ignore_tmux_options { let tmux_options: HashMap = tmux::get_options("@copyrat-")?; // Override default values with those coming from tmux. - let inner = &mut config_ext.basic_config; + let inner = &mut self.basic_config; for (name, value) in &tmux_options { match name.as_ref() { "@copyrat-capture-region" => { let case_insensitive = true; - config_ext.capture_region = CaptureRegion::from_str(value, case_insensitive) + self.capture_region = CaptureRegion::from_str(value, case_insensitive) .map_err(Error::ExpectedEnumVariant)? } "@copyrat-alphabet" => { @@ -108,7 +118,7 @@ impl ConfigExt { } } - Ok(config_ext) + Ok(self) } } diff --git a/copyrat.tmux b/tmux-copyrat.tmux similarity index 85% rename from copyrat.tmux rename to tmux-copyrat.tmux index b2d1890..fbe5bcd 100755 --- a/copyrat.tmux +++ b/tmux-copyrat.tmux @@ -1,4 +1,4 @@ -#!/usr/bin/env zsh +#!/usr/bin/env bash # This scripts provides a default configuration for tmux-copyrat options and # key bindings. It is run only once at tmux launch. @@ -28,18 +28,19 @@ # You can also entirely ignore this file (not even source it) and define all # options and bindings in your `tmux.conf`. -CURRENT_DIR="$( cd "$( dirname "$0" )" && pwd )" -BINARY=${CURRENT_DIR}/tmux-copyrat +BINARY=$(which tmux-copyrat) +# CURRENT_DIR="$( cd "$( dirname "$0" )" && pwd )" +# BINARY=${CURRENT_DIR}/tmux-copyrat # # Top-level options # -setup_option() { - local opt_name=$1 - local default_value=$2 - local current_value=$(tmux show-option -gqv @copyrat-${opt_name}) +setup_option () { + opt_name=$1 + default_value=$2 + current_value=$(tmux show-option -gqv @copyrat-${opt_name}) value=${current_value:-${default_value}} tmux set-option -g @copyrat-${opt_name} ${value} } @@ -70,12 +71,12 @@ tmux bind-key ${keyswitch} switch-client -T ${keytable} # Pattern bindings # -setup_pattern_binding() { - local key=$1 - local pattern_arg="$2" +setup_pattern_binding () { + key=$1 + pattern_arg="$2" # The default window name `[copyrat]` has to be single quoted because it is # interpreted by the shell when launched by tmux. - tmux bind-key -T ${keytable} ${key} new-window -d -n ${window_name} "${BINARY} --window-name '"${window_name}"' --reverse --unique-hint ${pattern_arg}" + tmux bind-key -T ${keytable} ${key} new-window -d -n ${window_name} "${BINARY} run --window-name '"${window_name}"' --reverse --unique-hint ${pattern_arg}" } # prefix + t + c searches for hex colors #aa00f5 @@ -112,10 +113,4 @@ setup_pattern_binding "6" "--pattern-name ipv6" setup_pattern_binding "space" "--all-patterns" # prefix + t + / prompts for a pattern and search for it -tmux bind-key -T ${keytable} "/" command-prompt -p "search:" "new-window -d -n '${window_name}' \"${BINARY}\" --window-name '${window_name}' --reverse --unique-hint --custom-pattern %%" - - -# Auto-install is currently disabled as it requires the user to have cargo installed. -# if [ ! -f "$BINARY" ]; then -# cd "${CURRENT_DIR}" && cargo build --release -# fi +tmux bind-key -T ${keytable} "/" command-prompt -p "search:" "new-window -d -n '${window_name}' \"${BINARY}\" run --window-name '${window_name}' --reverse --unique-hint --custom-pattern %%"