Skip to content

Commit

Permalink
Better builder signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 11, 2023
1 parent 70d9105 commit f92f147
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
@@ -1 +1 @@
msrv = "1.56.0"
msrv = "1.59.0"
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,11 +4,16 @@

### Enhancements

* Added `dialouger::Result` and `dialouger::Error`

### Breaking

* Updated MSRV to `1.59.0`
* Removed deprecated `Confirm::with_text`
* Operations now return `dialouger::Result` instead of `std::io::Result`
* Prompt builder functions now take `mut self` instead of `&mut self`
* Prompt builder functions now return `Self` instead of `&mut Self`
* Prompt interaction functions now take `self` instead of `&self`
* Prompt interaction functions and other operations now return `dialouger::Result` instead of `std::io::Result`

## 0.10.4

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -41,7 +41,7 @@ name = "editor"
required-features = ["editor"]

[[example]]
name = "fuzzyselect"
name = "fuzzy_select"
required-features = ["fuzzy-select"]

[[example]]
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions src/prompts/confirm.rs
Expand Up @@ -51,15 +51,15 @@ impl Confirm<'static> {

impl Confirm<'_> {
/// Sets the confirm prompt.
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}

/// Indicates whether or not to report the chosen selection after interaction.
///
/// The default is to report the chosen selection.
pub fn report(&mut self, val: bool) -> &mut Self {
pub fn report(mut self, val: bool) -> Self {
self.report = val;
self
}
Expand All @@ -73,7 +73,7 @@ impl Confirm<'_> {
/// When `true`, the user must type their choice and hit the Enter key before
/// proceeding. Valid inputs can be "yes", "no", "y", "n", or an empty string
/// to accept the default.
pub fn wait_for_newline(&mut self, wait: bool) -> &mut Self {
pub fn wait_for_newline(mut self, wait: bool) -> Self {
self.wait_for_newline = wait;
self
}
Expand All @@ -83,15 +83,15 @@ impl Confirm<'_> {
/// Out of the box the prompt does not have a default and will continue
/// to display until the user inputs something and hits enter. If a default is set the user
/// can instead accept the default with enter.
pub fn default(&mut self, val: bool) -> &mut Self {
pub fn default(mut self, val: bool) -> Self {
self.default = Some(val);
self
}

/// Disables or enables the default value display.
///
/// The default is to append the default value to the prompt to tell the user.
pub fn show_default(&mut self, val: bool) -> &mut Self {
pub fn show_default(mut self, val: bool) -> Self {
self.show_default = val;
self
}
Expand All @@ -103,7 +103,7 @@ impl Confirm<'_> {
/// Result contains `bool` if user answered "yes" or "no" or `default` (configured in [`default`](Self::default) if pushes enter.
/// This unlike [`interact_opt`](Self::interact_opt) does not allow to quit with 'Esc' or 'q'.
#[inline]
pub fn interact(&self) -> Result<bool> {
pub fn interact(self) -> Result<bool> {
self.interact_on(&Term::stderr())
}

Expand Down Expand Up @@ -131,25 +131,25 @@ impl Confirm<'_> {
/// }
/// ```
#[inline]
pub fn interact_opt(&self) -> Result<Option<bool>> {
pub fn interact_opt(self) -> Result<Option<bool>> {
self.interact_on_opt(&Term::stderr())
}

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
#[inline]
pub fn interact_on(&self, term: &Term) -> Result<bool> {
pub fn interact_on(self, term: &Term) -> Result<bool> {
Ok(self
._interact_on(term, false)?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
}

/// Like [`interact_opt`](Self::interact_opt) but allows a specific terminal to be set.
#[inline]
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<bool>> {
pub fn interact_on_opt(self, term: &Term) -> Result<Option<bool>> {
self._interact_on(term, true)
}

fn _interact_on(&self, term: &Term, allow_quit: bool) -> Result<Option<bool>> {
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<bool>> {
let mut render = TermThemeRenderer::new(term, self.theme);

let default_if_show = if self.show_default {
Expand Down
28 changes: 14 additions & 14 deletions src/prompts/fuzzy_select.rs
Expand Up @@ -62,33 +62,33 @@ impl FuzzySelect<'_> {
/// Sets the clear behavior of the menu.
///
/// The default is to clear the menu.
pub fn clear(&mut self, val: bool) -> &mut Self {
pub fn clear(mut self, val: bool) -> Self {
self.clear = val;
self
}

/// Sets a default for the menu
pub fn default(&mut self, val: usize) -> &mut Self {
pub fn default(mut self, val: usize) -> Self {
self.default = Some(val);
self
}

/// Add a single item to the fuzzy selector.
pub fn item<T: ToString>(&mut self, item: T) -> &mut Self {
pub fn item<T: ToString>(mut self, item: T) -> Self {
self.items.push(item.to_string());
self
}

/// Adds multiple items to the fuzzy selector.
pub fn items<T: ToString>(&mut self, items: &[T]) -> &mut Self {
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
for item in items {
self.items.push(item.to_string());
}
self
}

/// Sets the search text that a fuzzy search starts with.
pub fn with_initial_text<S: Into<String>>(&mut self, initial_text: S) -> &mut Self {
pub fn with_initial_text<S: Into<String>>(mut self, initial_text: S) -> Self {
self.initial_text = initial_text.into();
self
}
Expand All @@ -97,31 +97,31 @@ impl FuzzySelect<'_> {
///
/// When a prompt is set the system also prints out a confirmation after
/// the fuzzy selection.
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}

/// Indicates whether to report the selected value after interaction.
///
/// The default is to report the selection.
pub fn report(&mut self, val: bool) -> &mut Self {
pub fn report(mut self, val: bool) -> Self {
self.report = val;
self
}

/// Indicates whether to highlight matched indices
///
/// The default is to highlight the indices
pub fn highlight_matches(&mut self, val: bool) -> &mut Self {
pub fn highlight_matches(mut self, val: bool) -> Self {
self.highlight_matches = val;
self
}

/// Sets the maximum number of visible options.
///
/// The default is the height of the terminal minus 2.
pub fn max_length(&mut self, rows: usize) -> &mut Self {
pub fn max_length(mut self, rows: usize) -> Self {
self.max_length = Some(rows);
self
}
Expand All @@ -133,7 +133,7 @@ impl FuzzySelect<'_> {
/// Result contains `index` of selected item if user hit 'Enter'.
/// This unlike [`interact_opt`](Self::interact_opt) does not allow to quit with 'Esc' or 'q'.
#[inline]
pub fn interact(&self) -> Result<usize> {
pub fn interact(self) -> Result<usize> {
self.interact_on(&Term::stderr())
}

Expand Down Expand Up @@ -163,25 +163,25 @@ impl FuzzySelect<'_> {
/// }
/// ```
#[inline]
pub fn interact_opt(&self) -> Result<Option<usize>> {
pub fn interact_opt(self) -> Result<Option<usize>> {
self.interact_on_opt(&Term::stderr())
}

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
#[inline]
pub fn interact_on(&self, term: &Term) -> Result<usize> {
pub fn interact_on(self, term: &Term) -> Result<usize> {
Ok(self
._interact_on(term, false)?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
}

/// Like [`interact_opt`](Self::interact_opt) but allows a specific terminal to be set.
#[inline]
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<usize>> {
pub fn interact_on_opt(self, term: &Term) -> Result<Option<usize>> {
self._interact_on(term, true)
}

fn _interact_on(&self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
fn _interact_on(self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
// Place cursor at the end of the search term
let mut position = self.initial_text.len();
let mut search_term = self.initial_text.to_owned();
Expand Down
33 changes: 15 additions & 18 deletions src/prompts/input.rs
Expand Up @@ -74,30 +74,27 @@ impl<T> Input<'_, T> {
}

/// Sets the input prompt.
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}

/// Changes the prompt text to the post completion text after input is complete
pub fn with_post_completion_text<S: Into<String>>(
&mut self,
post_completion_text: S,
) -> &mut Self {
pub fn with_post_completion_text<S: Into<String>>(mut self, post_completion_text: S) -> Self {
self.post_completion_text = Some(post_completion_text.into());
self
}

/// Indicates whether to report the input value after interaction.
///
/// The default is to report the input value.
pub fn report(&mut self, val: bool) -> &mut Self {
pub fn report(mut self, val: bool) -> Self {
self.report = val;
self
}

/// Sets initial text that user can accept or erase.
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Self {
pub fn with_initial_text<S: Into<String>>(mut self, val: S) -> Self {
self.initial_text = Some(val.into());
self
}
Expand All @@ -107,15 +104,15 @@ impl<T> Input<'_, T> {
/// Out of the box the prompt does not have a default and will continue
/// to display until the user inputs something and hits enter. If a default is set the user
/// can instead accept the default with enter.
pub fn default(&mut self, value: T) -> &mut Self {
pub fn default(mut self, value: T) -> Self {
self.default = Some(value);
self
}

/// Enables or disables an empty input
///
/// By default, if there is no default value set for the input, the user must input a non-empty string.
pub fn allow_empty(&mut self, val: bool) -> &mut Self {
pub fn allow_empty(mut self, val: bool) -> Self {
self.permit_empty = val;
self
}
Expand All @@ -126,7 +123,7 @@ impl<T> Input<'_, T> {
/// user what is the default value.
///
/// This method does not affect existence of default value, only its display in the prompt!
pub fn show_default(&mut self, val: bool) -> &mut Self {
pub fn show_default(mut self, val: bool) -> Self {
self.show_default = val;
self
}
Expand Down Expand Up @@ -189,7 +186,7 @@ impl<'a, T> Input<'a, T> {
/// self.history.get(pos).cloned()
/// }
///
/// fn write(&mut self, val: &T)
/// fn write(mut self, val: &T)

Check failure on line 189 in src/prompts/input.rs

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, x86_64-unknown-linux-gnu, 1.59.0)

method `write` has an incompatible type for trait

Check failure on line 189 in src/prompts/input.rs

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, x86_64-unknown-linux-gnu, stable)

method `write` has an incompatible type for trait

Check failure on line 189 in src/prompts/input.rs

View workflow job for this annotation

GitHub Actions / Tests (macos-latest, x86_64-apple-darwin, 1.59.0)

method `write` has an incompatible type for trait

Check failure on line 189 in src/prompts/input.rs

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, x86_64-pc-windows-msvc, 1.59.0)

method `write` has an incompatible type for trait
/// where
/// {
/// self.history.push_front(val.to_string());
Expand All @@ -206,7 +203,7 @@ impl<'a, T> Input<'a, T> {
/// }
/// ```
#[cfg(feature = "history")]
pub fn history_with<H>(&mut self, history: &'a mut H) -> &mut Self
pub fn history_with<H>(mut self, history: &'a mut H) -> Self
where
H: History<T>,
{
Expand All @@ -216,7 +213,7 @@ impl<'a, T> Input<'a, T> {

/// Enable completion
#[cfg(feature = "completion")]
pub fn completion_with<C>(&mut self, completion: &'a C) -> &mut Self
pub fn completion_with<C>(mut self, completion: &'a C) -> Self
where
C: Completion,
{
Expand Down Expand Up @@ -250,7 +247,7 @@ where
/// .unwrap();
/// }
/// ```
pub fn validate_with<V>(&mut self, mut validator: V) -> &mut Self
pub fn validate_with<V>(mut self, mut validator: V) -> Self
where
V: Validator<T> + 'a,
V::Err: ToString,
Expand Down Expand Up @@ -285,12 +282,12 @@ where
/// while [`interact`](Self::interact) allows virtually any character to be used e.g arrow keys.
///
/// The dialog is rendered on stderr.
pub fn interact_text(&mut self) -> Result<T> {
pub fn interact_text(self) -> Result<T> {
self.interact_text_on(&Term::stderr())
}

/// Like [`interact_text`](Self::interact_text) but allows a specific terminal to be set.
pub fn interact_text_on(&mut self, term: &Term) -> Result<T> {
pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
let mut render = TermThemeRenderer::new(term, self.theme);

loop {
Expand Down Expand Up @@ -636,12 +633,12 @@ where
///
/// If the user confirms the result is `true`, `false` otherwise.
/// The dialog is rendered on stderr.
pub fn interact(&mut self) -> Result<T> {
pub fn interact(self) -> Result<T> {
self.interact_on(&Term::stderr())
}

/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
pub fn interact_on(&mut self, term: &Term) -> Result<T> {
pub fn interact_on(mut self, term: &Term) -> Result<T> {
let mut render = TermThemeRenderer::new(term, self.theme);

loop {
Expand Down

0 comments on commit f92f147

Please sign in to comment.