diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ansi.rs | 8 | ||||
-rw-r--r-- | src/config.rs | 71 | ||||
-rw-r--r-- | src/term/color.rs | 4 | ||||
-rw-r--r-- | src/term/mod.rs | 5 | ||||
-rw-r--r-- | src/util.rs | 32 |
5 files changed, 94 insertions, 26 deletions
diff --git a/src/ansi.rs b/src/ansi.rs index 34048b8d..817c6e1a 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -383,10 +383,10 @@ pub enum NamedColor { Foreground = 256, /// The background color Background, - /// The cursor foreground color - CursorForeground, - /// The cursor background color - CursorBackground, + /// Color for the text under the cursor + CursorText, + /// Color for the cursor itself + Cursor, } impl NamedColor { diff --git a/src/config.rs b/src/config.rs index bab2cc20..ba1ae5f8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,8 @@ use notify::{Watcher as WatcherApi, RecommendedWatcher as FileWatcher, op}; use input::{Action, Binding, MouseBinding, KeyBinding}; use index::{Line, Column}; +use util::fmt::Yellow; + /// Function that returns true for serde default fn true_bool() -> bool { true @@ -694,19 +696,72 @@ pub enum Error { #[derive(Debug, Deserialize)] pub struct Colors { pub primary: PrimaryColors, - #[serde(default="default_cursor_colors")] - pub cursor: PrimaryColors, + #[serde(deserialize_with="deserialize_cursor_colors", default="default_cursor_colors")] + pub cursor: CursorColors, pub normal: AnsiColors, pub bright: AnsiColors, } -fn default_cursor_colors() -> PrimaryColors { - PrimaryColors { - foreground: Rgb { r: 0, g: 0, b: 0 }, - background: Rgb { r: 0xff, g: 0xff, b: 0xff }, +fn deserialize_cursor_colors<D>(deserializer: D) -> ::std::result::Result<CursorColors, D::Error> + where D: de::Deserializer +{ + let either = CursorOrPrimaryColors::deserialize(deserializer)?; + Ok(either.into_cursor_colors()) +} + +#[derive(Deserialize)] +#[serde(untagged)] +pub enum CursorOrPrimaryColors { + Cursor { + #[serde(deserialize_with = "rgb_from_hex")] + text: Rgb, + #[serde(deserialize_with = "rgb_from_hex")] + cursor: Rgb, + }, + Primary { + #[serde(deserialize_with = "rgb_from_hex")] + foreground: Rgb, + #[serde(deserialize_with = "rgb_from_hex")] + background: Rgb, + } +} + +impl CursorOrPrimaryColors { + fn into_cursor_colors(self) -> CursorColors { + match self { + CursorOrPrimaryColors::Cursor { text, cursor } => CursorColors { + text: text, + cursor: cursor + }, + CursorOrPrimaryColors::Primary { foreground, background } => { + // Must print in config since logger isn't setup yet. + println!("{}", + Yellow("You're using a deprecated form of cursor color config. Please update \ + your config to use `text` and `cursor` properties instead of `foreground` \ + and `background`. This will become an error in a future release.") + ); + CursorColors { + text: foreground, + cursor: background + } + } + } } } +fn default_cursor_colors() -> CursorColors { + CursorColors { + text: Rgb { r: 0, g: 0, b: 0 }, + cursor: Rgb { r: 0xff, g: 0xff, b: 0xff }, + } +} + +#[derive(Debug)] +pub struct CursorColors { + pub text: Rgb, + pub cursor: Rgb, +} + #[derive(Debug, Deserialize)] pub struct PrimaryColors { #[serde(deserialize_with = "rgb_from_hex")] @@ -731,7 +786,7 @@ impl Default for Colors { blue: Rgb {r: 0x7a, g: 0xa6, b: 0xda}, magenta: Rgb {r: 0xc3, g: 0x97, b: 0xd8}, cyan: Rgb {r: 0x70, g: 0xc0, b: 0xba}, - white: Rgb {r: 0x42, g: 0x42, b: 0x42}, + white: Rgb {r: 0xea, g: 0xea, b: 0xea}, }, bright: AnsiColors { black: Rgb {r: 0x66, g: 0x66, b: 0x66}, @@ -741,7 +796,7 @@ impl Default for Colors { blue: Rgb {r: 0x7a, g: 0xa6, b: 0xda}, magenta: Rgb {r: 0xb7, g: 0x7e, b: 0xe0}, cyan: Rgb {r: 0x54, g: 0xce, b: 0xd6}, - white: Rgb {r: 0x2a, g: 0x2a, b: 0x2a}, + white: Rgb {r: 0xff, g: 0xff, b: 0xff}, } } } diff --git a/src/term/color.rs b/src/term/color.rs index 8e15ad41..0c701c34 100644 --- a/src/term/color.rs +++ b/src/term/color.rs @@ -53,8 +53,8 @@ impl List { self[ansi::NamedColor::Background] = colors.primary.background; // Foreground and background for custom cursor colors - self[ansi::NamedColor::CursorForeground] = colors.cursor.foreground; - self[ansi::NamedColor::CursorBackground] = colors.cursor.background; + self[ansi::NamedColor::CursorText] = colors.cursor.text; + self[ansi::NamedColor::Cursor] = colors.cursor.cursor; } fn fill_cube(&mut self) { diff --git a/src/term/mod.rs b/src/term/mod.rs index 786265ef..126d34e3 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -91,9 +91,8 @@ impl<'a> RenderableCellsIter<'a> { if self.config.custom_cursor_colors() { let cell = &mut self.grid[self.cursor]; - cell.fg = Color::Named(NamedColor::CursorForeground); - cell.bg = Color::Named(NamedColor::CursorBackground); - + cell.fg = Color::Named(NamedColor::CursorText); + cell.bg = Color::Named(NamedColor::Cursor); } else { let cell = &mut self.grid[self.cursor]; mem::swap(&mut cell.fg, &mut cell.bg); diff --git a/src/util.rs b/src/util.rs index ccb22cc9..3452e5b2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -49,19 +49,33 @@ pub fn limit<T: Ord>(value: T, min: T, max: T) -> T { pub mod fmt { use std::fmt; - /// Write a `Display` or `Debug` escaped with Red - pub struct Red<T>(pub T); + macro_rules! define_colors { + ($($(#[$attrs:meta])* pub struct $s:ident => $color:expr;)*) => { + $( + $(#[$attrs])* + pub struct $s<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::Display> fmt::Display for $s<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "\x1b[{}m{}\x1b[0m", $color, self.0) + } + } + + impl<T: fmt::Debug> fmt::Debug for $s<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "\x1b[{}m{:?}\x1b[0m", $color, 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) - } + define_colors! { + /// Write a `Display` or `Debug` escaped with Red + pub struct Red => "31"; + + /// Write a `Display` or `Debug` escaped with Yellow + pub struct Yellow => "33"; } } |