use crate::buffer::Buffer;
use crate::layout::Rect;
use crate::style::Style;
use crate::text::Text;
use crate::widgets::{Block, StatefulWidget, Widget};
#[derive(Debug, Clone)]
pub struct ListItem<'a> {
content: Text<'a>,
style: Style,
}
impl<'a> ListItem<'a> {
pub fn new<T>(content: T) -> ListItem<'a>
where
T: Into<Text<'a>>,
{
ListItem {
content: content.into(),
style: Style::default(),
}
}
pub fn style(mut self, style: Style) -> ListItem<'a> {
self.style = style;
self
}
fn height(&self) -> usize {
self.content.height().max(1)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ListState {
offset: usize,
selected: Option<usize>,
}
impl ListState {
pub fn selected(&self) -> Option<usize> {
self.selected
}
pub fn select(&mut self, index: Option<usize>) {
self.selected = index;
if index.is_none() {
self.offset = 0;
}
}
pub fn select_next(&mut self, len: usize) {
if len == 0 {
return;
}
let next = match self.selected {
Some(i) if i + 1 < len => i + 1,
Some(_) => len - 1,
None => 0,
};
self.selected = Some(next);
}
pub fn select_previous(&mut self) {
let prev = match self.selected {
Some(i) if i > 0 => i - 1,
Some(_) => 0,
None => 0,
};
self.selected = Some(prev);
}
}
#[derive(Debug, Default, Clone)]
pub struct List<'a> {
items: Vec<ListItem<'a>>,
block: Option<Block<'a>>,
style: Style,
highlight_style: Style,
highlight_symbol: Option<String>,
}
impl<'a> List<'a> {
pub fn new<T>(items: T) -> List<'a>
where
T: IntoIterator<Item = ListItem<'a>>,
{
List {
items: items.into_iter().collect(),
block: None,
style: Style::default(),
highlight_style: Style::default(),
highlight_symbol: None,
}
}
pub fn block(mut self, block: Block<'a>) -> List<'a> {
self.block = Some(block);
self
}
pub fn style(mut self, style: Style) -> List<'a> {
self.style = style;
self
}
pub fn highlight_style(mut self, style: Style) -> List<'a> {
self.highlight_style = style;
self
}
pub fn highlight_symbol<T: Into<String>>(mut self, symbol: T) -> List<'a> {
self.highlight_symbol = Some(symbol.into());
self
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
impl<'a> StatefulWidget for List<'a> {
type State = ListState;
fn render(mut self, area: Rect, buf: &mut Buffer, state: &mut ListState) {
let list_area = if let Some(block) = self.block.take() {
let inner = block.inner(area);
block.render(area, buf);
inner
} else {
area
};
if list_area.area() == 0 || self.items.is_empty() {
return;
}
buf.set_style(list_area, self.style);
let highlight_symbol = self.highlight_symbol.clone().unwrap_or_default();
let symbol_width = unicode_width::UnicodeWidthStr::width(highlight_symbol.as_str());
if let Some(selected) = state.selected {
if selected < state.offset {
state.offset = selected;
} else {
let mut used = 0usize;
let mut first_visible = selected;
for (i, item) in self.items.iter().enumerate().take(selected + 1).rev() {
used += item.height();
if used > list_area.height as usize {
first_visible = i + 1;
break;
}
first_visible = i;
}
if first_visible > state.offset {
state.offset = first_visible;
}
}
}
let mut y = list_area.y;
for (i, item) in self.items.iter().enumerate().skip(state.offset) {
if y >= list_area.bottom() {
break;
}
let is_selected = state.selected == Some(i);
let style = if is_selected {
item.style.patch(self.highlight_style)
} else {
item.style
};
if is_selected && !highlight_symbol.is_empty() {
buf.set_string(list_area.x, y, &highlight_symbol, style);
}
for (row, line) in item.content.lines.iter().enumerate() {
let line_y = y + row as u16;
if line_y >= list_area.bottom() {
break;
}
if is_selected {
buf.set_style(
Rect::new(list_area.x, line_y, list_area.width, 1),
self.highlight_style,
);
}
let mut cx = list_area.x + symbol_width as u16;
for span in &line.spans {
let merged = span.style.patch(if is_selected {
self.highlight_style
} else {
Style::default()
});
cx = buf.set_stringn(
cx,
line_y,
&span.content,
list_area.right().saturating_sub(cx) as usize,
merged,
);
}
}
y += item.height() as u16;
}
}
}
impl<'a> Widget for List<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut state = ListState::default();
StatefulWidget::render(self, area, buf, &mut state);
}
}