Skip to content

Commit

Permalink
type alias documentation pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wegreenall committed Mar 18, 2024
1 parent cf01321 commit e3b27fb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions inquire/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,40 @@ pub type OptionFormatter<'a, T> = &'a dyn Fn(ListOption<&T>) -> String;
/// ```
pub type MultiOptionFormatter<'a, T> = &'a dyn Fn(&[ListOption<&T>]) -> String;

/// Type alias for formatters used in [`MultiCount`](crate::MultiCount) prompts.
///
/// Formatters receive the user input and return a [(u32, String)] to be displayed
/// to the user as the final answer.
///
/// # Examples
///
/// ```
/// use inquire::list_option::ListOption;
/// use inquire::formatter::MultiCountFormatter;
///
/// let formatter: MultiCountFormatter<str> = &|opts| {
/// let len = opts.len();
/// let options = match len {
/// 1 => "option",
/// _ => "options",
/// };
/// let values = match len {
/// 1 => "value",
/// _ => "values",
/// };
/// let counts = match len{
/// 1 => "count",}
/// _ => "counts",
/// };
/// format!("You selected {} {} with {}: {:?} and {}: {:?}", len, options, value_counts, ans.iter().map(|(c, o)| (c, o.value)).collect::<Vec<_>>(), counts, ans.iter().map(|(c, _)| c).collect::<Vec<_>>()
/// };
///
/// let mut ans = vec![(1, ListOption::new(1, "a"))];
/// assert_eq!(String::from("You selected 1 option with value: ["a"] and count: [1]"), formatter(&ans));
///
/// ans.push((3, ListOption::new(3, "d")));
/// assert_eq!(String::from("You selected 2 options with values: ["a", "d"] and counts: [1, 3]"), formatter(&ans));
/// ```
pub type MultiCountFormatter<'a, T> = &'a dyn Fn(&[(u32, ListOption<&T>)]) -> String;

/// Type alias for formatters used in [`CustomType`](crate::CustomType) prompts.
Expand Down

0 comments on commit e3b27fb

Please sign in to comment.