diff --git a/Cargo.toml b/Cargo.toml index 269ddc5..0c1f348 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tui-widget-list" -version = "0.10.1" +version = "0.10.2" edition = "2021" authors = ["preiter "] description = "Widget List for TUI/Ratatui" diff --git a/src/legacy/widget.rs b/src/legacy/widget.rs index b34ca0e..7c991ef 100644 --- a/src/legacy/widget.rs +++ b/src/legacy/widget.rs @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 41e8af6..280816c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,16 +6,14 @@ //! //! //! -//! 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. @@ -23,7 +21,7 @@ //! ## 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 { @@ -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 diff --git a/src/list_view.rs b/src/list_view.rs index 111a0fe..a93afb3 100644 --- a/src/list_view.rs +++ b/src/list_view.rs @@ -83,6 +83,17 @@ impl Styled for ListView<'_, T> { } } +impl From> for ListView<'_, T> { + fn from(value: Vec) -> 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 {