Merge remote-tracking branch 'hcpl/default-color'

This commit is contained in:
Alexandre Bury 2017-07-18 01:01:35 +02:00
commit a668e7bc06
7 changed files with 55 additions and 5 deletions

View File

@ -0,0 +1,27 @@
extern crate cursive;
use cursive::Cursive;
use cursive::theme::{Color, Theme};
use cursive::views::TextView;
fn custom_theme_from_cursive(siv: &Cursive) -> Theme {
let mut theme = siv.current_theme().clone();
theme.colors.background = Color::Default;
theme
}
fn main() {
let mut siv = Cursive::new();
let theme = custom_theme_from_cursive(&siv);
// We can quit by pressing `q`
siv.add_global_callback('q', Cursive::quit);
siv.set_theme(theme);
siv.add_layer(TextView::new("Hello World with default terminal background color!\n\
Press q to quit the application."));
siv.run();
}

View File

@ -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<i16, (BltColor, BltColor)>,
}
@ -25,8 +30,8 @@ impl backend::Backend for Concrete {
}
fn with_color<F: FnOnce()>(&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),

View File

@ -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,

View File

@ -73,6 +73,7 @@ impl backend::Backend for Concrete {
ncurses::noecho();
ncurses::cbreak();
ncurses::start_color();
ncurses::use_default_colors();
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
Concrete {

View File

@ -72,6 +72,7 @@ impl backend::Backend for Concrete {
pancurses::noecho();
pancurses::cbreak();
pancurses::start_color();
pancurses::use_default_colors();
pancurses::curs_set(0);
Concrete {

View File

@ -197,6 +197,7 @@ fn with_color<F, R>(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),

View File

@ -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<u8> 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.