From 28d0db1bb63833cc0926d257d801ac575b8aeefb Mon Sep 17 00:00:00 2001 From: hcpl Date: Sun, 16 Jul 2017 16:05:04 +0300 Subject: [PATCH] Make use of colors preset by terminals --- src/backend/blt.rs | 24 +++++++++++++++++++----- src/backend/curses/mod.rs | 1 + src/backend/termion.rs | 1 + src/theme.rs | 5 +++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/backend/blt.rs b/src/backend/blt.rs index 28dd1df..03a60d2 100644 --- a/src/backend/blt.rs +++ b/src/backend/blt.rs @@ -2,12 +2,17 @@ extern crate bear_lib_terminal; use self::bear_lib_terminal::Color as BltColor; use self::bear_lib_terminal::geometry::Size; -use self::bear_lib_terminal::terminal::{self, Event as BltEvent, KeyCode}; +use self::bear_lib_terminal::terminal::{self, state, Event as BltEvent, KeyCode}; use backend; use event::{Event, Key}; use std::collections::BTreeMap; use theme::{BaseColor, Color, ColorPair, Effect}; +enum ColorRole { + Foreground, + Background, +} + pub struct Concrete { colours: BTreeMap, } @@ -25,8 +30,8 @@ impl backend::Backend for Concrete { } fn with_color(&self, color: ColorPair, f: F) { - let fg = colour_to_blt_colour(color.front); - let bg = colour_to_blt_colour(color.back); + let fg = colour_to_blt_colour(color.front, ColorRole::Foreground); + let bg = colour_to_blt_colour(color.back, ColorRole::Background); terminal::with_colors(fg, bg, f); } @@ -55,7 +60,7 @@ impl backend::Backend for Concrete { } fn clear(&self, color: Color) { - terminal::set_background(colour_to_blt_colour(color)); + terminal::set_background(colour_to_blt_colour(color, ColorRole::Background)); terminal::clear(None); } @@ -98,8 +103,17 @@ impl backend::Backend for Concrete { } } -fn colour_to_blt_colour(clr: Color) -> BltColor { +fn colour_to_blt_colour(clr: Color, role: ColorRole) -> BltColor { let (r, g, b) = match clr { + Color::Default => { + let clr = match role { + ColorRole::Foreground => state::foreground(), + ColorRole::Background => state::background(), + }; + + return clr; + }, + // Colours taken from // https://en.wikipedia.org/wiki/ANSI_escape_code#Colors Color::Dark(BaseColor::Black) => (0, 0, 0), diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index 9cfbf43..7f899f3 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -13,6 +13,7 @@ pub use self::pan::*; fn find_closest(color: &Color) -> i16 { match *color { + Color::Default => -1, Color::Dark(BaseColor::Black) => 0, Color::Dark(BaseColor::Red) => 1, Color::Dark(BaseColor::Green) => 2, diff --git a/src/backend/termion.rs b/src/backend/termion.rs index 567e564..63d7f5c 100644 --- a/src/backend/termion.rs +++ b/src/backend/termion.rs @@ -197,6 +197,7 @@ fn with_color(clr: &theme::Color, f: F) -> R { match *clr { + theme::Color::Default => f(&tcolor::Reset), theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black), theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red), theme::Color::Dark(theme::BaseColor::Green) => f(&tcolor::Green), diff --git a/src/theme.rs b/src/theme.rs index f8c8607..a349a0f 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -167,6 +167,8 @@ impl ColorPair { /// The current theme will assign each role a foreground and background color. #[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] pub enum ColorStyle { + /// Style set by terminal before entering a Cursive program. + Default, /// Application background, where no view is present. Background, /// Color used by view shadows. Only background matters. @@ -201,6 +203,7 @@ impl ColorStyle { pub fn resolve(&self, theme: &Theme) -> ColorPair { let c = &theme.colors; let (front, back) = match *self { + ColorStyle::Default => (Color::Default, Color::Default), ColorStyle::Background => (c.view, c.background), ColorStyle::Shadow => (c.shadow, c.shadow), ColorStyle::Primary => (c.primary, c.view), @@ -411,6 +414,8 @@ impl From for BaseColor { /// Represents a color used by the theme. #[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] pub enum Color { + /// Represents a color, preset by terminal. + Default, /// One of the 8 base colors. Dark(BaseColor), /// Lighter version of a base color.