mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 16:10:07 +01:00
refactor: refactor
This commit is contained in:
parent
01d6e62689
commit
1ea1af7bdd
4 changed files with 79 additions and 30 deletions
|
|
@ -10,14 +10,18 @@ mod tmux;
|
|||
#[derive(Clap, Debug)]
|
||||
#[clap(author, about, version)]
|
||||
struct BridgeOpt {
|
||||
/// Command to execute on selection.
|
||||
#[clap(long, default_value = "tmux set-buffer {}")]
|
||||
command: String,
|
||||
|
||||
/// Command to execute on uppercased selection.
|
||||
#[clap(long, default_value = "tmux set-buffer {} && tmux-paste-buffer")]
|
||||
alt_command: String,
|
||||
///// Command to execute on selection.
|
||||
//#[clap(long, default_value = "tmux set-buffer {}")]
|
||||
//command: String,
|
||||
|
||||
///// Command to execute on uppercased selection.
|
||||
/////
|
||||
///// This defaults to pasting in the original pane.
|
||||
//#[clap(
|
||||
// long,
|
||||
// default_value = "tmux set-buffer {} && tmux paste-buffer -t '#{active_pane}"
|
||||
//)]
|
||||
//alt_command: String,
|
||||
/// Don't read options from Tmux.
|
||||
///
|
||||
/// By default, options formatted like `copyrat-*` are read from tmux.
|
||||
|
|
@ -52,12 +56,12 @@ impl BridgeOpt {
|
|||
) -> Result<(), error::ParseError> {
|
||||
for (name, value) in options {
|
||||
match name.as_ref() {
|
||||
"@copyrat-command" => {
|
||||
self.command = String::from(value);
|
||||
}
|
||||
"@copyrat-alt-command" => {
|
||||
self.alt_command = String::from(value);
|
||||
}
|
||||
// "@copyrat-command" => {
|
||||
// self.command = String::from(value);
|
||||
// }
|
||||
// "@copyrat-alt-command" => {
|
||||
// self.alt_command = String::from(value);
|
||||
// }
|
||||
"@copyrat-capture" => {
|
||||
self.capture_region = tmux::CaptureRegion::from_str(&value)?;
|
||||
}
|
||||
|
|
@ -104,23 +108,44 @@ fn main() -> Result<(), error::ParseError> {
|
|||
|
||||
tmux::swap_pane_with(&temp_pane_spec)?;
|
||||
|
||||
// Execute a command on each selection.
|
||||
// TODO: consider getting rid of multi-selection mode.
|
||||
selections.iter().for_each(|(text, uppercased)| {
|
||||
let raw_command = if *uppercased {
|
||||
opt.alt_command.replace("{}", text)
|
||||
} else {
|
||||
opt.command.replace("{}", text)
|
||||
};
|
||||
|
||||
let mut it = raw_command.split(' ').into_iter();
|
||||
let command = it.next().unwrap();
|
||||
let args: Vec<&str> = it.collect();
|
||||
// Execute a command on each group of selections (normal and uppercased).
|
||||
let (normal_selections, uppercased_selections): (Vec<(String, bool)>, Vec<(String, bool)>) =
|
||||
selections
|
||||
.into_iter()
|
||||
.partition(|(_text, uppercased)| !*uppercased);
|
||||
|
||||
let buffer_selections: String = normal_selections
|
||||
.into_iter()
|
||||
.map(|(text, _)| text)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
if buffer_selections.len() > 0 {
|
||||
let args = vec!["set-buffer", &buffer_selections];
|
||||
// Simply execute the command as is, and let the program crash on
|
||||
// potential errors because it is not our responsibility.
|
||||
process::execute(&command, &args).unwrap();
|
||||
});
|
||||
process::execute("tmux", &args).unwrap();
|
||||
}
|
||||
|
||||
let buffer_selections: String = uppercased_selections
|
||||
.into_iter()
|
||||
.map(|(text, _)| text)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
if buffer_selections.len() > 0 {
|
||||
let args = vec!["set-buffer", &buffer_selections];
|
||||
// Simply execute the command as is, and let the program crash on
|
||||
// potential errors because it is not our responsibility.
|
||||
process::execute("tmux", &args).unwrap();
|
||||
|
||||
let args = vec!["paste-buffer", "-t", active_pane.id.as_str()];
|
||||
// Simply execute the command as is, and let the program crash on
|
||||
// potential errors because it is not our responsibility.
|
||||
process::execute("tmux", &args).unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Vec<(String, bool)> {
|
|||
let hint_style = match &opt.hint_style {
|
||||
None => None,
|
||||
Some(style) => match style {
|
||||
HintStyleCli::Bold => Some(view::HintStyle::Bold),
|
||||
HintStyleCli::Italic => Some(view::HintStyle::Italic),
|
||||
HintStyleCli::Underline => Some(view::HintStyle::Underline),
|
||||
HintStyleCli::Surround => {
|
||||
|
|
@ -117,6 +118,7 @@ pub struct CliOpt {
|
|||
/// as we cannot directly parse into view::HintStyle.
|
||||
#[derive(Debug, Clap)]
|
||||
enum HintStyleCli {
|
||||
Bold,
|
||||
Italic,
|
||||
Underline,
|
||||
Surround,
|
||||
|
|
|
|||
|
|
@ -97,6 +97,12 @@ impl FromStr for PaneId {
|
|||
}
|
||||
}
|
||||
|
||||
impl PaneId {
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PaneId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
|
|
|
|||
26
src/view.rs
26
src/view.rs
|
|
@ -83,6 +83,8 @@ impl FromStr for HintAlignment {
|
|||
/// # Note
|
||||
/// In practice, this is wrapped in an `Option`, so that the hint's text can be rendered with no style.
|
||||
pub enum HintStyle {
|
||||
/// The hint's text will be bold (leveraging `termion::style::Bold`).
|
||||
Bold,
|
||||
/// The hint's text will be italicized (leveraging `termion::style::Italic`).
|
||||
Italic,
|
||||
/// The hint's text will be underlined (leveraging `termion::style::Underline`).
|
||||
|
|
@ -231,6 +233,21 @@ impl<'a> View<'a> {
|
|||
.unwrap();
|
||||
}
|
||||
Some(hint_style) => match hint_style {
|
||||
HintStyle::Bold => {
|
||||
write!(
|
||||
stdout,
|
||||
"{goto}{bg_color}{fg_color}{sty}{hint}{sty_reset}{fg_reset}{bg_reset}",
|
||||
goto = cursor::Goto(offset.0 as u16 + 1, offset.1 as u16 + 1),
|
||||
fg_color = fg_color,
|
||||
bg_color = bg_color,
|
||||
fg_reset = fg_reset,
|
||||
bg_reset = bg_reset,
|
||||
sty = style::Bold,
|
||||
sty_reset = style::NoBold,
|
||||
hint = hint_text,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
HintStyle::Italic => {
|
||||
write!(
|
||||
stdout,
|
||||
|
|
@ -433,11 +450,10 @@ impl<'a> View<'a> {
|
|||
typed_hint.push_str(&lower_key);
|
||||
|
||||
// Find the match that corresponds to the entered key.
|
||||
let selection = self
|
||||
.matches
|
||||
.iter()
|
||||
// Avoid cloning typed_hint for comparison.
|
||||
.find(|&mat| mat.hint.as_deref().unwrap_or_default() == &typed_hint);
|
||||
let selection = self.matches
|
||||
.iter()
|
||||
// Avoid cloning typed_hint for comparison.
|
||||
.find(|&mat| mat.hint.as_deref().unwrap_or_default() == &typed_hint);
|
||||
|
||||
match selection {
|
||||
Some(mat) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue