Custom label for gauge

This commit is contained in:
Florian Dehau 2016-11-07 15:57:35 +01:00
parent 17c95cf826
commit a3c96865a8

View file

@ -23,6 +23,7 @@ use layout::Rect;
pub struct Gauge<'a> { pub struct Gauge<'a> {
block: Option<Block<'a>>, block: Option<Block<'a>>,
percent: u16, percent: u16,
label: Option<&'a str>,
style: Style, style: Style,
} }
@ -31,6 +32,7 @@ impl<'a> Default for Gauge<'a> {
Gauge { Gauge {
block: None, block: None,
percent: 0, percent: 0,
label: None,
style: Default::default(), style: Default::default(),
} }
} }
@ -47,6 +49,11 @@ impl<'a> Gauge<'a> {
self self
} }
pub fn label(&mut self, string: &'a str) -> &mut Gauge<'a> {
self.label = Some(string);
self
}
pub fn style(&mut self, style: Style) -> &mut Gauge<'a> { pub fn style(&mut self, style: Style) -> &mut Gauge<'a> {
self.style = style; self.style = style;
self self
@ -80,7 +87,8 @@ impl<'a> Widget for Gauge<'a> {
} }
// Label // Label
let label = format!("{}%", self.percent); let precent_label = format!("{}%", self.percent);
let label = self.label.unwrap_or(&precent_label);
let label_width = label.width() as u16; let label_width = label.width() as u16;
let middle = (gauge_area.width - label_width) / 2 + gauge_area.left(); let middle = (gauge_area.width - label_width) / 2 + gauge_area.left();
buf.set_string(middle, gauge_area.top(), &label, &self.style); buf.set_string(middle, gauge_area.top(), &label, &self.style);