refactor: refactor

This commit is contained in:
graelo 2020-05-29 16:23:46 +02:00
parent 01d6e62689
commit 1ea1af7bdd
4 changed files with 79 additions and 30 deletions

View file

@ -10,14 +10,18 @@ mod tmux;
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
#[clap(author, about, version)] #[clap(author, about, version)]
struct BridgeOpt { struct BridgeOpt {
/// Command to execute on selection. ///// Command to execute on selection.
#[clap(long, default_value = "tmux set-buffer {}")] //#[clap(long, default_value = "tmux set-buffer {}")]
command: String, //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 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. /// 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.
@ -52,12 +56,12 @@ impl BridgeOpt {
) -> Result<(), error::ParseError> { ) -> Result<(), error::ParseError> {
for (name, value) in options { for (name, value) in options {
match name.as_ref() { match name.as_ref() {
"@copyrat-command" => { // "@copyrat-command" => {
self.command = String::from(value); // self.command = String::from(value);
} // }
"@copyrat-alt-command" => { // "@copyrat-alt-command" => {
self.alt_command = String::from(value); // self.alt_command = String::from(value);
} // }
"@copyrat-capture" => { "@copyrat-capture" => {
self.capture_region = tmux::CaptureRegion::from_str(&value)?; self.capture_region = tmux::CaptureRegion::from_str(&value)?;
} }
@ -104,23 +108,44 @@ fn main() -> Result<(), error::ParseError> {
tmux::swap_pane_with(&temp_pane_spec)?; tmux::swap_pane_with(&temp_pane_spec)?;
// Execute a command on each selection.
// TODO: consider getting rid of multi-selection mode. // 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(); // Execute a command on each group of selections (normal and uppercased).
let command = it.next().unwrap(); let (normal_selections, uppercased_selections): (Vec<(String, bool)>, Vec<(String, bool)>) =
let args: Vec<&str> = it.collect(); 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 // Simply execute the command as is, and let the program crash on
// potential errors because it is not our responsibility. // 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(()) Ok(())
} }

View file

@ -24,6 +24,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Vec<(String, bool)> {
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 {
HintStyleCli::Bold => Some(view::HintStyle::Bold),
HintStyleCli::Italic => Some(view::HintStyle::Italic), HintStyleCli::Italic => Some(view::HintStyle::Italic),
HintStyleCli::Underline => Some(view::HintStyle::Underline), HintStyleCli::Underline => Some(view::HintStyle::Underline),
HintStyleCli::Surround => { HintStyleCli::Surround => {
@ -117,6 +118,7 @@ pub struct CliOpt {
/// as we cannot directly parse into view::HintStyle. /// as we cannot directly parse into view::HintStyle.
#[derive(Debug, Clap)] #[derive(Debug, Clap)]
enum HintStyleCli { enum HintStyleCli {
Bold,
Italic, Italic,
Underline, Underline,
Surround, Surround,

View file

@ -97,6 +97,12 @@ impl FromStr for PaneId {
} }
} }
impl PaneId {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for PaneId { impl fmt::Display for PaneId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0) write!(f, "{}", self.0)

View file

@ -83,6 +83,8 @@ impl FromStr for HintAlignment {
/// # Note /// # Note
/// In practice, this is wrapped in an `Option`, so that the hint's text can be rendered with no style. /// In practice, this is wrapped in an `Option`, so that the hint's text can be rendered with no style.
pub enum HintStyle { 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`). /// The hint's text will be italicized (leveraging `termion::style::Italic`).
Italic, Italic,
/// The hint's text will be underlined (leveraging `termion::style::Underline`). /// The hint's text will be underlined (leveraging `termion::style::Underline`).
@ -231,6 +233,21 @@ impl<'a> View<'a> {
.unwrap(); .unwrap();
} }
Some(hint_style) => match hint_style { 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 => { HintStyle::Italic => {
write!( write!(
stdout, stdout,
@ -433,11 +450,10 @@ impl<'a> View<'a> {
typed_hint.push_str(&lower_key); typed_hint.push_str(&lower_key);
// Find the match that corresponds to the entered key. // Find the match that corresponds to the entered key.
let selection = self let selection = self.matches
.matches .iter()
.iter() // Avoid cloning typed_hint for comparison.
// Avoid cloning typed_hint for comparison. .find(|&mat| mat.hint.as_deref().unwrap_or_default() == &typed_hint);
.find(|&mat| mat.hint.as_deref().unwrap_or_default() == &typed_hint);
match selection { match selection {
Some(mat) => { Some(mat) => {