Update clipboard error messages, to reflect support for xclip and xsel

This commit is contained in:
timvisee 2019-03-25 19:09:43 +01:00
parent 982027f33c
commit 5702367b84
No known key found for this signature in database
GPG key ID: B8DB720BC383E172

View file

@ -288,6 +288,12 @@ pub fn open_path(path: &str) -> Result<ExitStatus, IoError> {
open::that(path) open::that(path)
} }
/// Set the clipboard of the user to the given `content` string.
#[cfg(feature = "clipboard")]
pub fn set_clipboard(content: String) -> Result<(), ClipboardError> {
ClipboardType::select().set(content)
}
/// Clipboard management enum. /// Clipboard management enum.
/// ///
/// Defines which method of setting the clipboard is used. /// Defines which method of setting the clipboard is used.
@ -366,12 +372,13 @@ impl ClipboardType {
ClipboardProvider::new() ClipboardProvider::new()
.and_then(|mut context: ClipboardContext| context.set_contents(content)) .and_then(|mut context: ClipboardContext| context.set_contents(content))
.map_err(|err| format_err!("{}", err).compat()) .map_err(|err| format_err!("{}", err).compat())
.map_err(ClipboardError::Generic) .map_err(ClipboardError::Native)
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn xclip_set(path: Option<String>, content: String) -> Result<(), ClipboardError> { fn xclip_set(path: Option<String>, content: String) -> Result<(), ClipboardError> {
Self::sys_cmd_set( Self::sys_cmd_set(
"xclip",
Command::new(path.unwrap_or_else(|| "xclip".into())) Command::new(path.unwrap_or_else(|| "xclip".into()))
.arg("-sel") .arg("-sel")
.arg("clip"), .arg("clip"),
@ -382,20 +389,25 @@ impl ClipboardType {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn xsel_set(path: Option<String>, content: String) -> Result<(), ClipboardError> { fn xsel_set(path: Option<String>, content: String) -> Result<(), ClipboardError> {
Self::sys_cmd_set( Self::sys_cmd_set(
"xsel",
Command::new(path.unwrap_or_else(|| "xsel".into())).arg("--clipboard"), Command::new(path.unwrap_or_else(|| "xsel".into())).arg("--clipboard"),
content, content,
) )
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn sys_cmd_set(command: &mut Command, content: String) -> Result<(), ClipboardError> { fn sys_cmd_set(
bin: &'static str,
command: &mut Command,
content: String,
) -> Result<(), ClipboardError> {
// Spawn the command process for setting the clipboard // Spawn the command process for setting the clipboard
let mut process = match command.stdin(Stdio::piped()).spawn() { let mut process = match command.stdin(Stdio::piped()).spawn() {
Ok(process) => process, Ok(process) => process,
Err(err) => { Err(err) => {
return Err(match err.kind() { return Err(match err.kind() {
IoErrorKind::NotFound => ClipboardError::NoXclip, IoErrorKind::NotFound => ClipboardError::NoBinary,
_ => ClipboardError::Xclip(err), _ => ClipboardError::BinaryIo(bin, err),
}); });
} }
}; };
@ -406,12 +418,17 @@ impl ClipboardType {
.as_mut() .as_mut()
.unwrap() .unwrap()
.write_all(content.as_bytes()) .write_all(content.as_bytes())
.map_err(ClipboardError::Xclip)?; .map_err(|err| ClipboardError::BinaryIo(bin, err))?;
// Wait for xclip to exit // Wait for xclip to exit
let status = process.wait().map_err(ClipboardError::Xclip)?; let status = process
.wait()
.map_err(|err| ClipboardError::BinaryIo(bin, err))?;
if !status.success() { if !status.success() {
return Err(ClipboardError::XclipStatus(status.code().unwrap_or(0))); return Err(ClipboardError::BinaryStatus(
bin,
status.code().unwrap_or(0),
));
} }
Ok(()) Ok(())
@ -438,39 +455,35 @@ impl fmt::Display for ClipboardType {
} }
} }
/// Set the clipboard of the user to the given `content` string.
#[cfg(feature = "clipboard")]
pub fn set_clipboard(content: String) -> Result<(), ClipboardError> {
ClipboardType::select().set(content)
}
#[cfg(feature = "clipboard")] #[cfg(feature = "clipboard")]
#[derive(Debug, Fail)] #[derive(Debug, Fail)]
pub enum ClipboardError { pub enum ClipboardError {
/// A generic error occurred while setting the clipboard contents /// A generic error occurred while setting the clipboard contents.
///
/// This is for non-Linux systems, using a native clipboard interface.
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
#[fail(display = "failed to access clipboard")] #[fail(display = "failed to access clipboard")]
Generic(#[cause] Compat<Error>), Native(#[cause] Compat<Error>),
/// Xclip is not installed on the system, which is required for clipboard support. /// The `xclip` or `xsel` binary could not be found on the system, required for clipboard support.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[fail(display = "failed to access clipboard, xclip is not installed")] #[fail(display = "failed to access clipboard, xclip or xsel is not installed")]
NoXclip, NoBinary,
/// An error occurred while using xclip to set the clipboard contents. /// An error occurred while using `xclip` or `xsel` to set the clipboard contents.
/// This problem probably occurred when stargin the xclip process, or while piping the /// This problem probably occurred when starting, or while piping the clipboard contents to
/// clipboard contents to the process. /// the process.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[fail(display = "failed to access clipboard using xclip")] #[fail(display = "failed to access clipboard using {}", _0)]
Xclip(#[cause] IoError), BinaryIo(&'static str, #[cause] IoError),
/// Xclip unexpectetly exited with a non-successful status code. /// `xclip` or `xsel` unexpectetly exited with a non-successful status code.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[fail( #[fail(
display = "failed to use clipboard, xclip exited with status code {}", display = "failed to use clipboard, {} exited with status code {}",
_0 _0, _1
)] )]
XclipStatus(i32), BinaryStatus(&'static str, i32),
} }
/// Check for an emtpy password in the given `password`. /// Check for an emtpy password in the given `password`.