aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2017-01-01 19:09:27 -0800
committerJoe Wilm <joe@jwilm.com>2017-01-01 19:09:27 -0800
commitd4e20a4741a4966fbb0b8162ed7cdf47007ca7b8 (patch)
treed5afd5e61de6eb229fa2383b4dce8b3ada7edf62
parent82bfc41af7d6eff384bdf95cedaf0d62b5450979 (diff)
downloadalacritty-d4e20a4741a4966fbb0b8162ed7cdf47007ca7b8.tar.gz
alacritty-d4e20a4741a4966fbb0b8162ed7cdf47007ca7b8.zip
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.
-rw-r--r--src/input.rs24
-rw-r--r--src/main.rs8
-rw-r--r--src/util.rs20
3 files changed, 34 insertions, 18 deletions
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>(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
///
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<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;