Skip to content

Commit 53f333f

Browse files
author
Stephan Dilly
committed
isolate scrolllist widget further
1 parent 80da95b commit 53f333f

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

src/ui/mod.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
mod scrolllist;
22
pub mod style;
33

4-
use scrolllist::ScrollableList;
5-
use style::SharedTheme;
6-
use tui::{
7-
backend::Backend,
8-
layout::{Constraint, Direction, Layout, Rect},
9-
widgets::{Block, Borders, Text},
10-
Frame,
11-
};
4+
pub use scrolllist::draw_list;
5+
use tui::layout::{Constraint, Direction, Layout, Rect};
126

137
/// return the scroll position (line) necessary to have the `selection` in view if it is not already
148
pub fn calc_scroll_top(
@@ -84,26 +78,3 @@ pub fn centered_rect_absolute(
8478
height.min(r.height),
8579
)
8680
}
87-
88-
pub fn draw_list<'b, B: Backend, L>(
89-
f: &mut Frame<B>,
90-
r: Rect,
91-
title: &'b str,
92-
items: L,
93-
select: Option<usize>,
94-
selected: bool,
95-
theme: &SharedTheme,
96-
) where
97-
L: Iterator<Item = Text<'b>>,
98-
{
99-
let list = ScrollableList::new(items)
100-
.block(
101-
Block::default()
102-
.title(title)
103-
.borders(Borders::ALL)
104-
.title_style(theme.title(selected))
105-
.border_style(theme.block(selected)),
106-
)
107-
.scroll(select.unwrap_or_default());
108-
f.render_widget(list, r)
109-
}

src/ui/scrolllist.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
use super::style::SharedTheme;
12
use std::iter::Iterator;
23
use tui::{
4+
backend::Backend,
35
buffer::Buffer,
46
layout::Rect,
57
style::Style,
6-
widgets::{Block, List, Text, Widget},
8+
widgets::{Block, Borders, List, Text, Widget},
9+
Frame,
710
};
811

912
///
10-
pub struct ScrollableList<'b, L>
13+
struct ScrollableList<'b, L>
1114
where
1215
L: Iterator<Item = Text<'b>>,
1316
{
@@ -24,7 +27,7 @@ impl<'b, L> ScrollableList<'b, L>
2427
where
2528
L: Iterator<Item = Text<'b>>,
2629
{
27-
pub fn new(items: L) -> Self {
30+
fn new(items: L) -> Self {
2831
Self {
2932
block: None,
3033
items,
@@ -33,12 +36,12 @@ where
3336
}
3437
}
3538

36-
pub fn block(mut self, block: Block<'b>) -> Self {
39+
fn block(mut self, block: Block<'b>) -> Self {
3740
self.block = Some(block);
3841
self
3942
}
4043

41-
pub fn scroll(mut self, index: usize) -> Self {
44+
fn scroll(mut self, index: usize) -> Self {
4245
self.scroll = index;
4346
self
4447
}
@@ -56,3 +59,26 @@ where
5659
.render(area, buf);
5760
}
5861
}
62+
63+
pub fn draw_list<'b, B: Backend, L>(
64+
f: &mut Frame<B>,
65+
r: Rect,
66+
title: &'b str,
67+
items: L,
68+
select: Option<usize>,
69+
selected: bool,
70+
theme: &SharedTheme,
71+
) where
72+
L: Iterator<Item = Text<'b>>,
73+
{
74+
let list = ScrollableList::new(items)
75+
.block(
76+
Block::default()
77+
.title(title)
78+
.borders(Borders::ALL)
79+
.title_style(theme.title(selected))
80+
.border_style(theme.block(selected)),
81+
)
82+
.scroll(select.unwrap_or_default());
83+
f.render_widget(list, r)
84+
}

0 commit comments

Comments
 (0)