isolate scrolllist widget further

This commit is contained in:
Stephan Dilly 2020-08-27 22:41:09 +02:00
parent 80da95b6f5
commit 53f333f4ca
2 changed files with 33 additions and 36 deletions

View File

@ -1,14 +1,8 @@
mod scrolllist; mod scrolllist;
pub mod style; pub mod style;
use scrolllist::ScrollableList; pub use scrolllist::draw_list;
use style::SharedTheme; use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
widgets::{Block, Borders, Text},
Frame,
};
/// return the scroll position (line) necessary to have the `selection` in view if it is not already /// return the scroll position (line) necessary to have the `selection` in view if it is not already
pub fn calc_scroll_top( pub fn calc_scroll_top(
@ -84,26 +78,3 @@ pub fn centered_rect_absolute(
height.min(r.height), height.min(r.height),
) )
} }
pub fn draw_list<'b, B: Backend, L>(
f: &mut Frame<B>,
r: Rect,
title: &'b str,
items: L,
select: Option<usize>,
selected: bool,
theme: &SharedTheme,
) where
L: Iterator<Item = Text<'b>>,
{
let list = ScrollableList::new(items)
.block(
Block::default()
.title(title)
.borders(Borders::ALL)
.title_style(theme.title(selected))
.border_style(theme.block(selected)),
)
.scroll(select.unwrap_or_default());
f.render_widget(list, r)
}

View File

@ -1,13 +1,16 @@
use super::style::SharedTheme;
use std::iter::Iterator; use std::iter::Iterator;
use tui::{ use tui::{
backend::Backend,
buffer::Buffer, buffer::Buffer,
layout::Rect, layout::Rect,
style::Style, style::Style,
widgets::{Block, List, Text, Widget}, widgets::{Block, Borders, List, Text, Widget},
Frame,
}; };
/// ///
pub struct ScrollableList<'b, L> struct ScrollableList<'b, L>
where where
L: Iterator<Item = Text<'b>>, L: Iterator<Item = Text<'b>>,
{ {
@ -24,7 +27,7 @@ impl<'b, L> ScrollableList<'b, L>
where where
L: Iterator<Item = Text<'b>>, L: Iterator<Item = Text<'b>>,
{ {
pub fn new(items: L) -> Self { fn new(items: L) -> Self {
Self { Self {
block: None, block: None,
items, items,
@ -33,12 +36,12 @@ where
} }
} }
pub fn block(mut self, block: Block<'b>) -> Self { fn block(mut self, block: Block<'b>) -> Self {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn scroll(mut self, index: usize) -> Self { fn scroll(mut self, index: usize) -> Self {
self.scroll = index; self.scroll = index;
self self
} }
@ -56,3 +59,26 @@ where
.render(area, buf); .render(area, buf);
} }
} }
pub fn draw_list<'b, B: Backend, L>(
f: &mut Frame<B>,
r: Rect,
title: &'b str,
items: L,
select: Option<usize>,
selected: bool,
theme: &SharedTheme,
) where
L: Iterator<Item = Text<'b>>,
{
let list = ScrollableList::new(items)
.block(
Block::default()
.title(title)
.borders(Borders::ALL)
.title_style(theme.title(selected))
.border_style(theme.block(selected)),
)
.scroll(select.unwrap_or_default());
f.render_widget(list, r)
}