use std::error::Error as StdError;
use std::fmt;
use base64::engine::Engine;
use crate::combined::CombinedClipboardContext;
use crate::display::DisplayServer;
use crate::prelude::*;
pub type ClipboardContext = Osc52ClipboardContext;
pub struct Osc52ClipboardContext;
impl Osc52ClipboardContext {
pub fn new() -> Result<Self, Box<dyn StdError>> {
Ok(Self)
}
pub fn new_with<G>(get: G) -> Result<CombinedClipboardContext<G, Self>, Box<dyn StdError>>
where
G: ClipboardProvider,
{
Self::new()?.with(get)
}
pub fn with<G>(self, get: G) -> Result<CombinedClipboardContext<G, Self>, Box<dyn StdError>>
where
G: ClipboardProvider,
{
Ok(CombinedClipboardContext(get, self))
}
}
impl ClipboardProvider for Osc52ClipboardContext {
fn get_contents(&mut self) -> crate::ClipResult<String> {
Err(Error::Unsupported.into())
}
fn set_contents(&mut self, contents: String) -> crate::ClipResult<()> {
print!(
"\x1B]52;c;{}\x07",
base64::engine::general_purpose::STANDARD.encode(&contents)
);
Ok(())
}
}
impl ClipboardProviderExt for Osc52ClipboardContext {
fn display_server(&self) -> Option<DisplayServer> {
Some(DisplayServer::Tty)
}
fn has_bin_lifetime(&self) -> bool {
false
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Unsupported,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Unsupported => write!(
f,
"Getting clipboard contents is not supported through this context"
),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
None
}
}