File tree 5 files changed +113
-0
lines changed
5 files changed +113
-0
lines changed Original file line number Diff line number Diff line change
1
+ target
2
+ Cargo.lock
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " dialoguer"
3
+ description = " A command line prompting library."
4
+ version = " 0.1.0"
5
+ authors = [
" Armin Ronacher <[email protected] >" ]
6
+ keywords = [" cli" , " menu" , " prompt" ]
7
+ license = " MIT"
8
+ homepage = " https://github.com/mitsuhiko/dialoguer"
9
+ documentation = " https://docs.rs/dialoguer"
10
+ readme = " README.md"
11
+
12
+ [dependencies ]
13
+ console = " 0"
14
+
15
+ [replace ]
16
+ "console:0.1.0" = { path = " ../console" }
Original file line number Diff line number Diff line change
1
+ extern crate dialoguer;
2
+
3
+ use dialoguer:: Confirmation ;
4
+
5
+ fn main ( ) {
6
+ if Confirmation :: new ( "Do you want to continue?" ) . interact ( ) . unwrap ( ) {
7
+ println ! ( "Looks like you want to continue" ) ;
8
+ } else {
9
+ println ! ( "nevermind then :(" ) ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ extern crate console;
2
+
3
+ pub use prompts:: Confirmation ;
4
+
5
+ mod prompts;
Original file line number Diff line number Diff line change
1
+ use std:: io;
2
+
3
+ use console:: Term ;
4
+
5
+ pub struct Confirmation {
6
+ text : String ,
7
+ default : bool ,
8
+ show_default : bool ,
9
+ line_input : bool ,
10
+ clear : bool ,
11
+ }
12
+
13
+ impl Confirmation {
14
+ pub fn new ( text : & str ) -> Confirmation {
15
+ Confirmation {
16
+ text : text. into ( ) ,
17
+ default : true ,
18
+ show_default : true ,
19
+ line_input : false ,
20
+ clear : true ,
21
+ }
22
+ }
23
+
24
+ pub fn use_line_input ( & mut self , val : bool ) -> & mut Confirmation {
25
+ self . line_input = val;
26
+ self
27
+ }
28
+
29
+ pub fn clear ( & mut self , val : bool ) -> & mut Confirmation {
30
+ self . clear = val;
31
+ self
32
+ }
33
+
34
+ pub fn interact ( & self ) -> io:: Result < bool > {
35
+ self . interact_on ( & Term :: stdout ( ) )
36
+ }
37
+
38
+ pub fn interact_on ( & self , term : & Term ) -> io:: Result < bool > {
39
+ let prompt = format ! ( "{}{} " , & self . text, if self . show_default {
40
+ if self . default { " [Y/n]" } else { " [y/N]" }
41
+ } else {
42
+ ""
43
+ } ) ;
44
+
45
+ if !self . line_input {
46
+ term. write_str ( & prompt) ?;
47
+ loop {
48
+ let input = term. read_char ( ) ?;
49
+ let rv = match input {
50
+ 'y' | 'Y' => true ,
51
+ 'n' | 'N' => false ,
52
+ '\n' | '\r' => self . default ,
53
+ _ => { continue ; }
54
+ } ;
55
+ if self . clear {
56
+ term. clear_line ( ) ?;
57
+ } else {
58
+ term. write_line ( "" ) ?;
59
+ }
60
+ return Ok ( rv) ;
61
+ }
62
+ } else {
63
+ loop {
64
+ term. write_str ( & prompt) ?;
65
+ let input = term. read_line ( ) ?;
66
+ let rv = match input. trim ( ) {
67
+ "y" | "Y" => true ,
68
+ "n" | "N" => false ,
69
+ "\n " | "\r " => self . default ,
70
+ _ => { continue ; }
71
+ } ;
72
+ if self . clear {
73
+ term. clear_last_lines ( 1 ) ?;
74
+ }
75
+ return Ok ( rv) ;
76
+ }
77
+ }
78
+ }
79
+ }
You can’t perform that action at this time.
0 commit comments