Make use of colors preset by terminals

This commit is contained in:
hcpl 2017-07-16 16:05:04 +03:00
parent 94c67b2257
commit 28d0db1bb6
4 changed files with 26 additions and 5 deletions

View File

@ -2,12 +2,17 @@ extern crate bear_lib_terminal;
use self::bear_lib_terminal::Color as BltColor; use self::bear_lib_terminal::Color as BltColor;
use self::bear_lib_terminal::geometry::Size; 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 backend;
use event::{Event, Key}; use event::{Event, Key};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use theme::{BaseColor, Color, ColorPair, Effect}; use theme::{BaseColor, Color, ColorPair, Effect};
enum ColorRole {
Foreground,
Background,
}
pub struct Concrete { pub struct Concrete {
colours: BTreeMap<i16, (BltColor, BltColor)>, colours: BTreeMap<i16, (BltColor, BltColor)>,
} }
@ -25,8 +30,8 @@ impl backend::Backend for Concrete {
} }
fn with_color<F: FnOnce()>(&self, color: ColorPair, f: F) { fn with_color<F: FnOnce()>(&self, color: ColorPair, f: F) {
let fg = colour_to_blt_colour(color.front); let fg = colour_to_blt_colour(color.front, ColorRole::Foreground);
let bg = colour_to_blt_colour(color.back); let bg = colour_to_blt_colour(color.back, ColorRole::Background);
terminal::with_colors(fg, bg, f); terminal::with_colors(fg, bg, f);
} }
@ -55,7 +60,7 @@ impl backend::Backend for Concrete {
} }
fn clear(&self, color: Color) { 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); 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 { 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 // Colours taken from
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors // https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Color::Dark(BaseColor::Black) => (0, 0, 0), Color::Dark(BaseColor::Black) => (0, 0, 0),

View File

@ -13,6 +13,7 @@ pub use self::pan::*;
fn find_closest(color: &Color) -> i16 { fn find_closest(color: &Color) -> i16 {
match *color { match *color {
Color::Default => -1,
Color::Dark(BaseColor::Black) => 0, Color::Dark(BaseColor::Black) => 0,
Color::Dark(BaseColor::Red) => 1, Color::Dark(BaseColor::Red) => 1,
Color::Dark(BaseColor::Green) => 2, Color::Dark(BaseColor::Green) => 2,

View File

@ -197,6 +197,7 @@ fn with_color<F, R>(clr: &theme::Color, f: F) -> R
{ {
match *clr { match *clr {
theme::Color::Default => f(&tcolor::Reset),
theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black), theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black),
theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red), theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red),
theme::Color::Dark(theme::BaseColor::Green) => f(&tcolor::Green), theme::Color::Dark(theme::BaseColor::Green) => f(&tcolor::Green),

View File

@ -167,6 +167,8 @@ impl ColorPair {
/// The current theme will assign each role a foreground and background color. /// The current theme will assign each role a foreground and background color.
#[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] #[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)]
pub enum ColorStyle { pub enum ColorStyle {
/// Style set by terminal before entering a Cursive program.
Default,
/// Application background, where no view is present. /// Application background, where no view is present.
Background, Background,
/// Color used by view shadows. Only background matters. /// Color used by view shadows. Only background matters.
@ -201,6 +203,7 @@ impl ColorStyle {
pub fn resolve(&self, theme: &Theme) -> ColorPair { pub fn resolve(&self, theme: &Theme) -> ColorPair {
let c = &theme.colors; let c = &theme.colors;
let (front, back) = match *self { let (front, back) = match *self {
ColorStyle::Default => (Color::Default, Color::Default),
ColorStyle::Background => (c.view, c.background), ColorStyle::Background => (c.view, c.background),
ColorStyle::Shadow => (c.shadow, c.shadow), ColorStyle::Shadow => (c.shadow, c.shadow),
ColorStyle::Primary => (c.primary, c.view), ColorStyle::Primary => (c.primary, c.view),
@ -411,6 +414,8 @@ impl From<u8> for BaseColor {
/// Represents a color used by the theme. /// Represents a color used by the theme.
#[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] #[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)]
pub enum Color { pub enum Color {
/// Represents a color, preset by terminal.
Default,
/// One of the 8 base colors. /// One of the 8 base colors.
Dark(BaseColor), Dark(BaseColor),
/// Lighter version of a base color. /// Lighter version of a base color.