use std::iter; use std::fmt::Display; use std::iter::Iterator; use unicode_width::UnicodeWidthStr; use buffer::Buffer; use widgets::{Block, Widget}; use layout::Rect; use style::Style; pub enum Item<'i, D: 'i> { Data(D), StyledData(D, &'i Style), } pub struct List<'b, 'i, L, D: 'i> where L: Iterator>, { block: Option>, items: L, style: Style, } impl<'b, 'i, L, D> Default for List<'b, 'i, L, D> where L: Iterator> + Default, { fn default() -> List<'b, 'i, L, D> { List { block: None, items: L::default(), style: Default::default(), } } } impl<'b, 'i, L, D> List<'b, 'i, L, D> where L: Iterator>, { pub fn new(items: L) -> List<'b, 'i, L, D> { List { block: None, items: items, style: Default::default(), } } pub fn block(&'b mut self, block: Block<'b>) -> &mut List<'b, 'i, L, D> { self.block = Some(block); self } pub fn items(&'b mut self, items: I) -> &mut List<'b, 'i, L, D> where I: IntoIterator, IntoIter = L>, { self.items = items.into_iter(); self } pub fn style(&'b mut self, style: Style) -> &mut List<'b, 'i, L, D> { self.style = style; self } } impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D> where L: Iterator>, D: Display, { fn draw(&mut self, area: &Rect, buf: &mut Buffer) { let list_area = match self.block { Some(ref mut b) => { b.draw(area, buf); b.inner(area) } None => *area, }; if list_area.width < 1 || list_area.height < 1 { return; } self.background(&list_area, buf, self.style.bg); for (i, item) in self.items .by_ref() .enumerate() .take(list_area.height as usize) { match item { Item::Data(ref v) => { buf.set_stringn( list_area.left(), list_area.top() + i as u16, &format!("{}", v), list_area.width as usize, &Style::default(), ); } Item::StyledData(ref v, ref s) => { buf.set_stringn( list_area.left(), list_area.top() + i as u16, &format!("{}", v), list_area.width as usize, s, ); } }; } } } /// A widget to display several items among which one can be selected (optional) /// /// # Examples /// /// ``` /// # extern crate tui; /// # use tui::widgets::{Block, border, SelectableList}; /// # use tui::style::{Style, Color, Modifier}; /// # fn main() { /// SelectableList::default() /// .block(Block::default().title("SelectableList").borders(border::ALL)) /// .items(&["Item 1", "Item 2", "Item 3"]) /// .select(1) /// .style(Style::default().fg(Color::White)) /// .highlight_style(Style::default().modifier(Modifier::Italic)) /// .highlight_symbol(">>"); /// # } /// ``` pub struct SelectableList<'b> { block: Option>, /// Items to be displayed items: Vec<&'b str>, /// Index of the one selected selected: Option, /// Base style of the widget style: Style, /// Style used to render selected item highlight_style: Style, /// Symbol in front of the selected item (Shift all items to the right) highlight_symbol: Option<&'b str>, } impl<'b> Default for SelectableList<'b> { fn default() -> SelectableList<'b> { SelectableList { block: None, items: Vec::new(), selected: None, style: Default::default(), highlight_style: Default::default(), highlight_symbol: None, } } } impl<'b> SelectableList<'b> { pub fn block(&'b mut self, block: Block<'b>) -> &mut SelectableList<'b> { self.block = Some(block); self } pub fn items(&'b mut self, items: &'b [I]) -> &mut SelectableList<'b> where I: AsRef + 'b, { self.items = items.iter().map(|i| i.as_ref()).collect::>(); self } pub fn style(&'b mut self, style: Style) -> &mut SelectableList<'b> { self.style = style; self } pub fn highlight_symbol(&'b mut self, highlight_symbol: &'b str) -> &mut SelectableList<'b> { self.highlight_symbol = Some(highlight_symbol); self } pub fn highlight_style(&'b mut self, highlight_style: Style) -> &mut SelectableList<'b> { self.highlight_style = highlight_style; self } pub fn select(&'b mut self, index: usize) -> &'b mut SelectableList<'b> { self.selected = Some(index); self } } impl<'b> Widget for SelectableList<'b> { fn draw(&mut self, area: &Rect, buf: &mut Buffer) { let list_area = match self.block { Some(ref mut b) => b.inner(area), None => *area, }; let list_height = list_area.height as usize; // Use highlight_style only if something is selected let (selected, highlight_style) = match self.selected { Some(i) => (i, &self.highlight_style), None => (0, &self.style), }; let highlight_symbol = self.highlight_symbol.unwrap_or(""); let blank_symbol = iter::repeat(" ") .take(highlight_symbol.width()) .collect::(); // Make sure the list show the selected item let offset = if selected >= list_height { selected - list_height + 1 } else { 0 }; // Render items let items = self.items .iter() .enumerate() .map(|(i, item)| if i == selected { Item::StyledData(format!("{} {}", highlight_symbol, item), highlight_style) } else { Item::StyledData(format!("{} {}", blank_symbol, item), &self.style) }) .skip(offset as usize); List::new(items) .block(self.block.unwrap_or_default()) .draw(area, buf); } }