-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ynqa/v0.4.4/main
v0.4.4
- Loading branch information
Showing
12 changed files
with
501 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
Oops, something went wrong.