diff --git a/src/input.rs b/src/input.rs index 960bc017..012163b8 100644 --- a/src/input.rs +++ b/src/input.rs @@ -25,12 +25,12 @@ use glutin::{ElementState, VirtualKeyCode, MouseButton}; use glutin::{Mods, mods}; use glutin::{TouchPhase, MouseScrollDelta}; -use event::Notify; +use event::{Mouse, Notify}; use index::{Line, Column, Side, Point}; use selection::Selection; use term::mode::{self, TermMode}; use term::{self, Term}; -use event::Mouse; +use util::fmt::Red; /// Processes input from glutin. /// @@ -149,16 +149,17 @@ impl Action { let buf = ctx.terminal.string_from_selection(&selection); if !buf.is_empty() { Clipboard::new() - .expect("get clipboard") - .store_primary(buf) - .expect("copy into clipboard"); + .and_then(|mut clipboard| clipboard.store_primary(buf)) + .unwrap_or_else(|err| { + err_println!("Error storing selection to clipboard: {}", Red(err)); + }); } } }, Action::Paste | Action::PasteSelection => { - let clip = Clipboard::new().expect("get clipboard"); - clip.load_selection() + Clipboard::new() + .and_then(|clipboard| clipboard.load_selection()) .map(|contents| { if ctx.terminal.mode().contains(mode::BRACKETED_PASTE) { ctx.notifier.notify(&b"\x1b[200~"[..]); @@ -169,7 +170,7 @@ impl Action { } }) .unwrap_or_else(|err| { - err_println!("Error getting clipboard contents: {}", err); + err_println!("Error loading data from clipboard {}", Red(err)); }); }, } @@ -275,9 +276,10 @@ impl<'a, N: Notify + 'a> Processor<'a, N> { let buf = self.ctx.terminal.string_from_selection(&selection); if !buf.is_empty() { Clipboard::new() - .expect("get clipboard") - .store_selection(buf) - .expect("copy into clipboard"); + .and_then(|mut clipboard| clipboard.store_selection(buf)) + .unwrap_or_else(|err| { + err_println!("Error storing selection to clipboard: {}", Red(err)); + }); } } } diff --git a/src/main.rs b/src/main.rs index a1c8e77c..3328ab59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ use alacritty::event_loop::{self, EventLoop}; use alacritty::sync::FairMutex; use alacritty::term::{Term}; use alacritty::tty::{self, process_should_exit}; +use alacritty::util::fmt::Red; fn main() { // Load configuration @@ -58,13 +59,6 @@ fn main() { println!("Goodbye"); } -use std::fmt; -struct Red(T); -impl fmt::Display for Red { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "\x1b[31m{}\x1b[0m", self.0) - } -} /// Run Alacritty /// diff --git a/src/util.rs b/src/util.rs index cd8bc9a1..44f7b3de 100644 --- a/src/util.rs +++ b/src/util.rs @@ -35,6 +35,26 @@ pub fn limit(value: T, min: T, max: T) -> T { cmp::min(cmp::max(value, min), max) } +/// Utilities for writing to the +pub mod fmt { + use std::fmt; + + /// Write a `Display` or `Debug` escaped with Red + pub struct Red(pub T); + + impl fmt::Display for Red { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "\x1b[31m{}\x1b[0m", self.0) + } + } + + impl fmt::Debug for Red { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "\x1b[31m{:?}\x1b[0m", self.0) + } + } +} + #[cfg(test)] mod tests { use super::limit;