use unicode_width::UnicodeWidthStr; use crate::prelude::*; /// A bar to be shown by the [`BarChart`](crate::widgets::BarChart) widget. /// /// Here is an explanation of a `Bar`'s components. /// ```plain /// ███ ┐ /// █2█ <- text_value or value │ bar /// foo <- label ┘ /// ``` /// Note that every element can be styled individually. /// /// # Example /// /// The following example creates a bar with the label "Bar 1", a value "10", /// red background and a white value foreground. /// ``` /// use ratatui::{prelude::*, widgets::*}; /// /// Bar::default() /// .label("Bar 1".into()) /// .value(10) /// .style(Style::default().fg(Color::Red)) /// .value_style(Style::default().bg(Color::Red).fg(Color::White)) /// .text_value("10°C".to_string()); /// ``` #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] pub struct Bar<'a> { /// Value to display on the bar (computed when the data is passed to the widget) pub(super) value: u64, /// optional label to be printed under the bar pub(super) label: Option>, /// style for the bar pub(super) style: Style, /// style of the value printed at the bottom of the bar. pub(super) value_style: Style, /// optional text_value to be shown on the bar instead of the actual value pub(super) text_value: Option, } impl<'a> Bar<'a> { /// Set the value of this bar. /// /// The value will be displayed inside the bar. /// /// # See also /// /// [`Bar::value_style`] to style the value. /// [`Bar::text_value`] to set the displayed value. #[must_use = "method moves the value of self and returns the modified value"] pub const fn value(mut self, value: u64) -> Bar<'a> { self.value = value; self } /// Set the label of the bar. /// /// For [`Vertical`](crate::layout::Direction::Vertical) bars, /// display the label **under** the bar. /// For [`Horizontal`](crate::layout::Direction::Horizontal) bars, /// display the label **in** the bar. /// See [`BarChart::direction`](crate::widgets::BarChart::direction) to set the direction. #[must_use = "method moves the value of self and returns the modified value"] pub fn label(mut self, label: Line<'a>) -> Bar<'a> { self.label = Some(label); self } /// Set the style of the bar. /// /// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or /// your own type that implements [`Into