Skip to content

Commit 0710211

Browse files
committed
Extract clipboard setting into a crossterm command
1 parent 8a5c873 commit 0710211

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

helix-view/src/clipboard.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use anyhow::Result;
44
use std::borrow::Cow;
55

6+
#[derive(Clone, Copy, Debug)]
67
pub enum ClipboardType {
78
Clipboard,
89
Selection,
@@ -145,6 +146,37 @@ mod provider {
145146
use anyhow::Result;
146147
use std::borrow::Cow;
147148

149+
#[cfg(feature = "term")]
150+
mod osc52 {
151+
use {super::ClipboardType, base64, crossterm};
152+
153+
#[derive(Debug)]
154+
pub struct SetClipboardCommand {
155+
encoded_content :String,
156+
clipboard_type :ClipboardType,
157+
}
158+
159+
impl SetClipboardCommand {
160+
pub fn new(content :&str, clipboard_type :ClipboardType) -> Self {
161+
Self {
162+
encoded_content: base64::encode(content),
163+
clipboard_type,
164+
}
165+
}
166+
}
167+
168+
impl crossterm::Command for SetClipboardCommand {
169+
fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
170+
let kind = match &self.clipboard_type {
171+
ClipboardType::Clipboard => "c",
172+
ClipboardType::Selection => "p",
173+
};
174+
// Send an OSC 52 set command: https://terminalguide.namepad.de/seq/osc-52/
175+
write!(f, "\x1b]52;{};{}\x1b\\", kind, &self.encoded_content)
176+
}
177+
}
178+
}
179+
148180
#[derive(Debug)]
149181
pub struct FallbackProvider {
150182
buf: String,
@@ -168,9 +200,6 @@ mod provider {
168200
}
169201
}
170202

171-
#[cfg(feature = "term")]
172-
use {base64, crossterm, std::io::stdout};
173-
174203
impl ClipboardProvider for FallbackProvider {
175204
#[cfg(feature = "term")]
176205
fn name(&self) -> Cow<str> {
@@ -182,6 +211,7 @@ mod provider {
182211
Cow::Borrowed("none")
183212
}
184213

214+
185215
fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
186216
// This is the same noop if term is enabled or not.
187217
// We don't use the get side of OSC 52 as it isn't often enabled, it's a security hole,
@@ -194,28 +224,10 @@ mod provider {
194224
Ok(value)
195225
}
196226

197-
#[cfg(feature = "term")]
198-
fn set_contents(&mut self, content: String, clipboard_type: ClipboardType) -> Result<()> {
199-
let encoded = base64::encode(&content);
200-
let kind = match clipboard_type {
201-
ClipboardType::Clipboard => {
202-
// Still set our internal variables to use in get_content
203-
self.buf = content;
204-
"c"
205-
}
206-
ClipboardType::Selection => {
207-
self.primary_buf = content;
208-
"p"
209-
}
210-
};
211-
// Send an OSC 52 set command: https://terminalguide.namepad.de/seq/osc-52/
212-
let cmd = crossterm::style::Print(format!("\x1b]52;{};{}\x1b\\", kind, encoded));
213-
crossterm::execute!(stdout(), cmd)?;
214-
Ok(())
215-
}
216-
217-
#[cfg(not(feature = "term"))]
218227
fn set_contents(&mut self, content: String, clipboard_type: ClipboardType) -> Result<()> {
228+
#[cfg(feature = "term")]
229+
crossterm::execute!(std::io::stdout(), osc52::SetClipboardCommand::new(&content, clipboard_type))?;
230+
// Set our internal variables to use in get_content regardless of using OSC 52
219231
match clipboard_type {
220232
ClipboardType::Clipboard => self.buf = content,
221233
ClipboardType::Selection => self.primary_buf = content,

0 commit comments

Comments
 (0)