Give the background color to the clear method.

This commit is contained in:
Alexandre Bury 2017-06-12 23:29:26 -07:00
parent 059812f427
commit dea07d29cf
7 changed files with 54 additions and 56 deletions

View File

@ -6,7 +6,7 @@ use self::bear_lib_terminal::terminal::{self, Event as BltEvent, KeyCode};
use backend;
use event::{Event, Key};
use std::collections::BTreeMap;
use theme::{BaseColor, Color, ColorStyle, Effect};
use theme::{BaseColor, Color, ColorPair, Effect};
pub struct Concrete {
colours: BTreeMap<i16, (BltColor, BltColor)>,
@ -24,16 +24,9 @@ impl backend::Backend for Concrete {
terminal::close();
}
fn init_color_style(&mut self, style: ColorStyle, foreground: &Color,
background: &Color) {
self.colours
.insert(style.id(),
(colour_to_blt_colour(foreground),
colour_to_blt_colour(background)));
}
fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) {
let (fg, bg) = self.colours[&color.id()];
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);
terminal::with_colors(fg, bg, f);
}
@ -61,7 +54,7 @@ impl backend::Backend for Concrete {
(width as usize, height as usize)
}
fn clear(&self) {
fn clear(&self, color: Color) {
terminal::clear(None);
}
@ -104,8 +97,8 @@ impl backend::Backend for Concrete {
}
}
fn colour_to_blt_colour(clr: &Color) -> BltColor {
let (r, g, b) = match *clr {
fn colour_to_blt_colour(clr: Color) -> BltColor {
let (r, g, b) = match clr {
// Colours taken from
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
Color::Dark(BaseColor::Black) => (0, 0, 0),
@ -141,7 +134,7 @@ fn blt_keycode_to_ev(kc: KeyCode, shift: bool, ctrl: bool) -> Event {
KeyCode::F1 | KeyCode::F2 | KeyCode::F3 | KeyCode::F4 |
KeyCode::F5 | KeyCode::F6 | KeyCode::F7 | KeyCode::F8 |
KeyCode::F9 | KeyCode::F10 | KeyCode::F11 | KeyCode::F12 |
KeyCode::Enter | KeyCode::Escape | KeyCode::Backspace |
KeyCode::NumEnter | KeyCode::Enter | KeyCode::Escape | KeyCode::Backspace |
KeyCode::Tab | KeyCode::Pause | KeyCode::Insert | KeyCode::Home |
KeyCode::PageUp | KeyCode::Delete | KeyCode::End |
KeyCode::PageDown | KeyCode::Right | KeyCode::Left |
@ -169,7 +162,7 @@ fn blt_keycode_to_ev(kc: KeyCode, shift: bool, ctrl: bool) -> Event {
KeyCode::Apostrophe | KeyCode::Comma | KeyCode::Period |
KeyCode::Slash | KeyCode::Space | KeyCode::NumDivide |
KeyCode::NumMultiply | KeyCode::NumMinus | KeyCode::NumPlus |
KeyCode::NumEnter | KeyCode::NumPeriod | KeyCode::Num1 |
KeyCode::NumPeriod | KeyCode::Num1 |
KeyCode::Num2 | KeyCode::Num3 | KeyCode::Num4 | KeyCode::Num5 |
KeyCode::Num6 | KeyCode::Num7 | KeyCode::Num8 | KeyCode::Num9 |
KeyCode::Num0 => {
@ -247,7 +240,7 @@ fn blt_keycode_to_char(kc: KeyCode, shift: bool) -> char {
KeyCode::Num8 => '8',
KeyCode::Num9 => '9',
KeyCode::Num0 => '0',
_ => unreachable!(),
_ => { println_stderr!("{:?}", kc); unreachable!() },
}
}
@ -265,7 +258,7 @@ fn blt_keycode_to_key(kc: KeyCode) -> Key {
KeyCode::F10 => Key::F10,
KeyCode::F11 => Key::F11,
KeyCode::F12 => Key::F12,
KeyCode::Enter => Key::Enter,
KeyCode::NumEnter | KeyCode::Enter => Key::Enter,
KeyCode::Escape => Key::Esc,
KeyCode::Backspace => Key::Backspace,
KeyCode::Tab => Key::Tab,

View File

@ -1,4 +1,4 @@
use theme::{BaseColor, Color, ColorStyle};
use theme::{BaseColor, Color};
#[cfg(feature = "ncurses")]
mod n;
@ -38,18 +38,3 @@ fn find_closest(color: &Color) -> u8 {
Color::RgbLowRes(r, g, b) => (16 + 36 * r + 6 * g + b) as u8,
}
}
fn color_id(style: ColorStyle) -> i16 {
match style {
ColorStyle::Background => 1,
ColorStyle::Shadow => 2,
ColorStyle::Primary => 3,
ColorStyle::Secondary => 4,
ColorStyle::Tertiary => 5,
ColorStyle::TitlePrimary => 6,
ColorStyle::TitleSecondary => 7,
ColorStyle::Highlight => 8,
ColorStyle::HighlightInactive => 9,
ColorStyle::Custom { .. } => 10,
}
}

View File

@ -1,11 +1,11 @@
extern crate ncurses;
use self::super::{color_id, find_closest};
use self::super::find_closest;
use backend;
use event::{Event, Key};
use std::cell::{RefCell, Cell};
use std::collections::HashMap;
use theme::{ColorPair, ColorStyle, Effect};
use theme::{Color, ColorPair, Effect};
use utf8;
pub struct Concrete {
@ -14,7 +14,10 @@ pub struct Concrete {
}
impl Concrete {
fn insert_color(&self, pairs: &mut HashMap<ColorPair, i16>, pair: ColorPair) -> i16 {
/// Save a new color pair.
fn insert_color(&self, pairs: &mut HashMap<ColorPair, i16>,
pair: ColorPair)
-> i16 {
let n = 1 + pairs.len() as i16;
let target = if ncurses::COLOR_PAIRS() > n as i32 {
@ -33,19 +36,26 @@ impl Concrete {
find_closest(&pair.back) as i16);
target
}
fn set_colorstyle(&self, pair: ColorPair) {
self.current_style.set(pair);
/// Checks the pair in the cache, or re-define a color if needed.
fn get_or_create(&self, pair: ColorPair) -> i16 {
let mut pairs = self.pairs.borrow_mut();
// Find if we have this color in stock
let i = if pairs.contains_key(&pair) {
if pairs.contains_key(&pair) {
// We got it!
pairs[&pair]
} else {
self.insert_color(&mut *pairs, pair)
};
}
}
fn set_colorstyle(&self, pair: ColorPair) {
let i = self.get_or_create(pair);
self.current_style.set(pair);
let style = ncurses::COLOR_PAIR(i);
ncurses::attron(style);
}
@ -64,8 +74,6 @@ impl backend::Backend for Concrete {
ncurses::cbreak();
ncurses::start_color();
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
ncurses::wbkgd(ncurses::stdscr(),
ncurses::COLOR_PAIR(color_id(ColorStyle::Background)));
Concrete {
current_style: Cell::new(ColorPair::from_256colors(0, 0)),
@ -108,7 +116,13 @@ impl backend::Backend for Concrete {
ncurses::attroff(style);
}
fn clear(&self) {
fn clear(&self, color: Color) {
let id = self.get_or_create(ColorPair {
front: color,
back: color,
});
ncurses::wbkgd(ncurses::stdscr(), ncurses::COLOR_PAIR(id));
ncurses::clear();
}

View File

@ -18,19 +18,22 @@ pub use self::termion::*;
pub trait Backend {
fn init() -> Self;
// TODO: take `self` by value?
// Or implement Drop?
fn finish(&mut self);
fn clear(&self);
fn refresh(&mut self);
fn has_colors(&self) -> bool;
fn print_at(&self, (usize, usize), &str);
fn poll_event(&self) -> event::Event;
fn set_refresh_rate(&mut self, fps: u32);
fn screen_size(&self) -> (usize, usize);
/// Main input method
fn poll_event(&self) -> event::Event;
/// Main method used for printing
fn print_at(&self, (usize, usize), &str);
fn clear(&self, color: theme::Color);
fn set_refresh_rate(&mut self, fps: u32);
// TODO: unify those into a single method?
fn with_color<F: FnOnce()>(&self, colors: theme::ColorPair, f: F);
fn with_effect<F: FnOnce()>(&self, effect: theme::Effect, f: F);

View File

@ -124,8 +124,11 @@ impl backend::Backend for Concrete {
(x as usize, y as usize)
}
fn clear(&self) {
// self.apply_colorstyle(theme::ColorStyle::Background);
fn clear(&self, color: theme::Color) {
self.apply_colorstyle(theme::ColorPair {
front: color,
back: color,
});
print!("{}", termion::clear::All);
}

View File

@ -287,14 +287,14 @@ impl Cursive {
pub fn set_theme(&mut self, theme: theme::Theme) {
self.theme = theme;
// self.theme.activate(&mut self.backend);
self.backend.clear();
self.clear();
}
/// Clears the screen.
///
/// Users rarely have to call this directly.
pub fn clear(&self) {
self.backend.clear();
self.backend.clear(self.theme.colors.background);
}
/// Loads a theme from the given file.
@ -617,7 +617,7 @@ impl Cursive {
}
if event == Event::WindowResize {
self.backend.clear();
self.clear();
}
// Event dispatch order:

View File

@ -53,7 +53,7 @@ impl<'a> Printer<'a> {
///
/// Users rarely need to call this directly.
pub fn clear(&self) {
self.backend.clear();
self.backend.clear(self.theme.colors.background);
}
/// Returns `true` if nothing has been printed yet.