Fix List lifetime issues

Add a new lifetime for the items inside the list
This commit is contained in:
Florian Dehau 2017-09-03 13:06:10 +02:00
parent 7749e5ee35
commit d6a91d1865
3 changed files with 72 additions and 68 deletions

View file

@ -363,7 +363,7 @@ fn draw_charts(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
let warning_style = Style::default().fg(Color::Yellow); let warning_style = Style::default().fg(Color::Yellow);
let error_style = Style::default().fg(Color::Magenta); let error_style = Style::default().fg(Color::Magenta);
let critical_style = Style::default().fg(Color::Red); let critical_style = Style::default().fg(Color::Red);
List::new(app.events let events = app.events
.iter() .iter()
.map(|&(evt, level)| { .map(|&(evt, level)| {
Item::StyledData(format!("{}: {}", level, evt), match level { Item::StyledData(format!("{}: {}", level, evt), match level {
@ -372,7 +372,8 @@ fn draw_charts(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
"WARNING" => &warning_style, "WARNING" => &warning_style,
_ => &info_style, _ => &info_style,
}) })
})) });
List::new(events)
.block(Block::default().borders(border::ALL).title("List")) .block(Block::default().borders(border::ALL).title("List"))
.render(t, &chunks[1]); .render(t, &chunks[1]);
}); });

View file

@ -169,7 +169,8 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold)) .highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.highlight_symbol(">") .highlight_symbol(">")
.render(t, &chunks[0]); .render(t, &chunks[0]);
List::new(app.events {
let events = app.events
.iter() .iter()
.map(|&(evt, level)| { .map(|&(evt, level)| {
Item::StyledData(format!("{}: {}", level, evt), match level { Item::StyledData(format!("{}: {}", level, evt), match level {
@ -178,9 +179,11 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
"WARNING" => &app.warning_style, "WARNING" => &app.warning_style,
_ => &app.info_style, _ => &app.info_style,
}) })
})) });
List::new(events)
.block(Block::default().borders(border::ALL).title("List")) .block(Block::default().borders(border::ALL).title("List"))
.render(t, &chunks[1]); .render(t, &chunks[1]);
}
}); });
t.draw().unwrap(); t.draw().unwrap();

View file

@ -9,23 +9,23 @@ use widgets::{Widget, Block};
use layout::Rect; use layout::Rect;
use style::Style; use style::Style;
pub enum Item<'a, D: 'a> { pub enum Item<'i, D: 'i> {
Data(D), Data(D),
StyledData(D, &'a Style), StyledData(D, &'i Style),
} }
pub struct List<'a, L, D: 'a> pub struct List<'b, 'i, L, D: 'i>
where L: Iterator<Item = Item<'a, D>> where L: Iterator<Item = Item<'i, D>>
{ {
block: Option<Block<'a>>, block: Option<Block<'b>>,
items: L, items: L,
style: Style, style: Style,
} }
impl<'a, L, D> Default for List<'a, L, D> impl<'b, 'i, L, D> Default for List<'b, 'i, L, D>
where L: Iterator<Item = Item<'a, D>> + Default where L: Iterator<Item = Item<'i, D>> + Default
{ {
fn default() -> List<'a, L, D> { fn default() -> List<'b, 'i, L, D> {
List { List {
block: None, block: None,
items: L::default(), items: L::default(),
@ -34,10 +34,10 @@ impl<'a, L, D> Default for List<'a, L, D>
} }
} }
impl<'a, L, D> List<'a, L, D> impl<'b, 'i, L, D> List<'b, 'i, L, D>
where L: Iterator<Item = Item<'a, D>> where L: Iterator<Item = Item<'i, D>>
{ {
pub fn new(items: L) -> List<'a, L, D> { pub fn new(items: L) -> List<'b, 'i, L, D> {
List { List {
block: None, block: None,
items: items, items: items,
@ -45,26 +45,26 @@ impl<'a, L, D> List<'a, L, D>
} }
} }
pub fn block(&'a mut self, block: Block<'a>) -> &mut List<'a, L, D> { pub fn block(&'b mut self, block: Block<'b>) -> &mut List<'b, 'i, L, D> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn items<I: IntoIterator<Item = Item<'a, D>>>(&'a mut self, pub fn items<I>(&'b mut self, items: I) -> &mut List<'b, 'i, L, D>
items: L) where I: IntoIterator<Item = Item<'i, D>, IntoIter = L>
-> &mut List<'a, L, D> { {
self.items = items; self.items = items.into_iter();
self self
} }
pub fn style(&'a mut self, style: Style) -> &mut List<'a, L, D> { pub fn style(&'b mut self, style: Style) -> &mut List<'b, 'i, L, D> {
self.style = style; self.style = style;
self self
} }
} }
impl<'a, L, D> Widget for List<'a, L, D> impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D>
where L: Iterator<Item = Item<'a, D>>, where L: Iterator<Item = Item<'i, D>>,
D: Display D: Display
{ {
fn draw(&mut self, area: &Rect, buf: &mut Buffer) { fn draw(&mut self, area: &Rect, buf: &mut Buffer) {
@ -126,10 +126,10 @@ impl<'a, L, D> Widget for List<'a, L, D>
/// .highlight_symbol(">>"); /// .highlight_symbol(">>");
/// # } /// # }
/// ``` /// ```
pub struct SelectableList<'a> { pub struct SelectableList<'b> {
block: Option<Block<'a>>, block: Option<Block<'b>>,
/// Items to be displayed /// Items to be displayed
items: Vec<&'a str>, items: Vec<&'b str>,
/// Index of the one selected /// Index of the one selected
selected: Option<usize>, selected: Option<usize>,
/// Base style of the widget /// Base style of the widget
@ -137,11 +137,11 @@ pub struct SelectableList<'a> {
/// Style used to render selected item /// Style used to render selected item
highlight_style: Style, highlight_style: Style,
/// Symbol in front of the selected item (Shift all items to the right) /// Symbol in front of the selected item (Shift all items to the right)
highlight_symbol: Option<&'a str>, highlight_symbol: Option<&'b str>,
} }
impl<'a> Default for SelectableList<'a> { impl<'b> Default for SelectableList<'b> {
fn default() -> SelectableList<'a> { fn default() -> SelectableList<'b> {
SelectableList { SelectableList {
block: None, block: None,
items: Vec::new(), items: Vec::new(),
@ -153,41 +153,41 @@ impl<'a> Default for SelectableList<'a> {
} }
} }
impl<'a> SelectableList<'a> { impl<'b> SelectableList<'b> {
pub fn block(&'a mut self, block: Block<'a>) -> &mut SelectableList<'a> { pub fn block(&'b mut self, block: Block<'b>) -> &mut SelectableList<'b> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn items<I>(&'a mut self, items: &'a [I]) -> &mut SelectableList<'a> pub fn items<I>(&'b mut self, items: &'b [I]) -> &mut SelectableList<'b>
where I: AsRef<str> + 'a where I: AsRef<str> + 'b
{ {
self.items = items.iter().map(|i| i.as_ref()).collect::<Vec<&str>>(); self.items = items.iter().map(|i| i.as_ref()).collect::<Vec<&str>>();
self self
} }
pub fn style(&'a mut self, style: Style) -> &mut SelectableList<'a> { pub fn style(&'b mut self, style: Style) -> &mut SelectableList<'b> {
self.style = style; self.style = style;
self self
} }
pub fn highlight_symbol(&'a mut self, highlight_symbol: &'a str) -> &mut SelectableList<'a> { pub fn highlight_symbol(&'b mut self, highlight_symbol: &'b str) -> &mut SelectableList<'b> {
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 SelectableList<'a> { pub fn highlight_style(&'b mut self, highlight_style: Style) -> &mut SelectableList<'b> {
self.highlight_style = highlight_style; self.highlight_style = highlight_style;
self self
} }
pub fn select(&'a mut self, index: usize) -> &'a mut SelectableList<'a> { pub fn select(&'b mut self, index: usize) -> &'b mut SelectableList<'b> {
self.selected = Some(index); self.selected = Some(index);
self self
} }
} }
impl<'a> Widget for SelectableList<'a> { impl<'b> Widget for SelectableList<'b> {
fn draw(&mut self, area: &Rect, buf: &mut Buffer) { fn draw(&mut self, area: &Rect, buf: &mut Buffer) {
let list_area = match self.block { let list_area = match self.block {
@ -214,16 +214,16 @@ impl<'a> Widget for SelectableList<'a> {
}; };
// Render items // Render items
List::new(self.items let items = self.items
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, item)| if i == selected { .map(|(i, item)| if i == selected {
Item::StyledData(format!("{} {}", highlight_symbol, item), Item::StyledData(format!("{} {}", highlight_symbol, item), highlight_style)
highlight_style)
} else { } else {
Item::StyledData(format!("{} {}", blank_symbol, item), &self.style) Item::StyledData(format!("{} {}", blank_symbol, item), &self.style)
}) })
.skip(offset as usize)) .skip(offset as usize);
List::new(items)
.block(self.block.unwrap_or_default()) .block(self.block.unwrap_or_default())
.draw(area, buf); .draw(area, buf);
} }