Skip to content

Commit

Permalink
Merge pull request #26 from ynqa/v0.4.4/main
Browse files Browse the repository at this point in the history
v0.4.4
  • Loading branch information
ynqa authored Jun 7, 2024
2 parents a0933c9 + 2aaeeff commit 9f1fd2c
Show file tree
Hide file tree
Showing 12 changed files with 501 additions and 11 deletions.
69 changes: 68 additions & 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.4.3"
promkit = "0.4.4"
```

## Features
Expand All @@ -23,6 +23,7 @@ promkit = "0.4.3"
- [Readline](#readline)
- [Confirm](#confirm)
- [Password](#password)
- [Form](#form)
- [Listbox](#listbox)
- [QuerySelector](#queryselector)
- [Checkbox](#checkbox)
Expand Down Expand Up @@ -147,6 +148,72 @@ fn main() -> Result {

![password](https://github.com/ynqa/promkit/assets/6745370/396356ef-47de-44bc-a8d4-d03c7ac66a2f)

### Form

<details>
<summary>Command</summary>

```bash
cargo run --example form
```

</details>

<details>
<summary>Code</summary>

```rust
use promkit::{crossterm::style::Color, preset::form::Form, style::StyleBuilder, text_editor};

fn main() -> anyhow::Result<()> {
let mut p = Form::new([
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkRed).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkGreen).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkBlue).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
])
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
```

</details>

![form](https://github.com/ynqa/promkit/assets/6745370/c3dc88a7-d0f0-42f4-90b8-bc4d2e23e36d)

### Listbox

<details>
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.4.3"
version = "0.4.4"
authors = ["ynqa <[email protected]>"]
edition = "2021"
description = "A toolkit for building your own interactive command-line tools"
Expand Down
45 changes: 45 additions & 0 deletions promkit/examples/form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use promkit::{crossterm::style::Color, preset::form::Form, style::StyleBuilder, text_editor};

fn main() -> anyhow::Result<()> {
let mut p = Form::new([
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkRed).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkGreen).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
text_editor::State {
texteditor: Default::default(),
history: Default::default(),
prefix: String::from("❯❯ "),
mask: Default::default(),
prefix_style: StyleBuilder::new().fgc(Color::DarkBlue).build(),
active_char_style: StyleBuilder::new().bgc(Color::DarkCyan).build(),
inactive_char_style: StyleBuilder::new().build(),
edit_mode: Default::default(),
word_break_chars: Default::default(),
lines: Default::default(),
},
])
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
1 change: 1 addition & 0 deletions promkit/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod checkbox;
mod cursor;
pub use cursor::Cursor;
pub mod json;
pub mod listbox;
pub mod snapshot;
Expand Down
7 changes: 7 additions & 0 deletions promkit/src/core/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ impl Default for TextEditor {
}

impl TextEditor {
pub fn new<S: AsRef<str>>(s: S) -> Self {
let mut buf = s.as_ref().to_owned();
buf.push(' ');
let pos = buf.len() - 1;
Self(Cursor::new(StyledGraphemes::from(buf), pos, false))
}

/// Returns the current text including the cursor.
pub fn text(&self) -> StyledGraphemes {
self.0.contents().clone()
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.4.3"
//! promkit = "0.4.4"
//! ```
//!
//! ## 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.4.3#readline)
//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.4.3#confirm)
//! - [Password](https://github.com/ynqa/promkit/tree/v0.4.3#password)
//! - [Select](https://github.com/ynqa/promkit/tree/v0.4.3#select)
//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.4.3#queryselect)
//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.4.3#checkbox)
//! - [Tree](https://github.com/ynqa/promkit/tree/v0.4.3#tree)
//! - [Readline](https://github.com/ynqa/promkit/tree/v0.4.4#readline)
//! - [Confirm](https://github.com/ynqa/promkit/tree/v0.4.4#confirm)
//! - [Password](https://github.com/ynqa/promkit/tree/v0.4.4#password)
//! - [Select](https://github.com/ynqa/promkit/tree/v0.4.4#select)
//! - [QuerySelect](https://github.com/ynqa/promkit/tree/v0.4.4#queryselect)
//! - [Checkbox](https://github.com/ynqa/promkit/tree/v0.4.4#checkbox)
//! - [Tree](https://github.com/ynqa/promkit/tree/v0.4.4#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.4.3#examplesdemos)
//! See [here](https://github.com/ynqa/promkit/tree/v0.4.4#examplesdemos)
//!
//! ## Why *promkit*?
//!
Expand Down
2 changes: 2 additions & 0 deletions promkit/src/preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ pub mod query_selector;

/// Supports creating and interacting with a tree structure for hierarchical data.
pub mod tree;

pub mod form;
67 changes: 67 additions & 0 deletions promkit/src/preset/form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::cell::RefCell;

use crate::{
core::Cursor,
crossterm::style::{Attribute, Attributes},
style::StyleBuilder,
switch::ActiveKeySwitcher,
text_editor, Prompt,
};

mod keymap;
mod render;

/// `Form` struct provides functionality for managing multiple text input fields.
pub struct Form {
keymap: ActiveKeySwitcher<keymap::Keymap>,
text_editor_states: Vec<text_editor::State>,
/// Overwrite the default styles of text editor states when unselected.
overwrite_styles: Vec<render::Style>,
}

impl Form {
pub fn new<I: IntoIterator<Item = text_editor::State>>(states: I) -> Self {
let (text_editor_states, overwrite_styles): (Vec<_>, Vec<_>) = states
.into_iter()
.map(|state| {
let style = render::Style {
prefix_style: StyleBuilder::from(state.prefix_style)
.attrs(Attributes::from(Attribute::Dim))
.build(),
inactive_char_style: StyleBuilder::from(state.inactive_char_style)
.attrs(Attributes::from(Attribute::Dim))
.build(),
active_char_style: StyleBuilder::new()
.attrs(Attributes::from(Attribute::Dim))
.build(),
};
(state, style)
})
.unzip();
Self {
keymap: ActiveKeySwitcher::new("default", self::keymap::default as keymap::Keymap),
text_editor_states,
overwrite_styles,
}
}

pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
let default_styles = self
.text_editor_states
.iter()
.map(|state| render::Style {
prefix_style: state.prefix_style,
active_char_style: state.active_char_style,
inactive_char_style: state.inactive_char_style,
})
.collect();
let mut renderer = render::Renderer {
keymap: RefCell::new(self.keymap),
text_editor_states: Cursor::new(self.text_editor_states, 0, false),
default_styles,
overwrite_styles: self.overwrite_styles,
};
renderer.overwrite_styles();
Ok(Prompt { renderer })
}
}
Loading

0 comments on commit 9f1fd2c

Please sign in to comment.