From a281d7c81ed6e3f13b69eefe0032dcd019ba2a86 Mon Sep 17 00:00:00 2001 From: ynqa Date: Sat, 30 Nov 2024 23:54:14 +0900 Subject: [PATCH 1/3] chore: bump up version to v0.5.1 --- README.md | 2 +- promkit/Cargo.toml | 2 +- promkit/src/lib.rs | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c86a1b21..9b03d022 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Put the package in your `Cargo.toml`. ```toml [dependencies] -promkit = "0.5.0" +promkit = "0.5.1" ``` ## Features diff --git a/promkit/Cargo.toml b/promkit/Cargo.toml index c3d92d1a..81fb3ae4 100644 --- a/promkit/Cargo.toml +++ b/promkit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "promkit" -version = "0.5.0" +version = "0.5.1" authors = ["ynqa "] edition = "2021" description = "A toolkit for building your own interactive command-line tools" diff --git a/promkit/src/lib.rs b/promkit/src/lib.rs index 2b0515e4..72922934 100644 --- a/promkit/src/lib.rs +++ b/promkit/src/lib.rs @@ -11,7 +11,7 @@ //! //! ```toml //! [dependencies] -//! promkit = "0.5.0" +//! promkit = "0.5.1" //! ``` //! //! ## Features @@ -19,13 +19,13 @@ //! - Support cross-platform both UNIX and Windows owing to [crossterm](https://github.com/crossterm-rs/crossterm) //! - Various building methods //! - Preset; Support for quickly setting up a UI by providing simple parameters. -//! - [Readline](https://github.com/ynqa/promkit/tree/v0.5.0#readline) -//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.5.0#confirm) -//! - [Password](https://github.com/ynqa/promkit/tree/v0.5.0#password) -//! - [Select](https://github.com/ynqa/promkit/tree/v0.5.0#select) -//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.5.0#queryselect) -//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.5.0#checkbox) -//! - [Tree](https://github.com/ynqa/promkit/tree/v0.5.0#tree) +//! - [Readline](https://github.com/ynqa/promkit/tree/v0.5.1#readline) +//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.5.1#confirm) +//! - [Password](https://github.com/ynqa/promkit/tree/v0.5.1#password) +//! - [Select](https://github.com/ynqa/promkit/tree/v0.5.1#select) +//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.5.1#queryselect) +//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.5.1#checkbox) +//! - [Tree](https://github.com/ynqa/promkit/tree/v0.5.1#tree) //! - Combining various UI components. //! - They are provided with the same interface, allowing users to choose and //! assemble them according to their preferences. @@ -39,7 +39,7 @@ //! //! ## Examples/Demos //! -//! See [here](https://github.com/ynqa/promkit/tree/v0.5.0#examplesdemos) +//! See [here](https://github.com/ynqa/promkit/tree/v0.5.1#examplesdemos) //! //! ## Why *promkit*? //! From 0db127f714d949f3f083c2cb4820746669809287 Mon Sep 17 00:00:00 2001 From: ynqa Date: Sun, 1 Dec 2024 00:30:58 +0900 Subject: [PATCH 2/3] fix: split from_displayable and from_styled_graphemes instead of FromIterator to avoid vanishing styles of StyledGraphemes --- promkit/src/core/checkbox.rs | 25 ++++++++++++--------- promkit/src/core/listbox.rs | 21 ++++++++--------- promkit/src/preset/checkbox.rs | 2 +- promkit/src/preset/listbox.rs | 2 +- promkit/src/preset/query_selector.rs | 4 ++-- promkit/src/preset/query_selector/render.rs | 2 +- promkit/src/preset/readline.rs | 2 +- promkit/src/preset/readline/keymap.rs | 4 ++-- 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/promkit/src/core/checkbox.rs b/promkit/src/core/checkbox.rs index 56cd0779..234085d6 100644 --- a/promkit/src/core/checkbox.rs +++ b/promkit/src/core/checkbox.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, fmt, iter::FromIterator}; +use std::{collections::HashSet, fmt}; use crate::{core::listbox::Listbox, grapheme::StyledGraphemes}; @@ -16,20 +16,23 @@ pub struct Checkbox { picked: HashSet, } -impl FromIterator for Checkbox { - /// Creates a `Checkbox` from an iterator of items - /// that implement the `Display` trait. - /// Each item is added to the listbox, - /// and the set of picked indices is initialized as empty. - fn from_iter>(iter: I) -> Self { +impl Checkbox { + /// Creates a new `Checkbox` from a vector of `fmt::Display`. + pub fn from_displayable>(items: I) -> Self { Self { - listbox: Listbox::from_iter(iter), + listbox: Listbox::from_displayable(items), + picked: HashSet::new(), + } + } + + /// Creates a new `Checkbox` from a vector of `StyledGraphemes`. + pub fn from_styled_graphemes(items: Vec) -> Self { + Self { + listbox: Listbox::from_styled_graphemes(items), picked: HashSet::new(), } } -} -impl Checkbox { /// Creates a `Checkbox` from an iterator of tuples where the first element /// implements the `Display` trait and the second element is a bool indicating /// if the item is picked (selected). @@ -61,7 +64,7 @@ impl Checkbox { .collect::>(); Self { - listbox: Listbox::from_iter(listbox_items), + listbox: Listbox::from_displayable(listbox_items), picked: picked_indices, } } diff --git a/promkit/src/core/listbox.rs b/promkit/src/core/listbox.rs index df324227..19b74717 100644 --- a/promkit/src/core/listbox.rs +++ b/promkit/src/core/listbox.rs @@ -1,4 +1,4 @@ -use std::{fmt, iter::FromIterator}; +use std::fmt; use crate::{core::cursor::Cursor, grapheme::StyledGraphemes}; @@ -20,23 +20,24 @@ impl Default for Listbox { } } -impl FromIterator for Listbox { - /// Creates a `Listbox` from an iterator of items - /// that implement the `Display` trait. - /// Each item is converted to a `String` - /// and collected into a `Vec`. - fn from_iter>(iter: I) -> Self { +impl Listbox { + /// Creates a new `Listbox` from a vector of `fmt::Display`. + pub fn from_displayable>(items: I) -> Self { Self(Cursor::new( - iter.into_iter() + items + .into_iter() .map(|e| StyledGraphemes::from(format!("{}", e))) .collect(), 0, false, )) } -} -impl Listbox { + /// Creates a new `Listbox` from a vector of `StyledGraphemes`. + pub fn from_styled_graphemes(items: Vec) -> Self { + Self(Cursor::new(items, 0, false)) + } + /// Returns a reference to the vector of items in the listbox. pub fn items(&self) -> &Vec { self.0.contents() diff --git a/promkit/src/preset/checkbox.rs b/promkit/src/preset/checkbox.rs index 5445b3bd..ab4c97f7 100644 --- a/promkit/src/preset/checkbox.rs +++ b/promkit/src/preset/checkbox.rs @@ -39,7 +39,7 @@ impl Checkbox { .build(), }, checkbox_state: checkbox::State { - checkbox: checkbox::Checkbox::from_iter(items), + checkbox: checkbox::Checkbox::from_displayable(items), cursor: String::from("❯ "), active_mark: '☒', inactive_mark: '☐', diff --git a/promkit/src/preset/listbox.rs b/promkit/src/preset/listbox.rs index b487c46e..4190b4c0 100644 --- a/promkit/src/preset/listbox.rs +++ b/promkit/src/preset/listbox.rs @@ -38,7 +38,7 @@ impl Listbox { .build(), }, listbox_state: listbox::State { - listbox: listbox::Listbox::from_iter(items), + listbox: listbox::Listbox::from_displayable(items), cursor: String::from("❯ "), active_item_style: Some(StyleBuilder::new().fgc(Color::DarkCyan).build()), inactive_item_style: Some(StyleBuilder::new().build()), diff --git a/promkit/src/preset/query_selector.rs b/promkit/src/preset/query_selector.rs index 08ab0810..cf5d0c29 100644 --- a/promkit/src/preset/query_selector.rs +++ b/promkit/src/preset/query_selector.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, fmt::Display, iter::FromIterator}; +use std::{cell::RefCell, fmt::Display}; use crate::{ crossterm::style::{Attribute, Attributes, Color, ContentStyle}, @@ -66,7 +66,7 @@ impl QuerySelector { lines: Default::default(), }, listbox_state: listbox::State { - listbox: Listbox::from_iter(items), + listbox: Listbox::from_displayable(items), cursor: String::from("❯ "), active_item_style: Some(StyleBuilder::new().fgc(Color::DarkCyan).build()), inactive_item_style: Some(StyleBuilder::new().build()), diff --git a/promkit/src/preset/query_selector/render.rs b/promkit/src/preset/query_selector/render.rs index ae9f6783..ab034fed 100644 --- a/promkit/src/preset/query_selector/render.rs +++ b/promkit/src/preset/query_selector/render.rs @@ -71,7 +71,7 @@ impl crate::Renderer for Renderer { .map(|e| e.to_string()) .collect(), ); - self.listbox_snapshot.after_mut().listbox = Listbox::from_iter(list); + self.listbox_snapshot.after_mut().listbox = Listbox::from_displayable(list); } signal } diff --git a/promkit/src/preset/readline.rs b/promkit/src/preset/readline.rs index 7ab513b4..85875c5b 100644 --- a/promkit/src/preset/readline.rs +++ b/promkit/src/preset/readline.rs @@ -61,7 +61,7 @@ impl Default for Readline { }, suggest: Default::default(), suggest_state: listbox::State { - listbox: Listbox::from_iter(Vec::::new()), + listbox: Listbox::from_displayable(Vec::::new()), cursor: String::from("❯ "), active_item_style: Some( StyleBuilder::new() diff --git a/promkit/src/preset/readline/keymap.rs b/promkit/src/preset/readline/keymap.rs index d6701525..e0258ec8 100644 --- a/promkit/src/preset/readline/keymap.rs +++ b/promkit/src/preset/readline/keymap.rs @@ -93,7 +93,7 @@ pub fn default( .text_without_cursor() .to_string(); if let Some(candidates) = suggest.prefix_search(text) { - suggest_after_mut.listbox = Listbox::from_iter(candidates); + suggest_after_mut.listbox = Listbox::from_displayable(candidates); text_editor_after_mut .texteditor .replace(&suggest_after_mut.listbox.get().to_string()); @@ -279,7 +279,7 @@ pub fn on_suggest( } _ => { - suggest_after_mut.listbox = Listbox::from_iter(Vec::::new()); + suggest_after_mut.listbox = Listbox::from_displayable(Vec::::new()); renderer.keymap.borrow_mut().switch("default"); } From 1f105b205f634fd419b19896e4c2dae143e62d72 Mon Sep 17 00:00:00 2001 From: ynqa Date: Sun, 1 Dec 2024 00:33:32 +0900 Subject: [PATCH 3/3] chore: cargo-clippy --- promkit/src/core/text_editor.rs | 2 +- promkit/src/preset/checkbox.rs | 2 +- promkit/src/preset/listbox.rs | 2 +- promkit/src/preset/query_selector.rs | 6 +++--- promkit/src/validate.rs | 12 ++++++------ 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/promkit/src/core/text_editor.rs b/promkit/src/core/text_editor.rs index 61c3b402..acb9ed21 100644 --- a/promkit/src/core/text_editor.rs +++ b/promkit/src/core/text_editor.rs @@ -101,7 +101,7 @@ impl TextEditor { let pos = self.position(); self.0 .contents_mut() - .replace_range(pos..pos + 1, &ch.to_string()); + .replace_range(pos..pos + 1, ch.to_string()); self.forward(); } } diff --git a/promkit/src/preset/checkbox.rs b/promkit/src/preset/checkbox.rs index ab4c97f7..de4c6851 100644 --- a/promkit/src/preset/checkbox.rs +++ b/promkit/src/preset/checkbox.rs @@ -29,7 +29,7 @@ impl Checkbox { /// # Arguments /// /// * `items` - An iterator over items - /// that implement the `Display` trait, to be used as options. + /// that implement the `Display` trait, to be used as options. pub fn new>(items: I) -> Self { Self { title_state: text::State { diff --git a/promkit/src/preset/listbox.rs b/promkit/src/preset/listbox.rs index 4190b4c0..e27f8f79 100644 --- a/promkit/src/preset/listbox.rs +++ b/promkit/src/preset/listbox.rs @@ -28,7 +28,7 @@ impl Listbox { /// # Arguments /// /// * `items` - An iterator over items - /// that implement the `Display` trait, to be used as options. + /// that implement the `Display` trait, to be used as options. pub fn new>(items: I) -> Self { Self { title_state: text::State { diff --git a/promkit/src/preset/query_selector.rs b/promkit/src/preset/query_selector.rs index cf5d0c29..a2c679a0 100644 --- a/promkit/src/preset/query_selector.rs +++ b/promkit/src/preset/query_selector.rs @@ -37,10 +37,10 @@ impl QuerySelector { /// # Arguments /// /// * `items` - An iterator over items that implement the `Display` trait, - /// to be used as options in the list box. + /// to be used as options in the list box. /// * `filter` - A function that takes the current input - /// from the text editor and the list of items, - /// returning a filtered list of items to display. + /// from the text editor and the list of items, + /// returning a filtered list of items to display. pub fn new(items: I, filter: render::Filter) -> Self where T: Display, diff --git a/promkit/src/validate.rs b/promkit/src/validate.rs index 6f62ba8e..fd84a5c3 100644 --- a/promkit/src/validate.rs +++ b/promkit/src/validate.rs @@ -25,11 +25,11 @@ impl ValidatorManager { /// # Arguments /// /// * `validator` - A function that takes a reference - /// to an input of type `T` and returns a boolean - /// indicating whether the input passes the validation. + /// to an input of type `T` and returns a boolean + /// indicating whether the input passes the validation. /// * `error_message_generator` - A function that takes a reference - /// to an input of type `T` and returns a `String` - /// that describes the validation error. + /// to an input of type `T` and returns a `String` + /// that describes the validation error. /// /// # Returns /// @@ -47,7 +47,7 @@ impl ValidatorManager { /// # Arguments /// /// * `input` - A reference - /// to the input of type `T` to be validated. + /// to the input of type `T` to be validated. /// /// # Returns /// @@ -63,7 +63,7 @@ impl ValidatorManager { /// # Arguments /// /// * `input` - A reference to the input of type `T` - /// for which to generate an error message. + /// for which to generate an error message. /// /// # Returns ///