Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix termcode yank/paste #12142

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::sync::{Arc, Mutex};
use thiserror::Error;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -97,7 +98,7 @@ mod external {
Windows,
Termux,
#[cfg(feature = "term")]
Termcode,
Termcode(TermcodeProvider),
Custom(CommandProvider),
None,
}
Expand Down Expand Up @@ -163,7 +164,7 @@ mod external {
} else if binary_exists("win32yank.exe") {
Self::Win32Yank
} else if cfg!(feature = "term") {
Self::Termcode
Self::Termcode(TermcodeProvider::default())
} else {
Self::None
}
Expand Down Expand Up @@ -198,7 +199,7 @@ mod external {
#[cfg(windows)]
Self::Windows => "windows".into(),
#[cfg(feature = "term")]
Self::Termcode => "termcode".into(),
Self::Termcode(_) => "termcode".into(),
Self::Custom(command_provider) => Cow::Owned(format!(
"custom ({}+{})",
command_provider.yank.command, command_provider.paste.command
Expand Down Expand Up @@ -244,7 +245,7 @@ mod external {
ClipboardType::Selection => Ok(String::new()),
},
#[cfg(feature = "term")]
Self::Termcode => Err(ClipboardError::ReadingNotSupported),
Self::Termcode(provider) => provider.get_contents(clipboard_type),
Self::Custom(command_provider) => {
execute_command(&command_provider.yank, None, true)?
.ok_or(ClipboardError::MissingStdout)
Expand Down Expand Up @@ -290,12 +291,8 @@ mod external {
ClipboardType::Selection => Ok(()),
},
#[cfg(feature = "term")]
Self::Termcode => {
crossterm::queue!(
std::io::stdout(),
osc52::SetClipboardCommand::new(content, clipboard_type)
)?;
Ok(())
Self::Termcode(provider) => {
provider.set_contents(content.to_owned(), &clipboard_type)
}
Self::Custom(command_provider) => match clipboard_type {
ClipboardType::Clipboard => {
Expand All @@ -314,6 +311,54 @@ mod external {
}
}

#[cfg(feature = "term")]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct TermcodeProvider {
#[serde(skip)]
buf: Arc<Mutex<String>>,
#[serde(skip)]
primary_buf: Arc<Mutex<String>>,
}

#[cfg(feature = "term")]
impl PartialEq for TermcodeProvider {
fn eq(&self, _other: &Self) -> bool {
true
}
}

#[cfg(feature = "term")]
impl Eq for TermcodeProvider {}

#[cfg(feature = "term")]
impl TermcodeProvider {
fn get_contents(&self, clipboard_type: &ClipboardType) -> Result<String> {
// This is the same noop if term is enabled or not.
// We don't use the get side of OSC 52 as it isn't often enabled, it's a security hole,
// and it would require this to be async to listen for the response
let value = match clipboard_type {
ClipboardType::Clipboard => self.buf.lock().unwrap().clone(),
ClipboardType::Selection => self.primary_buf.lock().unwrap().clone(),
};

Ok(value)
}

fn set_contents(&self, content: String, clipboard_type: &ClipboardType) -> Result<()> {
#[cfg(feature = "term")]
crossterm::execute!(
std::io::stdout(),
osc52::SetClipboardCommand::new(&content, *clipboard_type)
)?;
// Set our internal variables to use in get_content regardless of using OSC 52
match clipboard_type {
ClipboardType::Clipboard => *self.buf.lock().unwrap() = content,
ClipboardType::Selection => *self.primary_buf.lock().unwrap() = content,
}
Ok(())
}
}

macro_rules! command_provider {
($name:ident,
yank => $yank_cmd:literal $( , $yank_arg:literal )* ;
Expand Down