Skip to content

Commit

Permalink
Add support for copying snippet to clipboard (#334)
Browse files Browse the repository at this point in the history
Fixes #205
  • Loading branch information
denisidoro authored Apr 10, 2020
1 parent 72cf882 commit 1f5db34
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::structures::error::command::BashSpawnError;
use anyhow::Error;
use std::process::Command;

pub fn copy(text: String) -> Result<(), Error> {
let cmd = r#"
exst() {
type "$1" &>/dev/null
}
_paste() {
if exst pbcopy; then
pbcopy
elif exst xclip; then
xclip -selection clipboard
elif exst clip.exe; then
clip.exe
else
exit 55
fi
}
"#;

Command::new("bash")
.arg("-c")
.arg(format!(r#"{} echo "{}" | _paste"#, cmd, text).as_str())
.spawn()
.map_err(|e| BashSpawnError::new(cmd, e))?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/flows/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::clipboard;
use crate::display;
use crate::filesystem;
use crate::finder::Finder;
use crate::flows;
use crate::handler;
use crate::parser;
use crate::structures::cheat::{Suggestion, VariableMap};
Expand Down Expand Up @@ -204,7 +204,7 @@ pub fn main(variant: Variant, config: Config, contains_key: bool) -> Result<(),

// copy to clipboard
if key == "ctrl-y" {
flows::aux::abort("copying snippets to the clipboard", 201)?
clipboard::copy(interpolated_snippet)?;
// print to stdout
} else if config.print {
println!("{}", interpolated_snippet);
Expand Down
5 changes: 4 additions & 1 deletion src/flows/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ pub fn main(func: String, args: Vec<String>) -> Result<(), Error> {
.into_iter()
.next()
.ok_or_else(|| anyhow!("No URL specified"))?;
let cmd = format!("url=\"$(echo \"{}\" | tr ' ' '+')\"; (xdg-open \"$url\" 2> /dev/null || open \"$url\" 2> /dev/null) &disown", url);
let cmd = format!(
r#"url="$(echo "{}" | tr ' ' '+')"; (xdg-open "$url" 2> /dev/null || open "$url" 2> /dev/null) &disown"#,
url
);
Command::new("bash")
.arg("-c")
.arg(cmd.as_str())
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate lazy_static;
#[macro_use]
extern crate anyhow;

mod clipboard;
mod display;
mod filesystem;
mod finder;
Expand Down

0 comments on commit 1f5db34

Please sign in to comment.