Rename {Color,ColorStyle}::Default to TerminalDefault

This commit is contained in:
Alexandre Bury 2017-07-18 01:36:34 +02:00
parent a668e7bc06
commit 2ffae1f524
6 changed files with 61 additions and 51 deletions

View File

@ -8,7 +8,7 @@ use cursive::views::TextView;
fn custom_theme_from_cursive(siv: &Cursive) -> Theme { fn custom_theme_from_cursive(siv: &Cursive) -> Theme {
let mut theme = siv.current_theme().clone(); let mut theme = siv.current_theme().clone();
theme.colors.background = Color::Default; theme.colors.background = Color::TerminalDefault;
theme theme
} }

View File

@ -2,7 +2,8 @@ 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, state, 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;
@ -13,16 +14,14 @@ enum ColorRole {
Background, Background,
} }
pub struct Concrete { pub struct Concrete {}
colours: BTreeMap<i16, (BltColor, BltColor)>,
}
impl backend::Backend for Concrete { impl backend::Backend for Concrete {
fn init() -> Self { fn init() -> Self {
terminal::open("Cursive", 80, 24); terminal::open("Cursive", 80, 24);
terminal::set(terminal::config::Window::empty().resizeable(true)); terminal::set(terminal::config::Window::empty().resizeable(true));
Concrete { colours: BTreeMap::new() } Concrete {}
} }
fn finish(&mut self) { fn finish(&mut self) {
@ -43,9 +42,11 @@ impl backend::Backend for Concrete {
// we'd need the colours in our position, // we'd need the colours in our position,
// but `f()` can do whatever // but `f()` can do whatever
Effect::Reverse => { Effect::Reverse => {
terminal::with_colors(BltColor::from_rgb(0, 0, 0), terminal::with_colors(
BltColor::from_rgb(255, 255, 255), BltColor::from_rgb(0, 0, 0),
f) BltColor::from_rgb(255, 255, 255),
f,
)
} }
} }
} }
@ -60,7 +61,9 @@ impl backend::Backend for Concrete {
} }
fn clear(&self, color: Color) { fn clear(&self, color: Color) {
terminal::set_background(colour_to_blt_colour(color, ColorRole::Background)); terminal::set_background(
colour_to_blt_colour(color, ColorRole::Background),
);
terminal::clear(None); terminal::clear(None);
} }
@ -105,14 +108,14 @@ impl backend::Backend for Concrete {
fn colour_to_blt_colour(clr: Color, role: ColorRole) -> 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 => { Color::TerminalDefault => {
let clr = match role { let clr = match role {
ColorRole::Foreground => state::foreground(), ColorRole::Foreground => state::foreground(),
ColorRole::Background => state::background(), ColorRole::Background => state::background(),
}; };
return clr; 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
@ -136,9 +139,11 @@ fn colour_to_blt_colour(clr: Color, role: ColorRole) -> BltColor {
Color::Rgb(r, g, b) => (r, g, b), Color::Rgb(r, g, b) => (r, g, b),
Color::RgbLowRes(r, g, b) => { Color::RgbLowRes(r, g, b) => {
((r as f32 / 5.0 * 255.0) as u8, (
(g as f32 / 5.0 * 255.0) as u8, (r as f32 / 5.0 * 255.0) as u8,
(b as f32 / 5.0 * 255.0) as u8) (g as f32 / 5.0 * 255.0) as u8,
(b as f32 / 5.0 * 255.0) as u8,
)
} }
}; };
BltColor::from_rgb(r, g, b) BltColor::from_rgb(r, g, b)

View File

@ -13,7 +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::TerminalDefault => -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,7 +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::TerminalDefault => 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

@ -287,7 +287,6 @@ impl Cursive {
/// Sets the current theme. /// Sets the current theme.
pub fn set_theme(&mut self, theme: theme::Theme) { pub fn set_theme(&mut self, theme: theme::Theme) {
self.theme = theme; self.theme = theme;
// self.theme.activate(&mut self.backend);
self.clear(); self.clear();
} }

View File

@ -132,7 +132,7 @@ pub enum Effect {
} }
/// Combines a front and back color. /// Combines a front and back color.
#[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ColorPair { pub struct ColorPair {
/// Color used for the foreground. /// Color used for the foreground.
pub front: Color, pub front: Color,
@ -165,10 +165,10 @@ impl ColorPair {
/// Represents a color pair role to use when printing something. /// Represents a color pair role to use when printing something.
/// ///
/// 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. /// Style set by terminal before entering a Cursive program.
Default, TerminalDefault,
/// 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.
@ -203,7 +203,10 @@ 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::TerminalDefault => (
Color::TerminalDefault,
Color::TerminalDefault,
),
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),
@ -220,7 +223,7 @@ impl ColorStyle {
} }
/// Represents the style a Cursive application will use. /// Represents the style a Cursive application will use.
#[derive(Clone,Debug)] #[derive(Clone, Debug)]
pub struct Theme { pub struct Theme {
/// Whether views in a StackView should have shadows. /// Whether views in a StackView should have shadows.
pub shadow: bool, pub shadow: bool,
@ -270,7 +273,7 @@ impl Theme {
/// Specifies how some borders should be drawn. /// Specifies how some borders should be drawn.
/// ///
/// Borders are used around Dialogs, select popups, and panels. /// Borders are used around Dialogs, select popups, and panels.
#[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BorderStyle { pub enum BorderStyle {
/// Simple borders. /// Simple borders.
Simple, Simple,
@ -295,7 +298,7 @@ impl BorderStyle {
/// Color configuration for the application. /// Color configuration for the application.
/// ///
/// Assign each color role an actual color. /// Assign each color role an actual color.
#[derive(Copy,Clone,Debug)] #[derive(Copy, Clone, Debug)]
pub struct Palette { pub struct Palette {
/// Color used for the application background. /// Color used for the application background.
pub background: Color, pub background: Color,
@ -331,8 +334,10 @@ impl Palette {
load_color(&mut self.title_primary, table.get("title_primary")); load_color(&mut self.title_primary, table.get("title_primary"));
load_color(&mut self.title_secondary, table.get("title_secondary")); load_color(&mut self.title_secondary, table.get("title_secondary"));
load_color(&mut self.highlight, table.get("highlight")); load_color(&mut self.highlight, table.get("highlight"));
load_color(&mut self.highlight_inactive, load_color(
table.get("highlight_inactive")); &mut self.highlight_inactive,
table.get("highlight_inactive"),
);
} }
} }
@ -359,7 +364,7 @@ fn load_color(target: &mut Color, value: Option<&toml::Value>) -> bool {
} }
/// One of the 8 base colors. /// One of the 8 base colors.
#[derive(Clone,Copy,Debug,PartialEq,Eq,Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BaseColor { pub enum BaseColor {
/// Black color /// Black color
/// ///
@ -412,10 +417,10 @@ 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. /// Represents a color, preset by terminal.
Default, TerminalDefault,
/// 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.
@ -475,24 +480,25 @@ impl Color {
fn parse(value: &str) -> Option<Self> { fn parse(value: &str) -> Option<Self> {
Some(match value { Some(match value {
"black" => Color::Dark(BaseColor::Black), "black" => Color::Dark(BaseColor::Black),
"red" => Color::Dark(BaseColor::Red), "red" => Color::Dark(BaseColor::Red),
"green" => Color::Dark(BaseColor::Green), "green" => Color::Dark(BaseColor::Green),
"yellow" => Color::Dark(BaseColor::Yellow), "yellow" => Color::Dark(BaseColor::Yellow),
"blue" => Color::Dark(BaseColor::Blue), "blue" => Color::Dark(BaseColor::Blue),
"magenta" => Color::Dark(BaseColor::Magenta), "magenta" => Color::Dark(BaseColor::Magenta),
"cyan" => Color::Dark(BaseColor::Cyan), "cyan" => Color::Dark(BaseColor::Cyan),
"white" => Color::Dark(BaseColor::White), "white" => Color::Dark(BaseColor::White),
"light black" => Color::Light(BaseColor::Black), "light black" => Color::Light(BaseColor::Black),
"light red" => Color::Light(BaseColor::Red), "light red" => Color::Light(BaseColor::Red),
"light green" => Color::Light(BaseColor::Green), "light green" => Color::Light(BaseColor::Green),
"light yellow" => Color::Light(BaseColor::Yellow), "light yellow" => Color::Light(BaseColor::Yellow),
"light blue" => Color::Light(BaseColor::Blue), "light blue" => Color::Light(BaseColor::Blue),
"light magenta" => Color::Light(BaseColor::Magenta), "light magenta" => Color::Light(BaseColor::Magenta),
"light cyan" => Color::Light(BaseColor::Cyan), "light cyan" => Color::Light(BaseColor::Cyan),
"light white" => Color::Light(BaseColor::White), "light white" => Color::Light(BaseColor::White),
value => return Color::parse_special(value), "default" => Color::TerminalDefault,
}) value => return Color::parse_special(value),
})
} }
fn parse_special(value: &str) -> Option<Color> { fn parse_special(value: &str) -> Option<Color> {
@ -514,9 +520,9 @@ impl Color {
let rgb: Vec<_> = let rgb: Vec<_> =
value.chars().map(|c| c as i16 - '0' as i16).collect(); value.chars().map(|c| c as i16 - '0' as i16).collect();
if rgb.iter().all(|&i| i >= 0 && i < 6) { if rgb.iter().all(|&i| i >= 0 && i < 6) {
Some(Color::RgbLowRes(rgb[0] as u8, Some(
rgb[1] as u8, Color::RgbLowRes(rgb[0] as u8, rgb[1] as u8, rgb[2] as u8),
rgb[2] as u8)) )
} else { } else {
None None
} }