use ratatui::{
style::{Color, Style},
text::Line,
};
pub struct ItemList<'a> {
lines: Vec<Line<'a>>,
field_line_indices: Vec<usize>,
item_bg: Color,
}
impl<'a> ItemList<'a> {
pub fn new(item_bg: Color) -> Self {
Self {
lines: Vec::new(),
field_line_indices: Vec::new(),
item_bg,
}
}
pub fn push(&mut self, line: Line<'a>) {
if !self.lines.is_empty() {
self.lines.push(Line::from(""));
}
self.field_line_indices.push(self.lines.len());
self.lines.push(self.with_bg(line));
}
pub fn push_raw(&mut self, line: Line<'a>) {
self.lines.push(line);
}
pub fn into_parts(self) -> (Vec<Line<'a>>, Vec<usize>) {
(self.lines, self.field_line_indices)
}
fn with_bg(&self, line: Line<'a>) -> Line<'a> {
line.patch_style(Style::default().bg(self.item_bg))
}
}
impl Default for ItemList<'_> {
fn default() -> Self {
Self {
lines: Vec::new(),
field_line_indices: Vec::new(),
item_bg: Color::Reset,
}
}
}