Skip to content

Commit

Permalink
Remove truncate parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
phire22 committed Feb 24, 2024
1 parent c4272e3 commit 40808b8
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 41 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

Released
--------
0.8.0 - 24 February 2024
0.8.1 - 24 February 2024
===================
- Add support for horizontal scroll
- Remove `truncate` option

**Breaking Change**
The ListableWidgets trait method main_axis_size() was renamed to
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tui-widget-list"
version = "0.8.0"
version = "0.8.1"
edition = "2021"
authors = ["preiter <phil.reiter93@gmail.com>"]
description = "Widget List for TUI/Ratatui"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ The [`List`] can be customized with the following options:
- `scroll_direction`: Specifies whether the list is vertically or horizontally scrollable.
- `style`: Defines the base style of the list.
- `block`: Optional outer block surrounding the list.
- `truncate`: Determines whether the first and last elements are truncated to fit the screen. Enabled by default.

You can adjust the behavior of [`ListState`] with the following options:
- `circular`: Determines if the selection is circular. When enabled, selecting the last item loops back to the first. Enabled by default.
Expand Down
3 changes: 1 addition & 2 deletions examples/mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ impl<'a> App<'a> {
];
let list = List::new(items)
.style(Style::default().bg(Color::Black))
.block(Block::default().borders(Borders::ALL).title("Outer block"))
.truncate(true);
.block(Block::default().borders(Borders::ALL).title("Outer block"));
let state = ListState::default();
App { list, state }
}
Expand Down
3 changes: 1 addition & 2 deletions examples/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ impl<'a> App<'a> {
];
let list = List::new(items)
.style(Style::default().bg(Color::Black))
.block(Block::default().borders(Borders::ALL).title("Outer block"))
.truncate(true);
.block(Block::default().borders(Borders::ALL).title("Outer block"));
let state = ListState::default();
App { list, state }
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! - `scroll_direction`: Specifies whether the list is vertically or horizontally scrollable.
//! - `style`: Defines the base style of the list.
//! - `block`: Optional outer block surrounding the list.
//! - `truncate`: Determines whether the first and last elements are truncated to fit the screen. Enabled by default.
//!
//! You can adjust the behavior of [`ListState`] with the following options:
//! - `circular`: Determines if the selection is circular. When enabled, selecting the last item loops back to the first. Enabled by default.
Expand Down
27 changes: 8 additions & 19 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ impl ListState {
/// on the screen and iterate until we have reached the maximum height. If the selected value
/// is within the bounds we do nothing. If the selected value is out of bounds, we adjust the
/// offset accordingly.
pub(crate) fn update_view_port(
&mut self,
heights: &[usize],
max_height: usize,
truncate: bool,
) -> Vec<usize> {
pub(crate) fn update_view_port(&mut self, heights: &[usize], max_height: usize) -> Vec<usize> {
// The items heights on the viewport will be calculated on the fly.
let mut view_heights: Vec<usize> = Vec::new();

Expand All @@ -149,12 +144,10 @@ impl ListState {
for height in heights.iter().skip(self.offset) {
// Out of bounds
if y + height > max_height {
if truncate {
// Truncate the last widget
let dy = max_height - y;
if dy > 0 {
view_heights.push(dy);
}
// Truncate the last widget
let dy = max_height - y;
if dy > 0 {
view_heights.push(dy);
}
break;
}
Expand All @@ -179,12 +172,8 @@ impl ListState {
for height in heights.iter().rev().skip(last.saturating_sub(selected)) {
// out of bounds
if y + height >= max_height {
if truncate {
view_heights.insert(0, max_height - y);
self.offset = i;
} else {
self.offset = i + 1;
}
view_heights.insert(0, max_height - y);
self.offset = i;
break;
}
view_heights.insert(0, *height);
Expand Down Expand Up @@ -223,7 +212,7 @@ mod tests {
};

//when
let heights = given_state.update_view_port(&$given_heights, $given_max_height, true);
let heights = given_state.update_view_port(&$given_heights, $given_max_height);
let offset = given_state.offset;

// then
Expand Down
15 changes: 1 addition & 14 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ pub struct List<'a, T: ListableWidget> {
/// Block surrounding the widget list.
block: Option<Block<'a>>,

/// Truncate widgets to fill the full screen. Defaults to true.
truncate: bool,

/// Specifies the scroll direction.
scroll_direction: ScrollAxis,
}
Expand All @@ -40,7 +37,6 @@ impl<'a, T: ListableWidget> List<'a, T> {
items,
style: Style::default(),
block: None,
truncate: true,
scroll_direction: ScrollAxis::default(),
}
}
Expand All @@ -59,14 +55,6 @@ impl<'a, T: ListableWidget> List<'a, T> {
self
}

/// If `truncate` is true, the list fills the full screen and truncates
/// the first or last item of the list. It is true by default.
#[must_use]
pub fn truncate(mut self, truncate: bool) -> Self {
self.truncate = truncate;
self
}

/// Checks whether the widget list is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -158,8 +146,7 @@ impl<'a, T: ListableWidget> StatefulWidget for List<'a, T> {
// get assigned to. The number of elements in `view_heights` is less than
// the number of elements in `raw_heights` if not all widgets are shown
// on the viewport.
let sizes_scroll_direction =
state.update_view_port(&raw_item_sizes, size_scroll_axis, self.truncate);
let sizes_scroll_direction = state.update_view_port(&raw_item_sizes, size_scroll_axis);

// Drain out elements that are shown on the view port from the vector of
// all elements.
Expand Down

0 comments on commit 40808b8

Please sign in to comment.