-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support clipboard copy action (#24)
Now we can use key 'y' and 'Y' to copy selected item's name or value to system clipboard. This is based on external clipboard helper program in system: - MacOS: `pbcopy` - Linux X11: `xclip` - Linux Wayland: `wl-copy` - Windows: `clip` The other system does not support clipboard currently.
- Loading branch information
Showing
9 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::env; | ||
use std::io::{self, Write}; | ||
use std::process::{Command, Stdio}; | ||
|
||
use anyhow::{bail, Context, Result}; | ||
|
||
fn get_cmd() -> Result<Command> { | ||
let cmd = match env::consts::OS { | ||
"macos" => Command::new("pbcopy"), | ||
"linux" => { | ||
if env::var("WAYLAND_DISPLAY").is_ok() { | ||
Command::new("wl-copy") | ||
} else { | ||
let mut cmd = Command::new("xclip"); | ||
cmd.args(["-selection", "clipboard"]); | ||
cmd | ||
} | ||
} | ||
"windows" => Command::new("clip"), | ||
_ => bail!( | ||
"os {} does not support clipboard, you can create issue if you have requirement", | ||
env::consts::OS | ||
), | ||
}; | ||
Ok(cmd) | ||
} | ||
|
||
pub fn write_clipboard(text: &str) -> Result<()> { | ||
let mut cmd = get_cmd()?; | ||
cmd.stdin(Stdio::piped()); | ||
|
||
let mut child = match cmd.spawn() { | ||
Ok(child) => child, | ||
Err(err) if err.kind() == io::ErrorKind::NotFound => { | ||
let program = cmd.get_program().to_string_lossy(); | ||
bail!("cannot find clipboard program '{program}' in your system, please install it first to support clipboard") | ||
} | ||
Err(err) => return Err(err).context("launch clipboard program failed"), | ||
}; | ||
|
||
let stdin = child.stdin.as_mut().unwrap(); | ||
if let Err(err) = stdin.write_all(text.as_bytes()) { | ||
return Err(err).context("write text to clipboard program"); | ||
} | ||
drop(child.stdin.take()); | ||
|
||
let status = child.wait().context("wait clipboard program done")?; | ||
if !status.success() { | ||
let code = status | ||
.code() | ||
.map(|code| code.to_string()) | ||
.unwrap_or("<unknown>".to_string()); | ||
bail!("clipboard program exited with bad status {code}",); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod clipboard; | ||
mod cmd; | ||
mod config; | ||
mod edit; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters