feat(command): add init command

This commit is contained in:
graelo 2022-11-07 14:40:49 +01:00
parent df661e25f1
commit 421c74b792
3 changed files with 52 additions and 30 deletions

View file

@ -1,13 +1,30 @@
use clap::Parser;
use copyrat::{ use copyrat::{
config::extended::{ConfigExt, OutputDestination}, config::extended::{ConfigExt, MainConfig, OutputDestination},
tmux, tmux,
ui::Selection, ui::Selection,
Result, Result,
}; };
fn main() -> 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. // Identify active pane and capture its content.
let panes: Vec<tmux::Pane> = tmux::available_panes()?; let panes: Vec<tmux::Pane> = tmux::available_panes()?;

View file

@ -1,16 +1,27 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use clap::{Parser, ValueEnum}; use clap::{Args, Parser, ValueEnum};
use super::basic; use super::basic;
use crate::{textbuf::alphabet, tmux, ui, Error, Result}; 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 /// Extended configuration for handling Tmux-specific configuration (options
/// and outputs). This is only used by `tmux-copyrat` and parsed from command /// and outputs). This is only used by `tmux-copyrat` and parsed from command
/// line. /// line.
#[derive(Parser, Debug)] #[derive(Args, Debug)]
#[clap(author, about, version)]
pub struct ConfigExt { pub struct ConfigExt {
/// Don't read options from Tmux. /// Don't read options from Tmux.
/// ///
@ -52,20 +63,19 @@ pub struct ConfigExt {
} }
impl ConfigExt { impl ConfigExt {
pub fn initialize() -> Result<ConfigExt> { /// Finalize the ConfigExt by merging the tmux options.
let mut config_ext = ConfigExt::parse(); pub fn build(mut self) -> Result<ConfigExt> {
if !self.ignore_tmux_options {
if !config_ext.ignore_tmux_options {
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.
let inner = &mut config_ext.basic_config; let inner = &mut self.basic_config;
for (name, value) in &tmux_options { for (name, value) in &tmux_options {
match name.as_ref() { match name.as_ref() {
"@copyrat-capture-region" => { "@copyrat-capture-region" => {
let case_insensitive = true; 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)? .map_err(Error::ExpectedEnumVariant)?
} }
"@copyrat-alphabet" => { "@copyrat-alphabet" => {
@ -108,7 +118,7 @@ impl ConfigExt {
} }
} }
Ok(config_ext) Ok(self)
} }
} }

View file

@ -1,4 +1,4 @@
#!/usr/bin/env zsh #!/usr/bin/env bash
# This scripts provides a default configuration for tmux-copyrat options and # This scripts provides a default configuration for tmux-copyrat options and
# key bindings. It is run only once at tmux launch. # key bindings. It is run only once at tmux launch.
@ -28,8 +28,9 @@
# You can also entirely ignore this file (not even source it) and define all # You can also entirely ignore this file (not even source it) and define all
# options and bindings in your `tmux.conf`. # options and bindings in your `tmux.conf`.
CURRENT_DIR="$( cd "$( dirname "$0" )" && pwd )" BINARY=$(which tmux-copyrat)
BINARY=${CURRENT_DIR}/tmux-copyrat # CURRENT_DIR="$( cd "$( dirname "$0" )" && pwd )"
# BINARY=${CURRENT_DIR}/tmux-copyrat
# #
@ -37,9 +38,9 @@ BINARY=${CURRENT_DIR}/tmux-copyrat
# #
setup_option () { setup_option () {
local opt_name=$1 opt_name=$1
local default_value=$2 default_value=$2
local current_value=$(tmux show-option -gqv @copyrat-${opt_name}) current_value=$(tmux show-option -gqv @copyrat-${opt_name})
value=${current_value:-${default_value}} value=${current_value:-${default_value}}
tmux set-option -g @copyrat-${opt_name} ${value} tmux set-option -g @copyrat-${opt_name} ${value}
} }
@ -71,11 +72,11 @@ tmux bind-key ${keyswitch} switch-client -T ${keytable}
# #
setup_pattern_binding () { setup_pattern_binding () {
local key=$1 key=$1
local pattern_arg="$2" pattern_arg="$2"
# The default window name `[copyrat]` has to be single quoted because it is # The default window name `[copyrat]` has to be single quoted because it is
# interpreted by the shell when launched by tmux. # 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 # 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" setup_pattern_binding "space" "--all-patterns"
# prefix + t + / prompts for a pattern and search for it # 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 %%" 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 %%"
# 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