Split style and color

Color is a color-pair
Style is an attribute, like Bold or Reversed
This commit is contained in:
Alexandre Bury 2015-05-26 21:45:00 -07:00
parent 5407c53545
commit abb09cf4bb
7 changed files with 22 additions and 12 deletions

View File

@ -30,7 +30,6 @@ pub const HIGHLIGHT: ThemeColor = 8;
/// Highlight color for inactive views (not in focus). /// Highlight color for inactive views (not in focus).
pub const HIGHLIGHT_INACTIVE: ThemeColor = 9; pub const HIGHLIGHT_INACTIVE: ThemeColor = 9;
fn load_hex(s: &str) -> i16 { fn load_hex(s: &str) -> i16 {
let mut sum = 0; let mut sum = 0;
for c in s.chars() { for c in s.chars() {

View File

@ -69,15 +69,23 @@ impl Printer {
/// printer.print((0,0), "This text is highlighted!"); /// printer.print((0,0), "This text is highlighted!");
/// }); /// });
/// ``` /// ```
pub fn with_style<'a, F>(&'a self, style: color::ThemeColor, f: F) pub fn with_color<'a, F>(&'a self, c: color::ThemeColor, f: F)
where F: Fn(&Printer) where F: Fn(&Printer)
{ {
ncurses::attron(ncurses::COLOR_PAIR(style)); ncurses::attron(ncurses::COLOR_PAIR(c));
f(self); f(self);
ncurses::attroff(ncurses::COLOR_PAIR(style)); ncurses::attroff(ncurses::COLOR_PAIR(c));
ncurses::attron(ncurses::COLOR_PAIR(color::PRIMARY)); ncurses::attron(ncurses::COLOR_PAIR(color::PRIMARY));
} }
pub fn with_style<'a, F>(&'a self, style: ncurses::attr_t, f: F)
where F: Fn(&Printer)
{
ncurses::attron(style);
f(self);
ncurses::attroff(style);
}
/// Prints a rectangular box. /// Prints a rectangular box.
/// ///
/// # Examples /// # Examples

View File

@ -32,7 +32,7 @@ impl View for Button {
let style = if !focused { color::PRIMARY } else { color::HIGHLIGHT }; let style = if !focused { color::PRIMARY } else { color::HIGHLIGHT };
let x = printer.size.x - 1; let x = printer.size.x - 1;
printer.with_style(style, |printer| { printer.with_color(style, |printer| {
printer.print((1,0), &self.label); printer.print((1,0), &self.label);
printer.print((0,0), "<"); printer.print((0,0), "<");
printer.print((x,0), ">"); printer.print((x,0), ">");

View File

@ -114,7 +114,7 @@ impl View for Dialog {
printer.print((x-2,0), ""); printer.print((x-2,0), "");
printer.print((x+self.title.len(),0), ""); printer.print((x+self.title.len(),0), "");
printer.with_style(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title)); printer.with_color(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title));
} }
} }

View File

@ -59,9 +59,12 @@ fn read_char(ch: i32) -> Option<char> {
impl View for EditView { impl View for EditView {
fn draw(&mut self, printer: &Printer, focused: bool) { fn draw(&mut self, printer: &Printer, focused: bool) {
let style = if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE }; // let style = if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE };
printer.with_style(style, |printer| { printer.with_color(color::SECONDARY, |printer| {
printer.with_style(ncurses::A_REVERSE(), |printer| {
printer.print((0,0), &self.content); printer.print((0,0), &self.content);
printer.print_hline((self.content.len(),0), printer.size.x-self.content.len(), '_' as u64);
});
}); });
} }

View File

@ -33,7 +33,7 @@ impl <T: View> ViewWrapper for ShadowView<T> {
fn wrap_draw(&mut self, printer: &Printer, focused: bool) { fn wrap_draw(&mut self, printer: &Printer, focused: bool) {
printer.with_style(color::PRIMARY, |printer| { printer.with_color(color::PRIMARY, |printer| {
// Draw the view background // Draw the view background
for y in 1..printer.size.y-1 { for y in 1..printer.size.y-1 {
printer.print_hline((1,y), printer.size.x-2, ' ' as u64); printer.print_hline((1,y), printer.size.x-2, ' ' as u64);
@ -46,7 +46,7 @@ impl <T: View> ViewWrapper for ShadowView<T> {
let h = printer.size.y-1; let h = printer.size.y-1;
let w = printer.size.x-1; let w = printer.size.x-1;
printer.with_style(color::SHADOW, |printer| { printer.with_color(color::SHADOW, |printer| {
printer.print_hline((2,h), w-1, ' ' as u64); printer.print_hline((2,h), w-1, ' ' as u64);
printer.print_vline((w,2), h-1, ' ' as u64); printer.print_vline((w,2), h-1, ' ' as u64);
}); });

View File

@ -186,7 +186,7 @@ impl View for TextView {
// Where ratio is ({start or end} / content.height) // Where ratio is ({start or end} / content.height)
let start = self.view_height * self.start_line / self.rows.len(); let start = self.view_height * self.start_line / self.rows.len();
let end = self.view_height * (self.start_line + self.view_height) / self.rows.len(); let end = self.view_height * (self.start_line + self.view_height) / self.rows.len();
printer.with_style( printer.with_color(
if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE }, if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE },
|printer| { |printer| {
printer.print_vline((printer.size.x-1, start), end-start, ' ' as u64); printer.print_vline((printer.size.x-1, start), end-start, ' ' as u64);