Skip to content

Commit

Permalink
fix: to build pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cih9088 committed Dec 5, 2024
1 parent 38c911f commit b9eaa95
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 63 deletions.
2 changes: 1 addition & 1 deletion promkit/examples/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> anyhow::Result<()> {
])
.title("What are your favorite fruits?")
.checkbox_lines(5)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use promkit::preset::confirm::Confirm;
use std::io;

fn main() -> anyhow::Result<()> {
let mut p = Confirm::new("Do you have a pet?").prompt(io::stdout())?;
let mut p = Confirm::new("Do you have a pet?").prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
lines: Default::default(),
},
])
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> anyhow::Result<()> {
let mut p = Json::new(stream)
.title("JSON viewer")
.json_lines(5)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io;
fn main() -> anyhow::Result<()> {
let mut p = Listbox::new(0..100)
.title("What number do you like?")
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> {
|text| 4 < text.len() && text.len() < 10,
|text| format!("Length must be over 4 and within 10 but got {}", text.len()),
)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/query_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()> {
})
.title("What number do you like?")
.listbox_lines(5)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> {
|text| text.len() > 10,
|text| format!("Length must be over 10 but got {}", text.len()),
)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
2 changes: 1 addition & 1 deletion promkit/examples/readline_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use promkit::preset::readline::Readline;
use std::io;

fn main() -> anyhow::Result<()> {
let mut p = Readline::default().prompt(io::stdout())?;
let mut p = Readline::default().prompt()?;

loop {
match p.run() {
Expand Down
2 changes: 1 addition & 1 deletion promkit/examples/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> anyhow::Result<()> {
let mut p = Tree::new(Node::try_from(&std::env::current_dir()?.join("src"))?)
.title("Select a directory or file")
.tree_lines(10)
.prompt(io::stdout())?;
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
}
8 changes: 4 additions & 4 deletions promkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ pub trait Renderer: Finalizer {
///
/// This struct encapsulates the rendering logic,
/// event handling, and result production for a prompt.
pub struct Prompt<T: Renderer, W: io::Write> {
pub struct Prompt<T: Renderer> {
pub renderer: T,
pub writer: W,
pub writer: Box<dyn io::Write>,
}

impl<T: Renderer, W: io::Write> Drop for Prompt<T, W> {
impl<T: Renderer> Drop for Prompt<T> {
fn drop(&mut self) {
execute!(
self.writer,
Expand All @@ -239,7 +239,7 @@ impl<T: Renderer, W: io::Write> Drop for Prompt<T, W> {
}
}

impl<T: Renderer, W: io::Write> Prompt<T, W> {
impl<T: Renderer> Prompt<T> {
/// Runs the prompt, handling events and producing a result.
///
/// This method initializes the terminal, and enters a loop
Expand Down
19 changes: 13 additions & 6 deletions promkit/src/preset/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, fmt::Display};
use std::{cell::RefCell, fmt::Display, io};

use crate::{
checkbox,
Expand All @@ -20,6 +20,8 @@ pub struct Checkbox {
title_state: text::State,
/// State for the checkbox list itself.
checkbox_state: checkbox::State,
/// Writer to which promptkit write its contents
writer: Box<dyn io::Write>,
}

impl Checkbox {
Expand Down Expand Up @@ -48,6 +50,7 @@ impl Checkbox {
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
writer: Box::new(io::stdout()),
}
}

Expand All @@ -69,6 +72,7 @@ impl Checkbox {
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
writer: Box::new(io::stdout()),
}
}

Expand Down Expand Up @@ -119,20 +123,23 @@ impl Checkbox {
self
}

/// Sets writer.
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
self.writer = Box::new(writer);
self
}

/// Displays the checkbox prompt and waits for user input.
/// Returns a `Result` containing the `Prompt` result,
/// which is a list of selected options.
pub fn prompt<W: std::io::Write>(
self,
writer: W,
) -> anyhow::Result<Prompt<render::Renderer, W>> {
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
title_snapshot: Snapshot::<text::State>::new(self.title_state),
checkbox_snapshot: Snapshot::<checkbox::State>::new(self.checkbox_state),
},
writer,
writer: self.writer,
})
}
}
21 changes: 15 additions & 6 deletions promkit/src/preset/form.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::RefCell;
use std::{cell::RefCell, io};

use crate::{
core::Cursor,
Expand All @@ -17,6 +17,8 @@ pub struct Form {
text_editor_states: Vec<text_editor::State>,
/// Overwrite the default styles of text editor states when unselected.
overwrite_styles: Vec<render::Style>,
/// Writer to which promptkit write its contents
writer: Box<dyn io::Write>,
}

impl Form {
Expand All @@ -42,13 +44,17 @@ impl Form {
keymap: ActiveKeySwitcher::new("default", self::keymap::default as keymap::Keymap),
text_editor_states,
overwrite_styles,
writer: Box::new(io::stdout()),
}
}

pub fn prompt<W: std::io::Write>(
self,
writer: W,
) -> anyhow::Result<Prompt<render::Renderer, W>> {
/// Sets writer.
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
self.writer = Box::new(writer);
self
}

pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
let default_styles = self
.text_editor_states
.iter()
Expand All @@ -65,6 +71,9 @@ impl Form {
overwrite_styles: self.overwrite_styles,
};
renderer.overwrite_styles();
Ok(Prompt { renderer, writer })
Ok(Prompt {
renderer,
writer: self.writer,
})
}
}
18 changes: 12 additions & 6 deletions promkit/src/preset/json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::RefCell;
use std::{cell::RefCell, io};

use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
Expand All @@ -17,6 +17,8 @@ pub struct Json {
keymap: ActiveKeySwitcher<keymap::Keymap>,
title_state: text::State,
json_state: json::State,
/// Writer to which promptkit write its contents
writer: Box<dyn io::Write>,
}

impl Json {
Expand Down Expand Up @@ -47,6 +49,7 @@ impl Json {
indent: 2,
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
writer: Box::new(io::stdout()),
}
}

Expand Down Expand Up @@ -91,18 +94,21 @@ impl Json {
self
}

/// Sets writer.
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
self.writer = Box::new(writer);
self
}

/// Creates a prompt based on the current configuration of the `Json` instance.
pub fn prompt<W: std::io::Write>(
self,
writer: W,
) -> anyhow::Result<Prompt<render::Renderer, W>> {
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
title_snapshot: Snapshot::<text::State>::new(self.title_state),
json_snapshot: Snapshot::<json::State>::new(self.json_state),
},
writer,
writer: self.writer,
})
}
}
15 changes: 12 additions & 3 deletions promkit/src/preset/listbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, fmt::Display};
use std::{cell::RefCell, fmt::Display, io};

use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
Expand All @@ -19,6 +19,8 @@ pub struct Listbox {
title_state: text::State,
/// State for the selectable list itself.
listbox_state: listbox::State,
/// Writer to which promptkit write its contents
writer: Box<dyn io::Write>,
}

impl Listbox {
Expand All @@ -45,6 +47,7 @@ impl Listbox {
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
writer: Box::new(io::stdout()),
}
}

Expand Down Expand Up @@ -89,17 +92,23 @@ impl Listbox {
self
}

/// Sets writer.
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
self.writer = Box::new(writer);
self
}

/// Displays the select prompt and waits for user input.
/// Returns a `Result` containing the `Prompt` result,
/// which is the selected option.
pub fn prompt<W: std::io::Write>(self, writer: W) -> anyhow::Result<Prompt<render::Renderer, W>> {
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
title_snapshot: Snapshot::<text::State>::new(self.title_state),
listbox_snapshot: Snapshot::<listbox::State>::new(self.listbox_state),
},
writer,
writer: self.writer,
})
}
}
18 changes: 12 additions & 6 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};
use std::{cell::RefCell, fmt::Display, io};

use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
Expand Down Expand Up @@ -28,6 +28,8 @@ pub struct QuerySelector {
/// A filter function to apply to the list box items
/// based on the text editor input.
filter: render::Filter,
/// Writer to which promptkit write its contents
writer: Box<dyn io::Write>,
}

impl QuerySelector {
Expand Down Expand Up @@ -74,6 +76,7 @@ impl QuerySelector {
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
filter,
writer: Box::new(io::stdout()),
}
}

Expand Down Expand Up @@ -154,13 +157,16 @@ impl QuerySelector {
self
}

/// Sets writer.
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
self.writer = Box::new(writer);
self
}

/// Displays the query select prompt and waits for user input.
/// Returns a `Result` containing the `Prompt` result,
/// which is the selected option.
pub fn prompt<W: std::io::Write>(
self,
writer: W,
) -> anyhow::Result<Prompt<render::Renderer, W>> {
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
Expand All @@ -169,7 +175,7 @@ impl QuerySelector {
listbox_snapshot: Snapshot::<listbox::State>::new(self.listbox_state),
filter: self.filter,
},
writer,
writer: self.writer,
})
}
}
Loading

0 comments on commit b9eaa95

Please sign in to comment.