diff --git a/src/buffer.rs b/src/buffer.rs index fa34f76..7db145b 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -103,11 +103,13 @@ impl Buffer { self.content[i].bg = color; } - pub fn set_string(&mut self, x: u16, y: u16, string: &str) { + pub fn set_string(&mut self, x: u16, y: u16, string: &str, fg: Color, bg: Color) { let mut cursor = (x, y); for c in string.chars() { let index = self.index_of(cursor.0, cursor.1); self.content[index].symbol = c; + self.content[index].fg = fg; + self.content[index].bg = bg; match self.next_pos(cursor.0, cursor.1) { Some(c) => { cursor = c; diff --git a/src/widgets/block.rs b/src/widgets/block.rs index d20ef6c..ba205e3 100644 --- a/src/widgets/block.rs +++ b/src/widgets/block.rs @@ -7,6 +7,8 @@ use widgets::{Widget, WidgetType, border, Line, vline, hline}; #[derive(Hash, Clone, Copy)] pub struct Block<'a> { title: Option<&'a str>, + title_fg: Color, + title_bg: Color, borders: border::Flags, border_fg: Color, border_bg: Color, @@ -16,6 +18,8 @@ impl<'a> Default for Block<'a> { fn default() -> Block<'a> { Block { title: None, + title_fg: Color::White, + title_bg: Color::Black, borders: border::NONE, border_fg: Color::White, border_bg: Color::Black, @@ -29,6 +33,16 @@ impl<'a> Block<'a> { self } + pub fn title_fg(&mut self, color: Color) -> &mut Block<'a> { + self.title_fg = color; + self + } + + pub fn title_bg(&mut self, color: Color) -> &mut Block<'a> { + self.title_bg = color; + self + } + pub fn borders(&mut self, flag: border::Flags) -> &mut Block<'a> { self.borders = flag; self @@ -111,7 +125,7 @@ impl<'a> Widget for Block<'a> { } else { (0, format!("{}", title)) }; - buf.set_string(margin_x, 0, &string); + buf.set_string(margin_x, 0, &string, self.title_fg, self.title_bg); } buf } diff --git a/src/widgets/gauge.rs b/src/widgets/gauge.rs index 1ef1124..61cb9f7 100644 --- a/src/widgets/gauge.rs +++ b/src/widgets/gauge.rs @@ -65,18 +65,19 @@ impl<'a> Widget for Gauge<'a> { if gauge_area.height < 1 { return buf; } else { - info!("{:?}{:?}", area, gauge_area); let margin_x = gauge_area.x - area.x; let margin_y = gauge_area.y - area.y; + // Label + let percent_string = format!("{}%", self.percent); + let len = percent_string.len() as u16; + let middle = gauge_area.width / 2 - len / 2; + buf.set_string(middle, margin_y, &percent_string, self.bg, self.fg); + // Gauge let width = (gauge_area.width * self.percent) / 100; for i in 0..width { buf.set_bg(margin_x + i, margin_y, self.bg); buf.set_fg(margin_x + i, margin_y, self.fg); } - let percent_string = format!("{}%", self.percent); - let len = percent_string.len() as u16; - let middle = gauge_area.width / 2 - len / 2; - buf.set_string(middle, margin_y, &percent_string); } buf } diff --git a/src/widgets/list.rs b/src/widgets/list.rs index 6a3bbeb..8c26a5c 100644 --- a/src/widgets/list.rs +++ b/src/widgets/list.rs @@ -5,11 +5,12 @@ use std::hash::{Hash, Hasher}; use buffer::Buffer; use widgets::{Widget, WidgetType, Block}; use layout::Rect; +use style::Color; pub struct List<'a, T> { block: Option>, selected: usize, - formatter: Box String>, + formatter: Box (String, Color, Color)>, items: Vec, } @@ -28,7 +29,7 @@ impl<'a, T> Default for List<'a, T> { List { block: None, selected: 0, - formatter: Box::new(|_, _| String::from("")), + formatter: Box::new(|_, _| (String::from(""), Color::White, Color::Black)), items: Vec::new(), } } @@ -43,7 +44,7 @@ impl<'a, T> List<'a, T> } pub fn formatter(&'a mut self, f: F) -> &mut List<'a, T> - where F: 'static + Fn(&T, bool) -> String + where F: 'static + Fn(&T, bool) -> (String, Color, Color) { self.formatter = Box::new(f); self @@ -82,9 +83,9 @@ impl<'a, T> Widget for List<'a, T> let index = i + offset; let ref item = self.items[index]; let ref formatter = self.formatter; - let mut string = formatter(item, self.selected == index); + let (mut string, fg, bg) = formatter(item, self.selected == index); string.truncate(list_area.width as usize); - buf.set_string(1, 1 + i as u16, &string); + buf.set_string(1, 1 + i as u16, &string, fg, bg); } buf } diff --git a/src/widgets/sparkline.rs b/src/widgets/sparkline.rs index 01e3579..f3bc2f7 100644 --- a/src/widgets/sparkline.rs +++ b/src/widgets/sparkline.rs @@ -9,7 +9,8 @@ use symbols::bar; #[derive(Hash)] pub struct Sparkline<'a> { block: Option>, - color: Color, + fg: Color, + bg: Color, data: Vec, max: Option, } @@ -18,7 +19,8 @@ impl<'a> Sparkline<'a> { pub fn new() -> Sparkline<'a> { Sparkline { block: None, - color: Color::White, + fg: Color::White, + bg: Color::Black, data: Vec::new(), max: None, } @@ -29,11 +31,17 @@ impl<'a> Sparkline<'a> { self } - pub fn color(&mut self, color: Color) -> &mut Sparkline<'a> { - self.color = color; + pub fn fg(&mut self, fg: Color) -> &mut Sparkline<'a> { + self.fg = fg; self } + pub fn bg(&mut self, bg: Color) -> &mut Sparkline<'a> { + self.bg = bg; + self + } + + pub fn data(&mut self, data: &[u64]) -> &mut Sparkline<'a> { self.data = data.to_vec(); self @@ -85,7 +93,7 @@ impl<'a> Widget for Sparkline<'a> { data[i] = 0; } } - buf.set_string(margin_x, margin_y + j, &line); + buf.set_string(margin_x, margin_y + j, &line, self.fg, self.bg); } } buf