diff --git a/examples/list.rs b/examples/list.rs index 51f9b8b7..13e56fc2 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -11,7 +11,7 @@ use termion::input::TermRead; use tui::Terminal; 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::widgets::{Block, Borders, Item, List, SelectableList, Widget}; @@ -183,6 +183,7 @@ fn draw(t: &mut Terminal, app: &App) { }); List::new(events) .block(Block::default().borders(Borders::ALL).title("List")) + .start_corner(Corner::BottomLeft) .render(t, &chunks[1]); } }); diff --git a/src/widgets/list.rs b/src/widgets/list.rs index c13722bb..ed03d06e 100644 --- a/src/widgets/list.rs +++ b/src/widgets/list.rs @@ -5,7 +5,7 @@ use std::iter::Iterator; use unicode_width::UnicodeWidthStr; use buffer::Buffer; -use layout::Rect; +use layout::{Corner, Rect}; use style::Style; use widgets::{Block, Widget}; @@ -21,6 +21,7 @@ where block: Option>, items: L, style: Style, + start_corner: Corner, } impl<'b, 'i, L, D> Default for List<'b, 'i, L, D> @@ -32,6 +33,7 @@ where block: None, items: L::default(), style: Default::default(), + start_corner: Corner::TopLeft, } } } @@ -45,6 +47,7 @@ where block: None, items: items, style: Default::default(), + start_corner: Corner::TopLeft, } } @@ -65,6 +68,11 @@ where self.style = style; 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> @@ -92,24 +100,24 @@ where .enumerate() .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 { Item::Data(ref v) => { buf.set_stringn( - list_area.left(), - list_area.top() + i as u16, + x, + y, &format!("{}", v), list_area.width as usize, &Style::default(), ); } Item::StyledData(ref v, s) => { - buf.set_stringn( - list_area.left(), - list_area.top() + i as u16, - &format!("{}", v), - list_area.width as usize, - s, - ); + buf.set_stringn(x, y, &format!("{}", v), list_area.width as usize, s); } }; }