use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style, Styled},
symbols::{self},
text::Line,
widgets::{Block, Widget, WidgetRef, block::BlockExt},
};
#[derive(Debug, Default, Clone, PartialEq)]
pub struct LineGauge<'a> {
block: Option<Block<'a>>,
ratio: f64,
label: Option<Line<'a>>,
line_set: symbols::line::Set,
style: Style,
filled_style: Style,
unfilled_style: Style,
}
#[allow(dead_code)]
impl<'a> LineGauge<'a> {
#[must_use = "method moves the value of self and returns the modified value"]
pub fn block(mut self, block: Block<'a>) -> Self {
self.block = Some(block);
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn ratio(mut self, ratio: f64) -> Self {
assert!(
(0.0..=1.0).contains(&ratio),
"Ratio should be between 0 and 1 inclusively."
);
self.ratio = ratio;
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn line_set(mut self, set: symbols::line::Set) -> Self {
self.line_set = set;
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn label<T>(mut self, label: T) -> Self
where
T: Into<Line<'a>>,
{
self.label = Some(label.into());
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
self.style = style.into();
self
}
#[deprecated(
since = "0.27.0",
note = "You should use `LineGauge::filled_style` instead."
)]
#[must_use = "method moves the value of self and returns the modified value"]
pub fn gauge_style<S: Into<Style>>(mut self, style: S) -> Self {
let style: Style = style.into();
let filled_color = style.fg.unwrap_or(Color::Reset);
let unfilled_color = style.bg.unwrap_or(Color::Reset);
self.filled_style = style.fg(filled_color).bg(Color::Reset);
self.unfilled_style = style.fg(unfilled_color).bg(Color::Reset);
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn filled_style<S: Into<Style>>(mut self, style: S) -> Self {
self.filled_style = style.into();
self
}
#[must_use = "method moves the value of self and returns the modified value"]
pub fn unfilled_style<S: Into<Style>>(mut self, style: S) -> Self {
self.unfilled_style = style.into();
self
}
}
impl Widget for LineGauge<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.render_ref(area, buf);
}
}
impl WidgetRef for LineGauge<'_> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
buf.set_style(area, self.style);
self.block.render_ref(area, buf);
let gauge_area = self.block.inner_if_some(area);
if gauge_area.is_empty() {
return;
}
let ratio = self.ratio;
let default_label = Line::from(format!("{:.0}%", ratio * 100.0));
let label = self.label.as_ref().unwrap_or(&default_label);
let (col, row) = buf.set_line(gauge_area.left(), gauge_area.top(), label, gauge_area.width);
buf[(col, row)]
.set_symbol("[")
.set_style(self.unfilled_style);
buf[(gauge_area.right(), row)]
.set_symbol("]")
.set_style(self.unfilled_style);
let start = col + 1;
if start >= gauge_area.right() - 1 {
return;
}
let end = start
+ (f64::from(gauge_area.right().saturating_sub(start)) * self.ratio).floor() as u16;
for col in start..end {
buf[(col, row)]
.set_symbol(self.line_set.horizontal)
.set_style(self.filled_style);
}
for col in end..gauge_area.right() {
buf[(col, row)]
.set_symbol(".")
.set_style(self.unfilled_style);
}
}
}
impl<'a> Styled for LineGauge<'a> {
type Item = Self;
fn style(&self) -> Style {
self.style
}
fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
self.style(style)
}
}