use crate::buffer::Buffer;
use crate::layout::Rect;
use crate::style::Style;
use crate::text::Line;
use crate::widgets::{Block, Widget};
const BARS: [char; 9] = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
#[derive(Debug, Clone)]
pub struct Bar<'a> {
label: Option<Line<'a>>,
value: u64,
style: Style,
}
impl<'a> Bar<'a> {
pub fn new(value: u64) -> Bar<'a> {
Bar {
label: None,
value,
style: Style::default(),
}
}
pub fn label<T: Into<Line<'a>>>(mut self, label: T) -> Bar<'a> {
self.label = Some(label.into());
self
}
pub fn style(mut self, style: Style) -> Bar<'a> {
self.style = style;
self
}
}
#[derive(Debug, Default, Clone)]
pub struct BarChart<'a> {
block: Option<Block<'a>>,
bars: Vec<Bar<'a>>,
bar_width: u16,
bar_gap: u16,
max: Option<u64>,
style: Style,
value_style: Style,
}
impl<'a> BarChart<'a> {
pub fn new(bars: Vec<Bar<'a>>) -> BarChart<'a> {
BarChart {
block: None,
bars,
bar_width: 3,
bar_gap: 1,
max: None,
style: Style::default(),
value_style: Style::default(),
}
}
pub fn block(mut self, block: Block<'a>) -> BarChart<'a> {
self.block = Some(block);
self
}
pub fn bar_width(mut self, width: u16) -> BarChart<'a> {
self.bar_width = width.max(1);
self
}
pub fn bar_gap(mut self, gap: u16) -> BarChart<'a> {
self.bar_gap = gap;
self
}
pub fn max(mut self, max: u64) -> BarChart<'a> {
self.max = Some(max);
self
}
pub fn style(mut self, style: Style) -> BarChart<'a> {
self.style = style;
self
}
pub fn value_style(mut self, style: Style) -> BarChart<'a> {
self.value_style = style;
self
}
}
impl<'a> Widget for BarChart<'a> {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let area = if let Some(block) = self.block.take() {
let inner = block.inner(area);
block.render(area, buf);
inner
} else {
area
};
if area.area() == 0 || self.bars.is_empty() {
return;
}
buf.set_style(area, self.style);
let has_labels = self.bars.iter().any(|b| b.label.is_some());
let bar_height = if has_labels {
area.height.saturating_sub(1)
} else {
area.height
};
if bar_height == 0 {
return;
}
let max = self
.max
.unwrap_or_else(|| self.bars.iter().map(|b| b.value).max().unwrap_or(0))
.max(1);
let mut x = area.x;
for bar in &self.bars {
if x >= area.right() {
break;
}
let width = self.bar_width.min(area.right().saturating_sub(x));
let subunits_total = bar_height as u64 * 8;
let filled = (bar.value.min(max) as u128 * subunits_total as u128 / max as u128) as u64;
let mut top_empty_row: Option<u16> = None;
for row in 0..bar_height {
let row_start = row as u64 * 8;
let y = area.y + bar_height - 1 - row;
let ch = if filled >= row_start + 8 {
BARS[8]
} else if filled > row_start {
BARS[(filled - row_start) as usize]
} else {
if top_empty_row.is_none() {
top_empty_row = Some(y);
}
BARS[0]
};
for cx in x..x + width {
buf.get_mut(cx, y).set_char(ch).set_style(bar.style.patch(self.style));
}
}
if let Some(y) = top_empty_row {
let text = bar.value.to_string();
let vx = x + width.saturating_sub(text.len() as u16) / 2;
buf.set_stringn(vx, y, &text, width as usize, self.value_style);
}
if let Some(label) = &bar.label {
let y = area.y + bar_height;
let lw = label.width() as u16;
let lx = x + width.saturating_sub(lw) / 2;
let mut cx = lx;
for span in &label.spans {
cx = buf.set_stringn(cx, y, &span.content, width as usize, span.style);
}
}
x += width + self.bar_gap;
}
}
}