forked from PaulJuliusMartinez/jless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make clipboard support optional, with shell command escape hatch
Squashed and cleaned up version of PaulJuliusMartinez#121 from the upstream repo. PaulJuliusMartinez#121
- Loading branch information
Showing
5 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
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,59 @@ | ||
use std::fmt; | ||
use std::io::{self, Write}; | ||
use std::process::{Command, ExitStatus, Stdio}; | ||
|
||
#[cfg(feature = "clipboard")] | ||
use clipboard as sys_clipboard; | ||
#[cfg(feature = "clipboard")] | ||
use sys_clipboard::ClipboardProvider; | ||
|
||
pub enum ClipProvider { | ||
CommandClipboard(String), | ||
#[cfg(feature = "clipboard")] | ||
SystemClipboard(sys_clipboard::ClipboardContext), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ClipError(pub String); | ||
impl fmt::Display for ClipError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl ClipProvider { | ||
pub fn copy(&mut self, content: String) -> Result<(), ClipError> { | ||
match self { | ||
Self::CommandClipboard(shell_command) => { | ||
let status = send_content_to_shell_command(content, shell_command) | ||
.map_err(|err| ClipError(err.to_string()))?; | ||
match status.code() { | ||
Some(code) if !status.success() => Err(ClipError(std::format!( | ||
"Command failed with status code {}", | ||
code | ||
))), | ||
Some(_) => Ok(()), | ||
None => Err(ClipError("Command terminated by signal".to_string())), | ||
} | ||
} | ||
|
||
#[cfg(feature = "clipboard")] | ||
Self::SystemClipboard(context) => context | ||
.set_contents(content) | ||
.map_err(|err| ClipError(err.to_string())), | ||
} | ||
} | ||
} | ||
|
||
fn send_content_to_shell_command(content: String, shell_command: &str) -> io::Result<ExitStatus> { | ||
let mut child = Command::new("sh") | ||
.args(["-c", shell_command]) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn()?; | ||
let mut stdin = child.stdin.take().expect("Failed to grab stdin"); | ||
stdin.write_all(content.as_bytes())?; | ||
drop(stdin); | ||
child.wait() | ||
} |
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