Improve error handling for clipboard actions

Previously, these could have crashed alacritty. Now, they simply print
an error message in Red to stderr. The Red format wrapper was moved to a
central location where both main.rs and the alacritty lib can access it.
This commit is contained in:
Joe Wilm 2017-01-01 19:09:27 -08:00
parent 82bfc41af7
commit d4e20a4741
3 changed files with 34 additions and 18 deletions

View File

@ -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));
});
}
}
}

View File

@ -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>(T);
impl<T: fmt::Display> fmt::Display for Red<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[31m{}\x1b[0m", self.0)
}
}
/// Run Alacritty
///

View File

@ -35,6 +35,26 @@ pub fn limit<T: Ord>(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<T>(pub T);
impl<T: fmt::Display> fmt::Display for Red<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[31m{}\x1b[0m", self.0)
}
}
impl<T: fmt::Debug> fmt::Debug for Red<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[31m{:?}\x1b[0m", self.0)
}
}
}
#[cfg(test)]
mod tests {
use super::limit;