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;
}
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;

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

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

@ -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<Block<'a>>,
selected: usize,
formatter: Box<Fn(&T, bool) -> String>,
formatter: Box<Fn(&T, bool) -> (String, Color, Color)>,
items: Vec<T>,
}
@ -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<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
@ -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
}

@ -9,7 +9,8 @@ use symbols::bar;
#[derive(Hash)]
pub struct Sparkline<'a> {
block: Option<Block<'a>>,
color: Color,
fg: Color,
bg: Color,
data: Vec<u64>,
max: Option<u64>,
}
@ -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

Loading…
Cancel
Save