Skip to content

Commit

Permalink
test 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Reiter committed Jul 28, 2024
1 parent 4050024 commit 8fe94a5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
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.10.1"
version = "0.10.2"
edition = "2021"
authors = ["preiter <[email protected]>"]
description = "Widget List for TUI/Ratatui"
Expand Down
1 change: 1 addition & 0 deletions src/legacy/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{legacy::utils::layout_on_viewport, ListState, PreRender, ScrollAxis}
/// A [`List`] is a widget for Ratatui that can render an arbitrary list of widgets.
/// It is generic over `T`, where each widget `T` should implement the [`PreRender`]
/// trait.
/// `List` is no longer developed. Consider using `ListView`.
#[derive(Clone)]
pub struct List<'a, T: PreRender> {
/// The list's items.
Expand Down
66 changes: 28 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@
//!
//! </div>
//!
//! This crate provides a stateful widget [`List`] implementation for `Ratatui`, enabling listing
//! widgets that implement the [`PreRender`] trait. The associated [`ListState`], offers functionalities
//! such as navigating to the next and previous items.
//! Additionally, the lists support both horizontal and vertical scrolling.
//! This crate provides a stateful widget [`ListView`] implementation for `Ratatui`. The associated [`ListState`], offers functionalities such as navigating to the next and previous items.
//! The list view support both horizontal and vertical scrolling.
//!
//! ## Configuration
//! The [`List`] can be customized with the following options:
//! - [`List::scroll_direction`]: Specifies whether the list is vertically or horizontally scrollable.
//! - [`List::style`]: Defines the base style of the list.
//! - [`List::block`]: Optional outer block surrounding the list.
//! The [`ListView`] can be customized with the following options:
//! - [`ListView::scroll_axis`]: Specifies whether the list is vertically or horizontally scrollable.
//! - [`ListView::style`]: Defines the base style of the list.
//! - [`ListView::block`]: Optional outer block surrounding the list.
//!
//! You can adjust the behavior of [`ListState`] with the following options:
//! - [`ListState::circular`]: Determines if the selection is circular. When enabled, selecting the last item loops back to the first. Enabled by default.
//!
//! ## Example
//! ```
//! use ratatui::prelude::*;
//! use tui_widget_list::{List, ListState, PreRender, PreRenderContext};
//! use tui_widget_list::{ListView, ListState, ListBuilder};
//!
//! #[derive(Debug, Clone)]
//! pub struct ListItem {
Expand All @@ -40,52 +38,44 @@
//! }
//! }
//!
//! impl PreRender for ListItem {
//! fn pre_render(&mut self, context: &PreRenderContext) -> u16 {
//! // Set alternating styles
//! impl Widget for ListItem {
//! fn render(self, area: Rect, buf: &mut Buffer) {
//! Line::from(self.text).style(self.style).render(area, buf);
//! }
//! }
//!
//! pub fn render(f: &mut Frame) {
//! let builder = ListBuilder::new(|context| {
//! let mut item = ListItem::new(&format!("Item {:0}", context.index));
//!
//! // Alternating styles
//! if context.index % 2 == 0 {
//! self.style = Style::default().bg(Color::Rgb(28, 28, 32));
//! item.style = Style::default().bg(Color::Rgb(28, 28, 32));
//! } else {
//! self.style = Style::default().bg(Color::Rgb(0, 0, 0));
//! item.style = Style::default().bg(Color::Rgb(0, 0, 0));
//! }
//!
//! // Highlight the selected widget
//! // Style the selected element
//! if context.is_selected {
//! self.style = Style::default()
//! item.style = Style::default()
//! .bg(Color::Rgb(255, 153, 0))
//! .fg(Color::Rgb(28, 28, 32));
//! };
//!
//! // Example: set main axis size to 1
//! // Return the size of the widget along the main axis.
//! let main_axis_size = 1;
//!
//! main_axis_size
//! }
//! }
//! (item, main_axis_size)
//! });
//!
//! impl Widget for ListItem {
//! fn render(self, area: Rect, buf: &mut Buffer) {
//! Line::from(self.text).style(self.style).render(area, buf);
//! }
//! }
//!
//! pub fn render(f: &mut Frame) {
//! let list = List::new(vec![
//! ListItem::new("Item 1"),
//! ListItem::new("Item 2"),
//! ]);
//! let mut state = ListState::default();
//! let item_count = 2;
//! let list = ListView::new(builder, item_count);
//!
//! f.render_stateful_widget(list, f.size(), &mut state);
//! }
//! ```
//!
//! ## Long lists
//!
//! `tui-widget-list` also allows to render long lists with thousands of items efficiently.
//! Check out the [example](https://github.com/preiter93/tui-widget-list/tree/main/examples/long.rs)
//! for demonstration. Note that the key is to create the items only once and implement `Widget` and
//! `PreRender` on the references to the list item.
//!
//! For more examples see [tui-widget-list](https://github.com/preiter93/tui-widget-list/tree/main/examples).
//!
//! ## Documentation
Expand Down
11 changes: 11 additions & 0 deletions src/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ impl<T> Styled for ListView<'_, T> {
}
}

impl<T: Copy + 'static> From<Vec<T>> for ListView<'_, T> {
fn from(value: Vec<T>) -> Self {
let item_count = value.len();
let builder = ListBuilder::new(move |context| {
return (value[context.index], 1);
});

ListView::new(builder, item_count)
}
}

/// This structure holds information about the item's position, selection
/// status, scrolling behavior, and size along the cross axis.
pub struct ListBuildContext {
Expand Down

0 comments on commit 8fe94a5

Please sign in to comment.