diff options
author | Joe Wilm <joe@jwilm.com> | 2017-01-01 19:09:27 -0800 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2017-01-01 19:09:27 -0800 |
commit | d4e20a4741a4966fbb0b8162ed7cdf47007ca7b8 (patch) | |
tree | d5afd5e61de6eb229fa2383b4dce8b3ada7edf62 /src/util.rs | |
parent | 82bfc41af7d6eff384bdf95cedaf0d62b5450979 (diff) | |
download | alacritty-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.
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 20 |
1 files changed, 20 insertions, 0 deletions
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; |