Update color support for block, gauge, list and sparkline

pull/3/head
Florian Dehau 8 years ago
parent ea1397319c
commit 6ed19df342

@ -103,11 +103,13 @@ impl Buffer {
self.content[i].bg = color; 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); let mut cursor = (x, y);
for c in string.chars() { for c in string.chars() {
let index = self.index_of(cursor.0, cursor.1); let index = self.index_of(cursor.0, cursor.1);
self.content[index].symbol = c; self.content[index].symbol = c;
self.content[index].fg = fg;
self.content[index].bg = bg;
match self.next_pos(cursor.0, cursor.1) { match self.next_pos(cursor.0, cursor.1) {
Some(c) => { Some(c) => {
cursor = c; cursor = c;

@ -7,6 +7,8 @@ use widgets::{Widget, WidgetType, border, Line, vline, hline};
#[derive(Hash, Clone, Copy)] #[derive(Hash, Clone, Copy)]
pub struct Block<'a> { pub struct Block<'a> {
title: Option<&'a str>, title: Option<&'a str>,
title_fg: Color,
title_bg: Color,
borders: border::Flags, borders: border::Flags,
border_fg: Color, border_fg: Color,
border_bg: Color, border_bg: Color,
@ -16,6 +18,8 @@ impl<'a> Default for Block<'a> {
fn default() -> Block<'a> { fn default() -> Block<'a> {
Block { Block {
title: None, title: None,
title_fg: Color::White,
title_bg: Color::Black,
borders: border::NONE, borders: border::NONE,
border_fg: Color::White, border_fg: Color::White,
border_bg: Color::Black, border_bg: Color::Black,
@ -29,6 +33,16 @@ impl<'a> Block<'a> {
self 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> { pub fn borders(&mut self, flag: border::Flags) -> &mut Block<'a> {
self.borders = flag; self.borders = flag;
self self
@ -111,7 +125,7 @@ impl<'a> Widget for Block<'a> {
} else { } else {
(0, format!("{}", title)) (0, format!("{}", title))
}; };
buf.set_string(margin_x, 0, &string); buf.set_string(margin_x, 0, &string, self.title_fg, self.title_bg);
} }
buf buf
} }

@ -65,18 +65,19 @@ impl<'a> Widget for Gauge<'a> {
if gauge_area.height < 1 { if gauge_area.height < 1 {
return buf; return buf;
} else { } else {
info!("{:?}{:?}", area, gauge_area);
let margin_x = gauge_area.x - area.x; let margin_x = gauge_area.x - area.x;
let margin_y = gauge_area.y - area.y; 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; let width = (gauge_area.width * self.percent) / 100;
for i in 0..width { for i in 0..width {
buf.set_bg(margin_x + i, margin_y, self.bg); buf.set_bg(margin_x + i, margin_y, self.bg);
buf.set_fg(margin_x + i, margin_y, self.fg); 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 buf
} }

@ -5,11 +5,12 @@ use std::hash::{Hash, Hasher};
use buffer::Buffer; use buffer::Buffer;
use widgets::{Widget, WidgetType, Block}; use widgets::{Widget, WidgetType, Block};
use layout::Rect; use layout::Rect;
use style::Color;
pub struct List<'a, T> { pub struct List<'a, T> {
block: Option<Block<'a>>, block: Option<Block<'a>>,
selected: usize, selected: usize,
formatter: Box<Fn(&T, bool) -> String>, formatter: Box<Fn(&T, bool) -> (String, Color, Color)>,
items: Vec<T>, items: Vec<T>,
} }
@ -28,7 +29,7 @@ impl<'a, T> Default for List<'a, T> {
List { List {
block: None, block: None,
selected: 0, selected: 0,
formatter: Box::new(|_, _| String::from("")), formatter: Box::new(|_, _| (String::from(""), Color::White, Color::Black)),
items: Vec::new(), items: Vec::new(),
} }
} }
@ -43,7 +44,7 @@ impl<'a, T> List<'a, T>
} }
pub fn formatter<F>(&'a mut self, f: F) -> &mut List<'a, T> pub fn formatter<F>(&'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.formatter = Box::new(f);
self self
@ -82,9 +83,9 @@ impl<'a, T> Widget for List<'a, T>
let index = i + offset; let index = i + offset;
let ref item = self.items[index]; let ref item = self.items[index];
let ref formatter = self.formatter; 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); 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 buf
} }

@ -9,7 +9,8 @@ use symbols::bar;
#[derive(Hash)] #[derive(Hash)]
pub struct Sparkline<'a> { pub struct Sparkline<'a> {
block: Option<Block<'a>>, block: Option<Block<'a>>,
color: Color, fg: Color,
bg: Color,
data: Vec<u64>, data: Vec<u64>,
max: Option<u64>, max: Option<u64>,
} }
@ -18,7 +19,8 @@ impl<'a> Sparkline<'a> {
pub fn new() -> Sparkline<'a> { pub fn new() -> Sparkline<'a> {
Sparkline { Sparkline {
block: None, block: None,
color: Color::White, fg: Color::White,
bg: Color::Black,
data: Vec::new(), data: Vec::new(),
max: None, max: None,
} }
@ -29,11 +31,17 @@ impl<'a> Sparkline<'a> {
self self
} }
pub fn color(&mut self, color: Color) -> &mut Sparkline<'a> { pub fn fg(&mut self, fg: Color) -> &mut Sparkline<'a> {
self.color = color; self.fg = fg;
self 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> { pub fn data(&mut self, data: &[u64]) -> &mut Sparkline<'a> {
self.data = data.to_vec(); self.data = data.to_vec();
self self
@ -85,7 +93,7 @@ impl<'a> Widget for Sparkline<'a> {
data[i] = 0; 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 buf

Loading…
Cancel
Save