feat(widgets): Add start_corner option to List

This commit is contained in:
Florian Dehau 2017-09-27 08:40:28 +02:00
parent 62df7badf3
commit 5de571fb03
2 changed files with 20 additions and 11 deletions

View file

@ -11,7 +11,7 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::layout::{Direction, Group, Rect, Size}; use tui::layout::{Corner, Direction, Group, Rect, Size};
use tui::style::{Color, Modifier, Style}; use tui::style::{Color, Modifier, Style};
use tui::widgets::{Block, Borders, Item, List, SelectableList, Widget}; use tui::widgets::{Block, Borders, Item, List, SelectableList, Widget};
@ -183,6 +183,7 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
}); });
List::new(events) List::new(events)
.block(Block::default().borders(Borders::ALL).title("List")) .block(Block::default().borders(Borders::ALL).title("List"))
.start_corner(Corner::BottomLeft)
.render(t, &chunks[1]); .render(t, &chunks[1]);
} }
}); });

View file

@ -5,7 +5,7 @@ use std::iter::Iterator;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use buffer::Buffer; use buffer::Buffer;
use layout::Rect; use layout::{Corner, Rect};
use style::Style; use style::Style;
use widgets::{Block, Widget}; use widgets::{Block, Widget};
@ -21,6 +21,7 @@ where
block: Option<Block<'b>>, block: Option<Block<'b>>,
items: L, items: L,
style: Style, style: Style,
start_corner: Corner,
} }
impl<'b, 'i, L, D> Default for List<'b, 'i, L, D> impl<'b, 'i, L, D> Default for List<'b, 'i, L, D>
@ -32,6 +33,7 @@ where
block: None, block: None,
items: L::default(), items: L::default(),
style: Default::default(), style: Default::default(),
start_corner: Corner::TopLeft,
} }
} }
} }
@ -45,6 +47,7 @@ where
block: None, block: None,
items: items, items: items,
style: Default::default(), style: Default::default(),
start_corner: Corner::TopLeft,
} }
} }
@ -65,6 +68,11 @@ where
self.style = style; self.style = style;
self self
} }
pub fn start_corner(&'b mut self, corner: Corner) -> &mut List<'b, 'i, L, D> {
self.start_corner = corner;
self
}
} }
impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D> impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D>
@ -92,24 +100,24 @@ where
.enumerate() .enumerate()
.take(list_area.height as usize) .take(list_area.height as usize)
{ {
let (x, y) = match self.start_corner {
Corner::TopLeft => (list_area.left(), list_area.top() + i as u16),
Corner::BottomLeft => (list_area.left(), list_area.bottom() - (i + 1) as u16),
// Not supported
_ => (list_area.left(), list_area.top() + i as u16),
};
match item { match item {
Item::Data(ref v) => { Item::Data(ref v) => {
buf.set_stringn( buf.set_stringn(
list_area.left(), x,
list_area.top() + i as u16, y,
&format!("{}", v), &format!("{}", v),
list_area.width as usize, list_area.width as usize,
&Style::default(), &Style::default(),
); );
} }
Item::StyledData(ref v, s) => { Item::StyledData(ref v, s) => {
buf.set_stringn( buf.set_stringn(x, y, &format!("{}", v), list_area.width as usize, s);
list_area.left(),
list_area.top() + i as u16,
&format!("{}", v),
list_area.width as usize,
s,
);
} }
}; };
} }