From 20aa45c8781f1954de90bb23070454a9581e6969 Mon Sep 17 00:00:00 2001 From: Geert Jansen Date: Tue, 2 Jan 2018 11:32:50 -0500 Subject: Add support for set-clipboard. (#970) This allows e.g. tmux to set the clipboard via the OSC 52 escape code. --- src/ansi.rs | 23 ++++++++++++++++++++++- src/lib.rs | 1 + src/term/mod.rs | 12 ++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ansi.rs b/src/ansi.rs index 3d66b497..8ff7d06b 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -18,7 +18,7 @@ use std::ops::Range; use std::str; use vte; - +use base64; use index::{Column, Line, Contains}; use ::{MouseCursor, Rgb}; @@ -342,6 +342,9 @@ pub trait Handler { /// Reset an indexed color to original value fn reset_color(&mut self, usize) {} + /// Set the clipboard + fn set_clipboard(&mut self, &str) {} + /// Run the dectest routine fn dectest(&mut self) {} } @@ -820,6 +823,24 @@ impl<'a, H, W> vte::Perform for Performer<'a, H, W> } } + // Set clipboard + b"52" => { + if params.len() < 3 { + return unhandled!(); + } + + match params[2] { + b"?" => unhandled!(), + selection => { + if let Ok(string) = base64::decode(selection) { + if let Ok(utf8_string) = str::from_utf8(&string) { + self.handler.set_clipboard(utf8_string); + } + } + } + } + } + // Reset color index b"104" => { // Reset all color indexes when no parameters are given diff --git a/src/lib.rs b/src/lib.rs index 1f3fcc96..cb0abb14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,7 @@ extern crate serde_yaml; extern crate unicode_width; extern crate vte; extern crate xdg; +extern crate base64; #[macro_use] pub mod macros; diff --git a/src/term/mod.rs b/src/term/mod.rs index 72d97ed9..e43ea7b6 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -29,6 +29,7 @@ use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclus use selection::{self, Span, Selection}; use config::{Config, VisualBellAnimation}; use {MouseCursor, Rgb}; +use copypasta::{Clipboard, Load, Store}; pub mod cell; pub mod color; @@ -1673,6 +1674,17 @@ impl ansi::Handler for Term { self.color_modified[index] = false; } + /// Set the clipboard + #[inline] + fn set_clipboard(&mut self, string: &str) + { + Clipboard::new() + .and_then(|mut clipboard| clipboard.store_primary(string)) + .unwrap_or_else(|err| { + warn!("Error storing selection to clipboard. {}", err); + }); + } + #[inline] fn clear_screen(&mut self, mode: ansi::ClearMode) { trace!("clear_screen: {:?}", mode); -- cgit