Skip to content

Commit

Permalink
Merge pull request #38 from ynqa/v0.5.1/dev
Browse files Browse the repository at this point in the history
v0.5.1
  • Loading branch information
ynqa authored Nov 30, 2024
2 parents 1eb3cd6 + 1f105b2 commit f7d917f
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Put the package in your `Cargo.toml`.

```toml
[dependencies]
promkit = "0.5.0"
promkit = "0.5.1"
```

## Features
Expand Down
2 changes: 1 addition & 1 deletion promkit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "promkit"
version = "0.5.0"
version = "0.5.1"
authors = ["ynqa <un.pensiero.vano@gmail.com>"]
edition = "2021"
description = "A toolkit for building your own interactive command-line tools"
Expand Down
25 changes: 14 additions & 11 deletions promkit/src/core/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, fmt, iter::FromIterator};
use std::{collections::HashSet, fmt};

use crate::{core::listbox::Listbox, grapheme::StyledGraphemes};

Expand All @@ -16,20 +16,23 @@ pub struct Checkbox {
picked: HashSet<usize>,
}

impl<T: fmt::Display> FromIterator<T> 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<I: IntoIterator<Item = T>>(iter: I) -> Self {
impl Checkbox {
/// Creates a new `Checkbox` from a vector of `fmt::Display`.
pub fn from_displayable<E: fmt::Display, I: IntoIterator<Item = E>>(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<StyledGraphemes>) -> 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).
Expand Down Expand Up @@ -61,7 +64,7 @@ impl Checkbox {
.collect::<HashSet<usize>>();

Self {
listbox: Listbox::from_iter(listbox_items),
listbox: Listbox::from_displayable(listbox_items),
picked: picked_indices,
}
}
Expand Down
21 changes: 11 additions & 10 deletions promkit/src/core/listbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, iter::FromIterator};
use std::fmt;

use crate::{core::cursor::Cursor, grapheme::StyledGraphemes};

Expand All @@ -20,23 +20,24 @@ impl Default for Listbox {
}
}

impl<T: fmt::Display> FromIterator<T> 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<String>`.
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
impl Listbox {
/// Creates a new `Listbox` from a vector of `fmt::Display`.
pub fn from_displayable<E: fmt::Display, I: IntoIterator<Item = E>>(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<StyledGraphemes>) -> Self {
Self(Cursor::new(items, 0, false))
}

/// Returns a reference to the vector of items in the listbox.
pub fn items(&self) -> &Vec<StyledGraphemes> {
self.0.contents()
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/core/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
18 changes: 9 additions & 9 deletions promkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
//!
//! ```toml
//! [dependencies]
//! promkit = "0.5.0"
//! promkit = "0.5.1"
//! ```
//!
//! ## Features
//!
//! - 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.
Expand All @@ -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*?
//!
Expand Down
4 changes: 2 additions & 2 deletions promkit/src/preset/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Display, I: IntoIterator<Item = T>>(items: I) -> Self {
Self {
title_state: text::State {
Expand All @@ -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: '☐',
Expand Down
4 changes: 2 additions & 2 deletions promkit/src/preset/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Display, I: IntoIterator<Item = T>>(items: I) -> Self {
Self {
title_state: text::State {
Expand All @@ -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()),
Expand Down
10 changes: 5 additions & 5 deletions promkit/src/preset/query_selector.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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<T, I>(items: I, filter: render::Filter) -> Self
where
T: Display,
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/preset/query_selector/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/preset/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Default for Readline {
},
suggest: Default::default(),
suggest_state: listbox::State {
listbox: Listbox::from_iter(Vec::<String>::new()),
listbox: Listbox::from_displayable(Vec::<String>::new()),
cursor: String::from("❯ "),
active_item_style: Some(
StyleBuilder::new()
Expand Down
4 changes: 2 additions & 2 deletions promkit/src/preset/readline/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -279,7 +279,7 @@ pub fn on_suggest(
}

_ => {
suggest_after_mut.listbox = Listbox::from_iter(Vec::<String>::new());
suggest_after_mut.listbox = Listbox::from_displayable(Vec::<String>::new());

renderer.keymap.borrow_mut().switch("default");
}
Expand Down
12 changes: 6 additions & 6 deletions promkit/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ impl<T: ?Sized> ValidatorManager<T> {
/// # 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
///
Expand All @@ -47,7 +47,7 @@ impl<T: ?Sized> ValidatorManager<T> {
/// # Arguments
///
/// * `input` - A reference
/// to the input of type `T` to be validated.
/// to the input of type `T` to be validated.
///
/// # Returns
///
Expand All @@ -63,7 +63,7 @@ impl<T: ?Sized> ValidatorManager<T> {
/// # 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
///
Expand Down

0 comments on commit f7d917f

Please sign in to comment.