|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
use std::iter;
|
|
|
|
use std::cmp::min;
|
|
|
|
use std::cmp::min;
|
|
|
|
|
|
|
|
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
@ -7,17 +8,88 @@ use widgets::{Widget, Block};
|
|
|
|
use layout::Rect;
|
|
|
|
use layout::Rect;
|
|
|
|
use style::Style;
|
|
|
|
use style::Style;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct List<'a, T>
|
|
|
|
|
|
|
|
where T: AsRef<str> + 'a
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
block: Option<Block<'a>>,
|
|
|
|
|
|
|
|
items: &'a [(T, &'a Style)],
|
|
|
|
|
|
|
|
style: Style,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Default for List<'a, T>
|
|
|
|
|
|
|
|
where T: AsRef<str> + 'a
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn default() -> List<'a, T> {
|
|
|
|
|
|
|
|
List {
|
|
|
|
|
|
|
|
block: None,
|
|
|
|
|
|
|
|
items: &[],
|
|
|
|
|
|
|
|
style: Default::default(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, T> List<'a, T>
|
|
|
|
|
|
|
|
where T: AsRef<str> + 'a
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
pub fn block(&'a mut self, block: Block<'a>) -> &mut List<'a, T> {
|
|
|
|
|
|
|
|
self.block = Some(block);
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn items(&'a mut self, items: &'a [(T, &'a Style)]) -> &mut List<'a, T> {
|
|
|
|
|
|
|
|
self.items = items;
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn style(&'a mut self, style: Style) -> &mut List<'a, T> {
|
|
|
|
|
|
|
|
self.style = style;
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Widget for List<'a, T>
|
|
|
|
|
|
|
|
where T: AsRef<str> + 'a
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn draw(&self, area: &Rect, buf: &mut Buffer) {
|
|
|
|
|
|
|
|
let list_area = match self.block {
|
|
|
|
|
|
|
|
Some(ref 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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let max_index = min(self.items.len(), list_area.height as usize);
|
|
|
|
|
|
|
|
for (i, &(ref item, style)) in self.items.iter().enumerate().take(max_index) {
|
|
|
|
|
|
|
|
buf.set_stringn(list_area.left(),
|
|
|
|
|
|
|
|
list_area.top() + i as u16,
|
|
|
|
|
|
|
|
item.as_ref(),
|
|
|
|
|
|
|
|
list_area.width as usize,
|
|
|
|
|
|
|
|
&style);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A widget to display several items among which one can be selected (optional)
|
|
|
|
/// A widget to display several items among which one can be selected (optional)
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
/// # extern crate tui;
|
|
|
|
/// # extern crate tui;
|
|
|
|
/// # use tui::widgets::{Block, border, List};
|
|
|
|
/// # use tui::widgets::{Block, border, SelectableList};
|
|
|
|
/// # use tui::style::{Style, Color, Modifier};
|
|
|
|
/// # use tui::style::{Style, Color, Modifier};
|
|
|
|
/// # fn main() {
|
|
|
|
/// # fn main() {
|
|
|
|
/// List::default()
|
|
|
|
/// SelectableList::default()
|
|
|
|
/// .block(Block::default().title("List").borders(border::ALL))
|
|
|
|
/// .block(Block::default().title("SelectableList").borders(border::ALL))
|
|
|
|
/// .items(&["Item 1", "Item 2", "Item 3"])
|
|
|
|
/// .items(&["Item 1", "Item 2", "Item 3"])
|
|
|
|
/// .select(1)
|
|
|
|
/// .select(1)
|
|
|
|
/// .style(Style::default().fg(Color::White))
|
|
|
|
/// .style(Style::default().fg(Color::White))
|
|
|
@ -25,7 +97,7 @@ use style::Style;
|
|
|
|
/// .highlight_symbol(">>");
|
|
|
|
/// .highlight_symbol(">>");
|
|
|
|
/// # }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub struct List<'a> {
|
|
|
|
pub struct SelectableList<'a> {
|
|
|
|
block: Option<Block<'a>>,
|
|
|
|
block: Option<Block<'a>>,
|
|
|
|
/// Items to be displayed
|
|
|
|
/// Items to be displayed
|
|
|
|
items: &'a [&'a str],
|
|
|
|
items: &'a [&'a str],
|
|
|
@ -39,9 +111,9 @@ pub struct List<'a> {
|
|
|
|
highlight_symbol: Option<&'a str>,
|
|
|
|
highlight_symbol: Option<&'a str>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Default for List<'a> {
|
|
|
|
impl<'a> Default for SelectableList<'a> {
|
|
|
|
fn default() -> List<'a> {
|
|
|
|
fn default() -> SelectableList<'a> {
|
|
|
|
List {
|
|
|
|
SelectableList {
|
|
|
|
block: None,
|
|
|
|
block: None,
|
|
|
|
items: &[],
|
|
|
|
items: &[],
|
|
|
|
selected: None,
|
|
|
|
selected: None,
|
|
|
@ -52,56 +124,46 @@ impl<'a> Default for List<'a> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> List<'a> {
|
|
|
|
impl<'a> SelectableList<'a> {
|
|
|
|
pub fn block(&'a mut self, block: Block<'a>) -> &mut List<'a> {
|
|
|
|
pub fn block(&'a mut self, block: Block<'a>) -> &mut SelectableList<'a> {
|
|
|
|
self.block = Some(block);
|
|
|
|
self.block = Some(block);
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn items(&'a mut self, items: &'a [&'a str]) -> &mut List<'a> {
|
|
|
|
pub fn items(&'a mut self, items: &'a [&'a str]) -> &mut SelectableList<'a> {
|
|
|
|
self.items = items;
|
|
|
|
self.items = items;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn style(&'a mut self, style: Style) -> &mut List<'a> {
|
|
|
|
pub fn style(&'a mut self, style: Style) -> &mut SelectableList<'a> {
|
|
|
|
self.style = style;
|
|
|
|
self.style = style;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn highlight_symbol(&'a mut self, highlight_symbol: &'a str) -> &mut List<'a> {
|
|
|
|
pub fn highlight_symbol(&'a mut self, highlight_symbol: &'a str) -> &mut SelectableList<'a> {
|
|
|
|
self.highlight_symbol = Some(highlight_symbol);
|
|
|
|
self.highlight_symbol = Some(highlight_symbol);
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn highlight_style(&'a mut self, highlight_style: Style) -> &mut List<'a> {
|
|
|
|
pub fn highlight_style(&'a mut self, highlight_style: Style) -> &mut SelectableList<'a> {
|
|
|
|
self.highlight_style = highlight_style;
|
|
|
|
self.highlight_style = highlight_style;
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn select(&'a mut self, index: usize) -> &'a mut List<'a> {
|
|
|
|
pub fn select(&'a mut self, index: usize) -> &'a mut SelectableList<'a> {
|
|
|
|
self.selected = Some(index);
|
|
|
|
self.selected = Some(index);
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Widget for List<'a> {
|
|
|
|
impl<'a> Widget for SelectableList<'a> {
|
|
|
|
fn draw(&self, area: &Rect, buf: &mut Buffer) {
|
|
|
|
fn draw(&self, area: &Rect, buf: &mut Buffer) {
|
|
|
|
|
|
|
|
|
|
|
|
let list_area = match self.block {
|
|
|
|
let list_area = match self.block {
|
|
|
|
Some(ref b) => {
|
|
|
|
Some(ref b) => b.inner(area),
|
|
|
|
b.draw(area, buf);
|
|
|
|
|
|
|
|
b.inner(area)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
None => *area,
|
|
|
|
None => *area,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if list_area.width < 1 || list_area.height < 1 {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.background(&list_area, buf, self.style.bg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let list_length = self.items.len();
|
|
|
|
|
|
|
|
let list_height = list_area.height as usize;
|
|
|
|
let list_height = list_area.height as usize;
|
|
|
|
|
|
|
|
|
|
|
|
// Use highlight_style only if something is selected
|
|
|
|
// Use highlight_style only if something is selected
|
|
|
@ -109,41 +171,30 @@ impl<'a> Widget for List<'a> {
|
|
|
|
Some(i) => (i, &self.highlight_style),
|
|
|
|
Some(i) => (i, &self.highlight_style),
|
|
|
|
None => (0, &self.style),
|
|
|
|
None => (0, &self.style),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
let highlight_symbol = self.highlight_symbol.unwrap_or("");
|
|
|
|
|
|
|
|
let blank_symbol = iter::repeat(" ").take(highlight_symbol.width()).collect::<String>();
|
|
|
|
// Make sure the list show the selected item
|
|
|
|
// Make sure the list show the selected item
|
|
|
|
let offset = if selected >= list_height {
|
|
|
|
let offset = if selected >= list_height {
|
|
|
|
selected - list_height + 1
|
|
|
|
selected - list_height + 1
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
0
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
let items = self.items
|
|
|
|
// Move items to the right if a highlight symbol was provided
|
|
|
|
.iter()
|
|
|
|
let x = match self.highlight_symbol {
|
|
|
|
.cloned()
|
|
|
|
Some(s) => (s.width() + 1) as u16 + list_area.left(),
|
|
|
|
.enumerate()
|
|
|
|
None => list_area.left(),
|
|
|
|
.map(|(i, item)| if i == selected {
|
|
|
|
};
|
|
|
|
(format!("{} {}", highlight_symbol, item), highlight_style)
|
|
|
|
|
|
|
|
|
|
|
|
// Render items
|
|
|
|
|
|
|
|
if x < list_area.right() {
|
|
|
|
|
|
|
|
let width = (list_area.right() - x) as usize;
|
|
|
|
|
|
|
|
let max_index = min(list_height, list_length);
|
|
|
|
|
|
|
|
for i in 0..max_index {
|
|
|
|
|
|
|
|
let index = i + offset;
|
|
|
|
|
|
|
|
let item = self.items[index];
|
|
|
|
|
|
|
|
let style = if index == selected {
|
|
|
|
|
|
|
|
highlight_style
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
&self.style
|
|
|
|
(format!("{} {}", blank_symbol, item), &self.style)
|
|
|
|
};
|
|
|
|
})
|
|
|
|
buf.set_stringn(x, list_area.top() + i as u16, item, width, style);
|
|
|
|
.skip(offset as usize)
|
|
|
|
}
|
|
|
|
.collect::<Vec<(String, &Style)>>();
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(s) = self.highlight_symbol {
|
|
|
|
// Render items
|
|
|
|
buf.set_string(list_area.left(),
|
|
|
|
List::default()
|
|
|
|
list_area.top() + (selected - offset) as u16,
|
|
|
|
.block(self.block.unwrap_or(Default::default()))
|
|
|
|
s,
|
|
|
|
.items(&items)
|
|
|
|
&self.highlight_style);
|
|
|
|
.draw(area, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|