@@ -2,79 +2,55 @@ use std::io;
22use std:: io:: Write ;
33use std:: collections:: BTreeMap ;
44use termcolor:: { Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
5-
6- #[ cfg( target_os = "windows" ) ]
7- pub const INPUT_DEFAULT_LEN : usize = 2 ;
8- #[ cfg( not( target_os = "windows" ) ) ]
9- pub const INPUT_DEFAULT_LEN : usize = 1 ;
5+ use dialoguer:: { Confirmation , Input , Checkboxes , Select , theme:: ColorfulTheme } ;
106
117pub fn progress ( color : Color , first : & str , rest : & str ) -> Result < ( ) , io:: Error > {
12- let mut stdout = StandardStream :: stdout ( ColorChoice :: Always ) ;
13- try! ( stdout . set_color ( ColorSpec :: new ( ) . set_fg ( Some ( color) )
8+ let mut stderr = StandardStream :: stderr ( ColorChoice :: Always ) ;
9+ stderr . set_color ( ColorSpec :: new ( ) . set_fg ( Some ( color) )
1410 . set_intense ( true )
15- . set_bold ( true ) ) ) ;
16- try! ( write ! ( & mut stdout , "{:>12}" , first) ) ;
17- stdout . reset ( ) ?;
18- writeln ! ( & mut stdout , " {}" , rest) ?;
11+ . set_bold ( true ) ) ? ;
12+ write ! ( & mut stderr , "{:>12}" , first) ? ;
13+ stderr . reset ( ) ?;
14+ writeln ! ( & mut stderr , " {}" , rest) ?;
1915 Ok ( ( ) )
2016}
2117
2218pub fn prompt_question ( prompt : & str , default : bool ) -> bool {
23- let mut stdout = StandardStream :: stdout ( ColorChoice :: Always ) ;
24- stdout. set_color ( ColorSpec :: new ( ) . set_fg ( Some ( Color :: Cyan ) ) ) . unwrap ( ) ;
25- write ! ( & mut stdout, "{}? " , prompt) . unwrap ( ) ;
26- stdout. reset ( ) . unwrap ( ) ;
27-
28- print ! ( "({}) " , if default { "yes" } else { "no" } ) ;
29-
30- let _ = io:: stdout ( ) . flush ( ) ;
31- let mut input = String :: new ( ) ;
32-
33- fn parse ( it : & str ) -> bool {
34- let lower = it. to_lowercase ( ) ;
35-
36- if lower == "y" || lower == "yes" {
37- return true ;
38- }
39-
40- false
41- }
42-
43- match io:: stdin ( ) . read_line ( & mut input) {
44- Ok ( n) => {
45- match n {
46- 0 => false ,
47- INPUT_DEFAULT_LEN => default,
48- _ => parse ( input. trim ( ) )
49- }
50- }
51- Err ( error) => panic ! ( error)
52- }
19+ Confirmation :: with_theme ( & ColorfulTheme :: default ( ) )
20+ . with_text ( prompt)
21+ . default ( default)
22+ . interact ( )
23+ . unwrap_or ( default)
5324}
5425
5526pub fn prompt_line ( prompt : & str , default : & str ) -> Option < String > {
56- let mut stdout = StandardStream :: stdout ( ColorChoice :: Always ) ;
57- stdout. set_color ( ColorSpec :: new ( ) . set_fg ( Some ( Color :: Cyan ) ) ) . unwrap ( ) ;
58- write ! ( & mut stdout, "{}: " , prompt) . unwrap ( ) ;
59- stdout. reset ( ) . unwrap ( ) ;
60-
61- if default != "" {
62- print ! ( "({}) " , default ) ;
63- }
64-
65- let _ = io:: stdout ( ) . flush ( ) ;
66- let mut input = String :: new ( ) ;
27+ Some ( Input :: with_theme ( & ColorfulTheme :: default ( ) )
28+ . with_prompt ( prompt)
29+ . default ( default. to_string ( ) )
30+ . interact ( )
31+ . unwrap_or ( default. to_string ( ) )
32+ . to_string ( ) )
33+ }
6734
68- match io:: stdin ( ) . read_line ( & mut input) {
69- Ok ( n) => {
70- match n {
71- 0 => None ,
72- INPUT_DEFAULT_LEN => Some ( default. to_owned ( ) ) ,
73- _ => Some ( input. trim ( ) . to_owned ( ) )
74- }
75- }
76- Err ( error) => panic ! ( error)
77- }
35+ pub fn prompt_multi_select ( prompt : & str , options : & [ & str ] ) -> Vec < String > {
36+ Checkboxes :: with_theme ( & ColorfulTheme :: default ( ) )
37+ . with_prompt ( prompt)
38+ . items ( options)
39+ . interact ( )
40+ . unwrap_or ( vec ! [ ] )
41+ . into_iter ( )
42+ . map ( |i| options[ i] . to_string ( ) )
43+ . collect ( )
44+ }
45+
46+ pub fn prompt_select ( prompt : & str , options : & [ String ] , default : usize ) -> String {
47+ Select :: with_theme ( & ColorfulTheme :: default ( ) )
48+ . with_prompt ( prompt)
49+ . items ( options)
50+ . default ( default)
51+ . interact ( )
52+ . map ( |i| options[ i] . to_string ( ) )
53+ . unwrap_or_else ( |_| options[ default] . to_string ( ) )
7854}
7955
8056pub fn parse_platform_list ( vec : & [ String ] ) -> BTreeMap < String , String > {
0 commit comments