@@ -45,50 +45,69 @@ impl App {
4545 }
4646 }
4747
48+ // begin_converting_progress_bar beginning displaying the progress bar on the screen, and
49+ // return a function to stop the progress bar.
50+ fn begin_converting_progress_bar ( ) -> impl FnOnce ( ) {
51+ let spinner_style = ProgressStyle :: with_template ( "{prefix:.bold.dim} {spinner} {wide_msg}" )
52+ . unwrap ( )
53+ . tick_chars ( "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈✔" ) ;
54+ let progress_bar = ProgressBar :: new_spinner ( ) ;
55+ progress_bar. set_style ( spinner_style) ;
56+ progress_bar. set_prefix ( "Converting" ) ;
57+ progress_bar. enable_steady_tick ( std:: time:: Duration :: from_millis ( 25 ) ) ;
58+ move || {
59+ progress_bar. finish_and_clear ( ) ;
60+ }
61+ }
62+
4863 pub fn run ( & mut self ) {
4964 loop {
5065 match & self . status {
51- Status :: Begin ( hello ) => {
52- println ! ( "{}" , hello ) ;
66+ Status :: Begin ( hello_msg ) => {
67+ println ! ( "{}" , hello_msg ) ;
5368 self . status = Status :: WaitingText ;
5469 }
5570 Status :: WaitingText => {
5671 let question = Text :: new ( "Text: " )
57- . with_help_message ( "Input 'exit' or ' quit' to end the program." )
72+ . with_help_message ( "Input 'quit' to quit the program." )
5873 . prompt ( )
5974 . unwrap ( ) ;
6075
61- if question == "exit" || question == " quit" {
76+ if question == "quit" {
6277 self . status = Status :: End ;
6378 continue ;
6479 }
6580
66- let spinner_style =
67- ProgressStyle :: with_template ( "{prefix:.bold.dim} {spinner} {wide_msg}" )
68- . unwrap ( )
69- . tick_chars ( "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈✔" ) ;
70- let progress_bar = ProgressBar :: new_spinner ( ) ;
71- progress_bar. set_style ( spinner_style) ;
72- progress_bar. enable_steady_tick ( std:: time:: Duration :: from_millis ( 25 ) ) ;
73- progress_bar. set_prefix ( "Converting" ) ;
81+ let progress_bar_stopper = Self :: begin_converting_progress_bar ( ) ;
7482
7583 let detail = self . converter . convert ( & question) ;
76- progress_bar. finish_and_clear ( ) ;
77- if let Err ( e) = detail {
78- println ! ( "Error: {}" , e. to_string( ) . red( ) ) ;
84+ progress_bar_stopper ( ) ;
85+
86+ if detail. is_err ( ) {
87+ log:: error!(
88+ "Cannot convert the text to command detail, error: {}" ,
89+ detail. err( ) . unwrap( )
90+ ) ;
91+ self . status = Status :: WaitingText ;
7992 continue ;
8093 }
94+
8195 let detail = detail. unwrap ( ) ;
96+
8297 println ! ( "{}:" , "Description" . bold( ) ) ;
8398 for ( index, desc) in detail. descriptions . iter ( ) . enumerate ( ) {
8499 println ! ( "{}. {}" , ( index + 1 ) . to_string( ) . bold( ) , desc) ;
85100 }
86101 println ! ( "{}: {}" , "Command" . bold( ) , detail. command. bold( ) ) ;
102+
87103 self . last_detail = Some ( detail) ;
88104 self . status = Status :: WaitingUserChoice ;
89105 }
90106 Status :: WaitingUserChoice => {
91- let last_detail = self . last_detail . as_ref ( ) . unwrap ( ) ;
107+ let last_detail = self
108+ . last_detail
109+ . as_ref ( )
110+ . expect ( "Expecting get the last detail in WaitingUserChoice status." ) ;
92111
93112 let options = vec ! [
94113 UserChoice :: ExecuteCommand ,
@@ -99,6 +118,7 @@ impl App {
99118 let ans = Select :: new ( "What do you want to do next?" , options)
100119 . prompt ( )
101120 . unwrap ( ) ;
121+
102122 match ans {
103123 UserChoice :: ExecuteCommand => {
104124 let result = execute_command ( last_detail. command . as_str ( ) ) ;
0 commit comments