Skip to content

Commit c417f9c

Browse files
committed
Update docs to use new result & error
1 parent 44cf638 commit c417f9c

File tree

12 files changed

+397
-378
lines changed

12 files changed

+397
-378
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
### Breaking
88

99
* Updated MSRV to `1.59.0`
10+
* Removed deprecated `Confirm::with_text`
11+
* Operations now return `dialouger::Result` instead of `std::io::Result`
1012

1113
## 0.10.4
1214

examples/completion.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use dialoguer::{theme::ColorfulTheme, Completion, Input};
22

3-
fn main() -> Result<(), std::io::Error> {
3+
fn main() {
44
println!("Use the Right arrow or Tab to complete your command");
5+
56
let completion = MyCompletion::default();
7+
68
Input::<String>::with_theme(&ColorfulTheme::default())
79
.with_prompt("dialoguer")
810
.completion_with(&completion)
9-
.interact_text()?;
10-
Ok(())
11+
.interact_text()
12+
.unwrap();
1113
}
1214

1315
struct MyCompletion {

src/edit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::{
22
env,
33
ffi::{OsStr, OsString},
4-
fs, io,
4+
fs,
55
io::{Read, Write},
66
process,
77
};
88

9+
use crate::Result;
10+
911
/// Launches the default editor to edit a string.
1012
///
1113
/// ## Example
@@ -88,7 +90,7 @@ impl Editor {
8890
///
8991
/// Returns `None` if the file was not saved or otherwise the
9092
/// entered text.
91-
pub fn edit(&self, s: &str) -> io::Result<Option<String>> {
93+
pub fn edit(&self, s: &str) -> Result<Option<String>> {
9294
let mut f = tempfile::Builder::new()
9395
.prefix("edit-")
9496
.suffix(&self.extension)

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@
3131
3232
#![deny(clippy::all)]
3333

34+
pub use console;
35+
3436
#[cfg(feature = "completion")]
3537
pub use completion::Completion;
36-
pub use console;
3738
#[cfg(feature = "editor")]
3839
pub use edit::Editor;
3940
pub use error::{Error, Result};
4041
#[cfg(feature = "history")]
4142
pub use history::History;
4243
use paging::Paging;
44+
pub use validate::Validator;
45+
4346
#[cfg(feature = "fuzzy-select")]
4447
pub use prompts::fuzzy_select::FuzzySelect;
4548
#[cfg(feature = "password")]
4649
pub use prompts::password::Password;
4750
pub use prompts::{
4851
confirm::Confirm, input::Input, multi_select::MultiSelect, select::Select, sort::Sort,
4952
};
50-
pub use validate::Validator;
5153

5254
#[cfg(feature = "completion")]
5355
mod completion;

src/paging.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::io;
2-
31
use console::Term;
42

53
use crate::Result;
@@ -45,7 +43,7 @@ impl<'a> Paging<'a> {
4543
}
4644

4745
/// Updates all internal based on the current terminal size and cursor position
48-
pub fn update(&mut self, cursor_pos: usize) -> io::Result<()> {
46+
pub fn update(&mut self, cursor_pos: usize) -> Result {
4947
let new_term_size = self.term.size();
5048

5149
if self.current_term_size != new_term_size {

src/prompts/confirm.rs

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
use std::io;
22

3+
use console::{Key, Term};
4+
35
use crate::{
46
theme::{SimpleTheme, TermThemeRenderer, Theme},
57
Result,
68
};
79

8-
use console::{Key, Term};
9-
1010
/// Renders a confirm prompt.
1111
///
12-
/// ## Example usage
12+
/// ## Example
1313
///
1414
/// ```rust,no_run
15-
/// # fn test() -> Result<(), Box<dyn std::error::Error>> {
1615
/// use dialoguer::Confirm;
1716
///
18-
/// if Confirm::new().with_prompt("Do you want to continue?").interact()? {
19-
/// println!("Looks like you want to continue");
20-
/// } else {
21-
/// println!("nevermind then :(");
17+
/// fn main() {
18+
/// let confirmation = Confirm::new()
19+
/// .with_prompt("Do you want to continue?")
20+
/// .interact()
21+
/// .unwrap();
22+
///
23+
/// if confirmation {
24+
/// println!("Looks like you want to continue");
25+
/// } else {
26+
/// println!("nevermind then :(");
27+
/// }
2228
/// }
23-
/// # Ok(()) } fn main() { test().unwrap(); }
2429
/// ```
2530
pub struct Confirm<'a> {
2631
prompt: String,
@@ -38,7 +43,7 @@ impl Default for Confirm<'static> {
3843
}
3944

4045
impl Confirm<'static> {
41-
/// Creates a confirm prompt.
46+
/// Creates a confirm prompt with default theme.
4247
pub fn new() -> Self {
4348
Self::with_theme(&SimpleTheme)
4449
}
@@ -59,12 +64,6 @@ impl Confirm<'_> {
5964
self
6065
}
6166

62-
#[deprecated(note = "Use with_prompt() instead", since = "0.6.0")]
63-
#[inline]
64-
pub fn with_text(&mut self, text: &str) -> &mut Self {
65-
self.with_prompt(text)
66-
}
67-
6867
/// Sets when to react to user input.
6968
///
7069
/// When `false` (default), we check on each user keystroke immediately as
@@ -114,52 +113,37 @@ impl Confirm<'_> {
114113
///
115114
/// Result contains `Some(bool)` if user answered "yes" or "no" or `Some(default)` (configured in [`default`](Self::default)) if pushes enter,
116115
/// or `None` if user cancelled with 'Esc' or 'q'.
117-
#[inline]
118-
pub fn interact_opt(&self) -> Result<Option<bool>> {
119-
self.interact_on_opt(&Term::stderr())
120-
}
121-
122-
/// Like [interact](#method.interact) but allows a specific terminal to be set.
123116
///
124-
/// ## Examples
117+
/// ## Example
125118
///
126119
/// ```rust,no_run
127120
/// use dialoguer::Confirm;
128-
/// use console::Term;
129121
///
130-
/// # fn main() -> std::io::Result<()> {
131-
/// let proceed = Confirm::new()
132-
/// .with_prompt("Do you wish to continue?")
133-
/// .interact_on(&Term::stderr())?;
134-
/// # Ok(())
135-
/// # }
122+
/// fn main() {
123+
/// let confirmation = Confirm::new()
124+
/// .interact_opt()
125+
/// .unwrap();
126+
///
127+
/// match confirmation {
128+
/// Some(answer) => println!("User answered {}", if answer { "yes" } else { "no " }),
129+
/// None => println!("User did not answer")
130+
/// }
131+
/// }
136132
/// ```
137133
#[inline]
134+
pub fn interact_opt(&self) -> Result<Option<bool>> {
135+
self.interact_on_opt(&Term::stderr())
136+
}
137+
138+
/// Like [`interact`](Self::interact) but allows a specific terminal to be set.
139+
#[inline]
138140
pub fn interact_on(&self, term: &Term) -> Result<bool> {
139141
Ok(self
140142
._interact_on(term, false)?
141143
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
142144
}
143145

144146
/// Like [`interact_opt`](Self::interact_opt) but allows a specific terminal to be set.
145-
///
146-
/// ## Examples
147-
/// ```rust,no_run
148-
/// use dialoguer::Confirm;
149-
/// use console::Term;
150-
///
151-
/// fn main() -> std::io::Result<()> {
152-
/// let confirmation = Confirm::new()
153-
/// .interact_on_opt(&Term::stdout())?;
154-
///
155-
/// match confirmation {
156-
/// Some(answer) => println!("User answered {}", if answer { "yes" } else { "no " }),
157-
/// None => println!("User did not answer")
158-
/// }
159-
///
160-
/// Ok(())
161-
/// }
162-
/// ```
163147
#[inline]
164148
pub fn interact_on_opt(&self, term: &Term) -> Result<Option<bool>> {
165149
self._interact_on(term, true)
@@ -264,19 +248,16 @@ impl Confirm<'_> {
264248
impl<'a> Confirm<'a> {
265249
/// Creates a confirm prompt with a specific theme.
266250
///
267-
/// ## Examples
251+
/// ## Example
252+
///
268253
/// ```rust,no_run
269-
/// use dialoguer::{
270-
/// Confirm,
271-
/// theme::ColorfulTheme
272-
/// };
254+
/// use dialoguer::{theme::ColorfulTheme, Confirm};
273255
///
274-
/// # fn main() -> std::io::Result<()> {
275-
/// let proceed = Confirm::with_theme(&ColorfulTheme::default())
276-
/// .with_prompt("Do you wish to continue?")
277-
/// .interact()?;
278-
/// # Ok(())
279-
/// # }
256+
/// fn main() {
257+
/// let confirmation = Confirm::with_theme(&ColorfulTheme::default())
258+
/// .interact()
259+
/// .unwrap();
260+
/// }
280261
/// ```
281262
pub fn with_theme(theme: &'a dyn Theme) -> Self {
282263
Self {

src/prompts/fuzzy_select.rs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,26 @@ use crate::{
88
Result,
99
};
1010

11-
/// Renders a selection menu that user can fuzzy match to reduce set.
11+
/// Renders a select prompt with fuzzy search.
1212
///
1313
/// User can use fuzzy search to limit selectable items.
1414
/// Interaction returns index of an item selected in the order they appear in `item` invocation or `items` slice.
1515
///
16-
/// ## Examples
16+
/// ## Example
1717
///
1818
/// ```rust,no_run
19-
/// use dialoguer::{
20-
/// FuzzySelect,
21-
/// theme::ColorfulTheme
22-
/// };
23-
/// use console::Term;
19+
/// use dialoguer::FuzzySelect;
2420
///
25-
/// fn main() -> std::io::Result<()> {
26-
/// let items = vec!["Item 1", "item 2"];
27-
/// let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
28-
/// .items(&items)
29-
/// .default(0)
30-
/// .interact_on_opt(&Term::stderr())?;
21+
/// fn main() {
22+
/// let items = vec!["foo", "bar", "baz"];
3123
///
32-
/// match selection {
33-
/// Some(index) => println!("User selected item : {}", items[index]),
34-
/// None => println!("User did not select anything")
35-
/// }
24+
/// let selection = FuzzySelect::new()
25+
/// .with_prompt("What do you choose?")
26+
/// .items(&items)
27+
/// .interact()
28+
/// .unwrap();
3629
///
37-
/// Ok(())
30+
/// println!("You chose: {}", items[selection]);
3831
/// }
3932
/// ```
4033
@@ -59,7 +52,7 @@ impl Default for FuzzySelect<'static> {
5952
}
6053

6154
impl FuzzySelect<'static> {
62-
/// Creates the prompt with a specific text.
55+
/// Creates a fuzzy select prompt with default theme.
6356
pub fn new() -> Self {
6457
Self::with_theme(&SimpleTheme)
6558
}
@@ -138,7 +131,7 @@ impl FuzzySelect<'_> {
138131
/// The user can select the items using 'Enter' and the index of selected item will be returned.
139132
/// The dialog is rendered on stderr.
140133
/// Result contains `index` of selected item if user hit 'Enter'.
141-
/// This unlike [interact_opt](#method.interact_opt) does not allow to quit with 'Esc' or 'q'.
134+
/// This unlike [`interact_opt`](Self::interact_opt) does not allow to quit with 'Esc' or 'q'.
142135
#[inline]
143136
pub fn interact(&self) -> Result<usize> {
144137
self.interact_on(&Term::stderr())
@@ -149,26 +142,45 @@ impl FuzzySelect<'_> {
149142
/// The user can select the items using 'Enter' and the index of selected item will be returned.
150143
/// The dialog is rendered on stderr.
151144
/// Result contains `Some(index)` if user hit 'Enter' or `None` if user cancelled with 'Esc' or 'q'.
145+
///
146+
/// ## Example
147+
///
148+
/// ```rust,no_run
149+
/// use dialoguer::FuzzySelect;
150+
///
151+
/// fn main() {
152+
/// let items = vec!["foo", "bar", "baz"];
153+
///
154+
/// let selection = FuzzySelect::new()
155+
/// .items(&items)
156+
/// .interact_opt()
157+
/// .unwrap();
158+
///
159+
/// match selection {
160+
/// Some(index) => println!("You chose: {}", items[index]),
161+
/// None => println!("You did not choose anything.")
162+
/// }
163+
/// }
164+
/// ```
152165
#[inline]
153166
pub fn interact_opt(&self) -> Result<Option<usize>> {
154167
self.interact_on_opt(&Term::stderr())
155168
}
156169

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

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

171-
/// Like `interact` but allows a specific terminal to be set.
172184
fn _interact_on(&self, term: &Term, allow_quit: bool) -> Result<Option<usize>> {
173185
// Place cursor at the end of the search term
174186
let mut position = self.initial_text.len();
@@ -315,7 +327,20 @@ impl FuzzySelect<'_> {
315327
}
316328

317329
impl<'a> FuzzySelect<'a> {
318-
/// Same as `new` but with a specific theme.
330+
/// Creates a fuzzy select prompt with a specific theme.
331+
///
332+
/// ## Example
333+
///
334+
/// ```rust,no_run
335+
/// use dialoguer::{theme::ColorfulTheme, FuzzySelect};
336+
///
337+
/// fn main() {
338+
/// let selection = FuzzySelect::with_theme(&ColorfulTheme::default())
339+
/// .items(&["foo", "bar", "baz"])
340+
/// .interact()
341+
/// .unwrap();
342+
/// }
343+
/// ```
319344
pub fn with_theme(theme: &'a dyn Theme) -> Self {
320345
Self {
321346
default: None,

0 commit comments

Comments
 (0)