diff options
author | Kirill Chibisov <contact@kchibisov.com> | 2020-06-07 00:33:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-07 00:33:20 +0300 |
commit | 4f5f8412966626771043bd3a90800124fed44ebe (patch) | |
tree | 264e8ffa917324b5e30b7532fcf7d773382012c3 /alacritty_terminal/src | |
parent | 7aafbb757d485c5ff065324464dde8b5322cdd92 (diff) | |
download | alacritty-4f5f8412966626771043bd3a90800124fed44ebe.tar.gz alacritty-4f5f8412966626771043bd3a90800124fed44ebe.zip |
Remove copypasta dependency from alacritty_terminal
Diffstat (limited to 'alacritty_terminal/src')
-rw-r--r-- | alacritty_terminal/src/ansi.rs | 12 | ||||
-rw-r--r-- | alacritty_terminal/src/clipboard.rs | 98 | ||||
-rw-r--r-- | alacritty_terminal/src/event.rs | 25 | ||||
-rw-r--r-- | alacritty_terminal/src/lib.rs | 1 | ||||
-rw-r--r-- | alacritty_terminal/src/selection.rs | 3 | ||||
-rw-r--r-- | alacritty_terminal/src/term/mod.rs | 73 | ||||
-rw-r--r-- | alacritty_terminal/src/vi_mode.rs | 3 |
7 files changed, 65 insertions, 150 deletions
diff --git a/alacritty_terminal/src/ansi.rs b/alacritty_terminal/src/ansi.rs index fe0cc162..619b1d39 100644 --- a/alacritty_terminal/src/ansi.rs +++ b/alacritty_terminal/src/ansi.rs @@ -307,11 +307,11 @@ pub trait Handler { /// Reset an indexed color to original value. fn reset_color(&mut self, _: usize) {} - /// Set the clipboard. - fn set_clipboard(&mut self, _: u8, _: &[u8]) {} + /// Store data into clipboard. + fn clipboard_store(&mut self, _: u8, _: &[u8]) {} - /// Write clipboard data to child. - fn write_clipboard<W: io::Write>(&mut self, _: u8, _: &mut W, _: &str) {} + /// Load data from clipboard. + fn clipboard_load(&mut self, _: u8, _: &str) {} /// Run the decaln routine. fn decaln(&mut self) {} @@ -892,8 +892,8 @@ where let clipboard = params[1].get(0).unwrap_or(&b'c'); match params[2] { - b"?" => self.handler.write_clipboard(*clipboard, writer, terminator), - base64 => self.handler.set_clipboard(*clipboard, base64), + b"?" => self.handler.clipboard_load(*clipboard, terminator), + base64 => self.handler.clipboard_store(*clipboard, base64), } }, diff --git a/alacritty_terminal/src/clipboard.rs b/alacritty_terminal/src/clipboard.rs deleted file mode 100644 index 8a045bf7..00000000 --- a/alacritty_terminal/src/clipboard.rs +++ /dev/null @@ -1,98 +0,0 @@ -#[cfg(not(any(target_os = "macos", target_os = "windows")))] -use std::ffi::c_void; - -use log::{debug, warn}; - -use copypasta::nop_clipboard::NopClipboardContext; -#[cfg(all(not(any(target_os = "macos", windows)), feature = "wayland"))] -use copypasta::wayland_clipboard; -#[cfg(all(not(any(target_os = "macos", windows)), feature = "x11"))] -use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext}; -#[cfg(any(feature = "x11", target_os = "macos", windows))] -use copypasta::ClipboardContext; -use copypasta::ClipboardProvider; - -pub struct Clipboard { - clipboard: Box<dyn ClipboardProvider>, - selection: Option<Box<dyn ClipboardProvider>>, -} - -impl Clipboard { - #[cfg(any(target_os = "macos", windows))] - pub fn new() -> Self { - Self::default() - } - - #[cfg(not(any(target_os = "macos", windows)))] - pub fn new(_display: Option<*mut c_void>) -> Self { - #[cfg(feature = "wayland")] - { - if let Some(display) = _display { - let (selection, clipboard) = - unsafe { wayland_clipboard::create_clipboards_from_external(display) }; - return Self { - clipboard: Box::new(clipboard), - selection: Some(Box::new(selection)), - }; - } - } - - #[cfg(feature = "x11")] - return Self { - clipboard: Box::new(ClipboardContext::new().unwrap()), - selection: Some(Box::new(X11ClipboardContext::<X11SelectionClipboard>::new().unwrap())), - }; - - #[cfg(not(feature = "x11"))] - return Self::new_nop(); - } - - // Use for tests and ref-tests. - pub fn new_nop() -> Self { - Self { clipboard: Box::new(NopClipboardContext::new().unwrap()), selection: None } - } -} - -impl Default for Clipboard { - fn default() -> Self { - #[cfg(any(feature = "x11", target_os = "macos", windows))] - return Self { clipboard: Box::new(ClipboardContext::new().unwrap()), selection: None }; - #[cfg(not(any(feature = "x11", target_os = "macos", windows)))] - return Self::new_nop(); - } -} - -#[derive(Debug)] -pub enum ClipboardType { - Clipboard, - Selection, -} - -impl Clipboard { - pub fn store(&mut self, ty: ClipboardType, text: impl Into<String>) { - let clipboard = match (ty, &mut self.selection) { - (ClipboardType::Selection, Some(provider)) => provider, - (ClipboardType::Selection, None) => return, - _ => &mut self.clipboard, - }; - - clipboard.set_contents(text.into()).unwrap_or_else(|err| { - warn!("Unable to store text in clipboard: {}", err); - }); - } - - pub fn load(&mut self, ty: ClipboardType) -> String { - let clipboard = match (ty, &mut self.selection) { - (ClipboardType::Selection, Some(provider)) => provider, - _ => &mut self.clipboard, - }; - - match clipboard.get_contents() { - Err(err) => { - debug!("Unable to load text from clipboard: {}", err); - String::new() - }, - Ok(text) => text, - } - } -} diff --git a/alacritty_terminal/src/event.rs b/alacritty_terminal/src/event.rs index 68a3282e..20b105cd 100644 --- a/alacritty_terminal/src/event.rs +++ b/alacritty_terminal/src/event.rs @@ -1,21 +1,42 @@ use std::borrow::Cow; +use std::fmt::{self, Debug, Formatter}; use std::path::PathBuf; +use std::sync::Arc; use crate::message_bar::Message; -use crate::term::SizeInfo; +use crate::term::{ClipboardType, SizeInfo}; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone)] pub enum Event { DPRChanged(f64, (u32, u32)), ConfigReload(PathBuf), MouseCursorDirty, Message(Message), Title(String), + ClipboardStore(ClipboardType, String), + ClipboardLoad(ClipboardType, Arc<dyn Fn(&str) -> String + Sync + Send + 'static>), Wakeup, Urgent, Exit, } +impl Debug for Event { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Event::DPRChanged(scale, size) => write!(f, "DPRChanged({}, {:?})", scale, size), + Event::ConfigReload(path) => write!(f, "ConfigReload({:?})", path), + Event::MouseCursorDirty => write!(f, "MouseCursorDirty"), + Event::Message(msg) => write!(f, "Message({:?})", msg), + Event::Title(title) => write!(f, "Title({})", title), + Event::ClipboardStore(ty, text) => write!(f, "ClipboardStore({:?}, {})", ty, text), + Event::ClipboardLoad(ty, _) => write!(f, "ClipboardLoad({:?})", ty), + Event::Wakeup => write!(f, "Wakeup"), + Event::Urgent => write!(f, "Urgent"), + Event::Exit => write!(f, "Exit"), + } + } +} + /// Byte sequences are sent to a `Notify` in response to some events. pub trait Notify { /// Notify that an escape sequence should be written to the PTY. diff --git a/alacritty_terminal/src/lib.rs b/alacritty_terminal/src/lib.rs index 6c847588..1d2fe551 100644 --- a/alacritty_terminal/src/lib.rs +++ b/alacritty_terminal/src/lib.rs @@ -9,7 +9,6 @@ extern crate objc; pub mod ansi; -pub mod clipboard; pub mod config; pub mod event; pub mod event_loop; diff --git a/alacritty_terminal/src/selection.rs b/alacritty_terminal/src/selection.rs index b1fd147c..7e1e108e 100644 --- a/alacritty_terminal/src/selection.rs +++ b/alacritty_terminal/src/selection.rs @@ -372,7 +372,6 @@ impl Selection { mod tests { use super::*; - use crate::clipboard::Clipboard; use crate::config::MockConfig; use crate::event::{Event, EventListener}; use crate::index::{Column, Line, Point, Side}; @@ -393,7 +392,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock) + Term::new(&MockConfig::default(), &size, Mock) } /// Test case of single cell selection. diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs index 9bb7488c..6aac05d9 100644 --- a/alacritty_terminal/src/term/mod.rs +++ b/alacritty_terminal/src/term/mod.rs @@ -2,6 +2,7 @@ use std::cmp::{max, min}; use std::ops::{Index, IndexMut, Range}; +use std::sync::Arc; use std::time::{Duration, Instant}; use std::{io, mem, ptr, str}; @@ -12,7 +13,6 @@ use unicode_width::UnicodeWidthChar; use crate::ansi::{ self, Attr, CharsetIndex, Color, CursorStyle, Handler, NamedColor, StandardCharset, TermInfo, }; -use crate::clipboard::{Clipboard, ClipboardType}; use crate::config::{Config, VisualBellAnimation}; use crate::event::{Event, EventListener}; use crate::grid::{ @@ -777,9 +777,6 @@ pub struct Term<T> { /// Style of the vi mode cursor. vi_mode_cursor_style: Option<CursorStyle>, - /// Clipboard access coupled to the active window. - clipboard: Clipboard, - /// Proxy for sending events to the event loop. event_proxy: T, @@ -808,12 +805,7 @@ impl<T> Term<T> { self.dirty = true; } - pub fn new<C>( - config: &Config<C>, - size: &SizeInfo, - clipboard: Clipboard, - event_proxy: T, - ) -> Term<T> { + pub fn new<C>(config: &Config<C>, size: &SizeInfo, event_proxy: T) -> Term<T> { let num_cols = size.cols(); let num_lines = size.lines(); @@ -847,7 +839,6 @@ impl<T> Term<T> { default_cursor_style: config.cursor.style, vi_mode_cursor_style: config.cursor.vi_mode_style, dynamic_title: config.dynamic_title(), - clipboard, event_proxy, is_focused: true, title: None, @@ -1153,11 +1144,6 @@ impl<T> Term<T> { self.event_proxy.send_event(Event::Exit); } - #[inline] - pub fn clipboard(&mut self) -> &mut Clipboard { - &mut self.clipboard - } - /// Toggle the vi mode. #[inline] pub fn toggle_vi_mode(&mut self) { @@ -1802,9 +1788,9 @@ impl<T: EventListener> Handler for Term<T> { self.color_modified[index] = false; } - /// Set the clipboard. + /// Store data into clipboard. #[inline] - fn set_clipboard(&mut self, clipboard: u8, base64: &[u8]) { + fn clipboard_store(&mut self, clipboard: u8, base64: &[u8]) { let clipboard_type = match clipboard { b'c' => ClipboardType::Clipboard, b'p' | b's' => ClipboardType::Selection, @@ -1812,25 +1798,30 @@ impl<T: EventListener> Handler for Term<T> { }; if let Ok(bytes) = base64::decode(base64) { - if let Ok(text) = str::from_utf8(&bytes) { - self.clipboard.store(clipboard_type, text); + if let Ok(text) = String::from_utf8(bytes) { + self.event_proxy.send_event(Event::ClipboardStore(clipboard_type, text)); } } } - /// Write clipboard data to child. + /// Load data from clipboard. #[inline] - fn write_clipboard<W: io::Write>(&mut self, clipboard: u8, writer: &mut W, terminator: &str) { + fn clipboard_load(&mut self, clipboard: u8, terminator: &str) { let clipboard_type = match clipboard { b'c' => ClipboardType::Clipboard, b'p' | b's' => ClipboardType::Selection, _ => return, }; - let text = self.clipboard.load(clipboard_type); - let base64 = base64::encode(&text); - let escape = format!("\x1b]52;{};{}{}", clipboard as char, base64, terminator); - let _ = writer.write_all(escape.as_bytes()); + let terminator = terminator.to_owned(); + + self.event_proxy.send_event(Event::ClipboardLoad( + clipboard_type, + Arc::new(move |text| { + let base64 = base64::encode(&text); + format!("\x1b]52;{};{}{}", clipboard as char, base64, terminator) + }), + )); } #[inline] @@ -2140,6 +2131,12 @@ impl<T: EventListener> Handler for Term<T> { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ClipboardType { + Clipboard, + Selection, +} + struct TabStops { tabs: Vec<bool>, } @@ -2195,7 +2192,6 @@ mod tests { use std::mem; use crate::ansi::{self, CharsetIndex, Handler, StandardCharset}; - use crate::clipboard::Clipboard; use crate::config::MockConfig; use crate::event::{Event, EventListener}; use crate::grid::{Grid, Scroll}; @@ -2219,7 +2215,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); let mut grid: Grid<Cell> = Grid::new(Line(3), Column(5), 0, Cell::default()); for i in 0..5 { for j in 0..2 { @@ -2275,7 +2271,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); let mut grid: Grid<Cell> = Grid::new(Line(1), Column(5), 0, Cell::default()); for i in 0..5 { grid[Line(0)][Column(i)].c = 'a'; @@ -2304,7 +2300,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); let mut grid: Grid<Cell> = Grid::new(Line(3), Column(3), 0, Cell::default()); for l in 0..3 { if l != 1 { @@ -2349,7 +2345,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); let cursor = Point::new(Line(0), Column(0)); term.configure_charset(CharsetIndex::G0, StandardCharset::SpecialCharacterAndLineDrawing); term.input('a'); @@ -2368,7 +2364,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Add one line of scrollback. term.grid.scroll_up(&(Line(0)..Line(1)), Line(1), Cell::default()); @@ -2398,7 +2394,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Create 10 lines of scrollback. for _ in 0..19 { @@ -2426,7 +2422,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Create 10 lines of scrollback. for _ in 0..19 { @@ -2460,7 +2456,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Create 10 lines of scrollback. for _ in 0..19 { @@ -2488,7 +2484,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Create 10 lines of scrollback. for _ in 0..19 { @@ -2522,7 +2518,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - let mut term = Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock); + let mut term = Term::new(&MockConfig::default(), &size, Mock); // Title None by default. assert_eq!(term.title, None); @@ -2576,7 +2572,6 @@ mod benches { use std::fs; use std::mem; - use crate::clipboard::Clipboard; use crate::config::MockConfig; use crate::event::{Event, EventListener}; use crate::grid::Grid; @@ -2617,7 +2612,7 @@ mod benches { let config = MockConfig::default(); - let mut terminal = Term::new(&config, &size, Clipboard::new_nop(), Mock); + let mut terminal = Term::new(&config, &size, Mock); mem::swap(&mut terminal.grid, &mut grid); b.iter(|| { diff --git a/alacritty_terminal/src/vi_mode.rs b/alacritty_terminal/src/vi_mode.rs index 650c3a8a..6621eda5 100644 --- a/alacritty_terminal/src/vi_mode.rs +++ b/alacritty_terminal/src/vi_mode.rs @@ -412,7 +412,6 @@ fn is_boundary<T>(term: &Term<T>, point: Point<usize>, left: bool) -> bool { mod tests { use super::*; - use crate::clipboard::Clipboard; use crate::config::MockConfig; use crate::event::Event; use crate::index::{Column, Line}; @@ -433,7 +432,7 @@ mod tests { padding_y: 0.0, dpr: 1.0, }; - Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock) + Term::new(&MockConfig::default(), &size, Mock) } #[test] |