feat(bar): impl Styled for Bar (#1476)

Related: https://github.com/ratatui/ratatui/issues/683
This commit is contained in:
Emirhan TALA 2024-11-22 04:30:08 +03:00 committed by GitHub
parent afd1ce179b
commit b76ad3b02e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,13 @@
use ratatui_core::{buffer::Buffer, layout::Rect, style::Style, text::Line, widgets::Widget};
use ratatui_core::{
buffer::Buffer,
layout::Rect,
style::{Style, Styled},
text::Line,
widgets::Widget,
};
use unicode_width::UnicodeWidthStr;
/// A bar to be shown by the [`BarChart`](crate::barchart::BarChart) widget.
/// A bar to be shown by the [`BarChart`](super::BarChart) widget.
///
/// Here is an explanation of a `Bar`'s components.
/// ```plain
@ -24,7 +30,7 @@ use unicode_width::UnicodeWidthStr;
/// Bar::default()
/// .label("Bar 1".into())
/// .value(10)
/// .style(Style::new().red())
/// .red()
/// .value_style(Style::new().red().on_white())
/// .text_value("10°C".to_string());
/// ```
@ -206,3 +212,32 @@ impl<'a> Bar<'a> {
}
}
}
impl<'a> Styled for Bar<'a> {
type Item = Self;
fn style(&self) -> Style {
self.style
}
fn set_style<S: Into<Style>>(mut self, style: S) -> Self::Item {
self.style = style.into();
self
}
}
#[cfg(test)]
mod tests {
use ratatui_core::style::{Color, Modifier, Style, Stylize};
use super::*;
#[test]
fn test_bar_stylized() {
let bar = Bar::default().red().bold();
assert_eq!(
bar.style,
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)
);
}
}