use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::widgets::Block;
use ratatui::widgets::Clear;
use ratatui::widgets::Paragraph;
use ratatui::widgets::StatefulWidget;
use ratatui::widgets::Widget;
use super::StatusBarPosition;
use super::StatusBarState;
pub struct StatusBarWidget
{
style: Style,
}
impl std::default::Default for StatusBarWidget
{
fn default() -> Self
{
Self::new()
}
}
impl StatusBarWidget
{
pub fn new() -> Self
{
Self {
style: Style::default(),
}
}
pub fn style<S: Into<Style>>(
mut self,
style: S,
) -> Self
{
self.style = style.into();
self
}
}
impl StatefulWidget for StatusBarWidget
{
type State = StatusBarState;
fn render(
self,
area: Rect,
buf: &mut Buffer,
state: &mut Self::State,
)
{
Clear.render(
area, buf,
);
Block::new()
.style(self.style)
.render(
area, buf,
);
let mut current_left = 1u16;
let mut current_right = area.width - 1;
for item in state
.items
.iter()
{
if let Some(value) = item
.value
.as_ref()
{
let mut text = value.clone();
let mut value_width = value
.chars()
.count();
if let Some(max_width) = item.max_width
{
if value_width > max_width
{
value_width = max_width;
let (new_text, _) = text.split_at(max_width);
text = new_text.to_string();
}
}
let mut item_area = area;
match item.position
{
StatusBarPosition::Right =>
{
current_right -= value_width as u16;
item_area.x = current_right;
current_right -= 2;
}
StatusBarPosition::Left =>
{
item_area.x = current_left;
current_left += value_width as u16;
current_left += 2;
}
}
Paragraph::new(text.as_str()).render(
item_area, buf,
);
}
}
}
}