Skip to content

Commit 79d47a6

Browse files
authored
Merge pull request console-rs#62 from SzaryKot/more_documentation
2 parents a6dc2d3 + 72c6119 commit 79d47a6

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

src/edit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::{
66
process,
77
};
88

9-
/// Launches the default editor edit a string.
9+
/// Launches the default editor to edit a string.
1010
///
11-
/// Example:
11+
/// ## Example
1212
///
1313
/// ```rust,no_run
1414
/// # fn test() -> Result<(), Box<std::error::Error>> {

src/prompts/confirm.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ impl<'a> Confirm<'a> {
3939
}
4040

4141
/// Creates a confirm prompt with a specific theme.
42+
///
43+
/// ## Examples
44+
/// ```rust,no_run
45+
/// use dialoguer::{
46+
/// Confirm,
47+
/// theme::ColorfulTheme
48+
/// };
49+
///
50+
/// # fn main() -> std::io::Result<()> {
51+
/// let proceed = Confirm::with_theme(&ColorfulTheme::default())
52+
/// .with_prompt("Do you wish to continue?")
53+
/// .interact()?;
54+
/// # Ok(())
55+
/// # }
56+
/// ```
4257
pub fn with_theme(theme: &'a dyn Theme) -> Confirm<'a> {
4358
Confirm {
4459
prompt: "".into(),
@@ -60,13 +75,16 @@ impl<'a> Confirm<'a> {
6075
self.with_prompt(text)
6176
}
6277

63-
/// Overrides the default.
78+
/// Overrides the default output if user pushes enter key without inputing any character.
79+
/// Character corresponding to the default choice (e.g `Y` if default is `true`) will be uppercased in the displayed prompt.
80+
///
81+
/// The default output is true.
6482
pub fn default(&mut self, val: bool) -> &mut Confirm<'a> {
6583
self.default = val;
6684
self
6785
}
6886

69-
/// Disables or enables the default value display.
87+
/// Disables or enables display of options user can choose from.
7088
///
7189
/// The default is to append `[y/n]` to the prompt to tell the
7290
/// user which keys to press. This also renders the default choice
@@ -78,13 +96,29 @@ impl<'a> Confirm<'a> {
7896

7997
/// Enables user interaction and returns the result.
8098
///
81-
/// If the user confirms the result is `true`, `false` otherwise.
99+
/// If the user confirms the result is `true`, `false` if declines or default (configured in [default](#method.default)) if pushes enter.
100+
/// Otherwise function discards input waiting for valid one.
101+
///
82102
/// The dialog is rendered on stderr.
83103
pub fn interact(&self) -> io::Result<bool> {
84104
self.interact_on(&Term::stderr())
85105
}
86106

87-
/// Like `interact` but allows a specific terminal to be set.
107+
/// Like [interact](#method.interact) but allows a specific terminal to be set.
108+
///
109+
/// ## Examples
110+
///
111+
/// ```rust,nor_run
112+
/// use dialoguer::Confirm;
113+
/// use console::Term;
114+
///
115+
/// # fn main() -> std::io::Result<()> {
116+
/// let proceed = Confirm::new()
117+
/// .with_prompt("Do you wish to continue?")
118+
/// .interact_on(&Term::stderr())?;
119+
/// # Ok(())
120+
/// # }
121+
/// ```
88122
pub fn interact_on(&self, term: &Term) -> io::Result<bool> {
89123
let mut render = TermThemeRenderer::new(term, self.theme);
90124

src/prompts/input.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ use console::{Key, Term};
1616
/// ## Example usage
1717
///
1818
/// ```rust,no_run
19-
/// # fn test() -> Result<(), Box<std::error::Error>> {
2019
/// use dialoguer::Input;
2120
///
22-
/// let name = Input::<String>::new().with_prompt("Your name").interact()?;
23-
/// println!("Name: {}", name);
24-
/// # Ok(()) } fn main() { test().unwrap(); }
21+
/// let input : String = Input::new()
22+
/// .with_prompt("Tea or coffee?")
23+
/// .with_initial_text("Yes")
24+
/// .default("No".into())
25+
/// .interact_text()?;
26+
/// ```
27+
/// It can also be used with turbofish notation:
28+
///
29+
/// ```rust,no_run
30+
/// let input = Input::<String>::new()
31+
/// .interact_text()?;
2532
/// ```
2633
pub struct Input<'a, T> {
2734
prompt: String,
@@ -72,7 +79,7 @@ where
7279
self
7380
}
7481

75-
/// Sets whether the default can be editable.
82+
/// Sets initial text that user can accept or erase.
7683
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Input<'a, T> {
7784
self.initial_text = Some(val.into());
7885
self
@@ -81,7 +88,7 @@ where
8188
/// Sets a default.
8289
///
8390
/// Out of the box the prompt does not have a default and will continue
84-
/// to display until the user hit enter. If a default is set the user
91+
/// to display until the user inputs something and hits enter. If a default is set the user
8592
/// can instead accept the default with enter.
8693
pub fn default(&mut self, value: T) -> &mut Input<'a, T> {
8794
self.default = Some(value);
@@ -98,8 +105,10 @@ where
98105

99106
/// Disables or enables the default value display.
100107
///
101-
/// The default is to append `[default]` to the prompt to tell the
102-
/// user that a default is acceptable.
108+
/// The default behaviour is to append [`default`] to the prompt to tell the
109+
/// user what is the default value.
110+
///
111+
/// This method does not affect existance of default value, only its display in the prompt!
103112
pub fn show_default(&mut self, val: bool) -> &mut Input<'a, T> {
104113
self.show_default = val;
105114
self

src/prompts/multi_select.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ use crate::theme::{SimpleTheme, TermThemeRenderer, Theme};
55
use console::{Key, Term};
66

77
/// Renders a multi select prompt.
8+
///
9+
/// ## Example usage
10+
/// ```rust,no_run
11+
/// use dialoguer::MultiSelect;
12+
///
13+
/// let items = vec![("Option 1", true), ("Option 2", false)];
14+
/// let chosen : Vec<usize> = MultiSelect::new()
15+
/// .items(&items)
16+
/// .interact()?;
17+
/// ```
818
pub struct MultiSelect<'a> {
919
defaults: Vec<bool>,
1020
items: Vec<String>,
@@ -112,7 +122,7 @@ impl<'a> MultiSelect<'a> {
112122
self.interact_on(&Term::stderr())
113123
}
114124

115-
/// Like `interact` but allows a specific terminal to be set.
125+
/// Like [interact](#method.interact) but allows a specific terminal to be set.
116126
pub fn interact_on(&self, term: &Term) -> io::Result<Vec<usize>> {
117127
let mut page = 0;
118128

src/prompts/sort.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ use crate::theme::{SimpleTheme, TermThemeRenderer, Theme};
55
use console::{Key, Term};
66

77
/// Renders a sort prompt.
8+
///
9+
/// Returns list of indices in original items list sorted according to user input.
10+
///
11+
/// ## Example usage
12+
/// ```rust,no_run
13+
/// use dialoguer::Sort;
14+
///
15+
/// let items_to_order = vec!["Item 1", "Item 2", "Item 3"];
16+
/// let ordered = Sort::new()
17+
/// .with_prompt("Order the items")
18+
/// .items(&items_to_order)
19+
/// .interact()?;
20+
/// ```
821
pub struct Sort<'a> {
922
items: Vec<String>,
1023
prompt: Option<String>,
@@ -44,7 +57,7 @@ impl<'a> Sort<'a> {
4457

4558
/// Sets the clear behavior of the menu.
4659
///
47-
/// The default is to clear the menu.
60+
/// The default is to clear the menu after user interaction.
4861
pub fn clear(&mut self, val: bool) -> &mut Sort<'a> {
4962
self.clear = val;
5063
self
@@ -81,7 +94,7 @@ impl<'a> Sort<'a> {
8194
self.interact_on(&Term::stderr())
8295
}
8396

84-
/// Like `interact` but allows a specific terminal to be set.
97+
/// Like [interact](#method.interact) but allows a specific terminal to be set.
8598
pub fn interact_on(&self, term: &Term) -> io::Result<Vec<usize>> {
8699
let mut page = 0;
87100

src/validate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! Provides validation for text inputs
22
use std::fmt::{Debug, Display};
3+
4+
/// Trait for input validators.
5+
///
6+
/// A generic implementation for `Fn(&str) -> Result<(), E>` is provided
7+
/// to facilitate development.
38
pub trait Validator {
49
type Err: Debug + Display;
510

0 commit comments

Comments
 (0)