@@ -6,41 +6,51 @@ use rustyline::{error::ReadlineError, DefaultEditor};
6
6
7
7
use crate :: Action ;
8
8
9
+ pub fn process_input ( input : & str , dvd_bnd : & DvdBnd ) -> Result < ( ) , Box < dyn Error > > {
10
+ let args = shlex:: split ( input) . ok_or ( "failed to parse input" ) ?;
11
+
12
+ let command = Action :: augment_subcommands ( clap:: Command :: new ( "repl" ) . no_binary_name ( true ) ) ;
13
+ let action = command
14
+ . clone ( )
15
+ . try_get_matches_from ( args)
16
+ . and_then ( |matches| Action :: from_arg_matches ( & matches) )
17
+ . map_err ( |e| e. to_string ( ) ) ?;
18
+
19
+ match action {
20
+ Action :: Repl => {
21
+ println ! ( "Already running repl." ) ;
22
+ Ok ( ( ) )
23
+ }
24
+ action => {
25
+ action. run ( dvd_bnd)
26
+ }
27
+ }
28
+ }
29
+
9
30
pub fn begin ( dvd_bnd : & DvdBnd ) -> Result < ( ) , Box < dyn Error > > {
10
31
let mut rl = DefaultEditor :: new ( ) ?;
11
32
let _ = rl. load_history ( "history.txt" ) ;
12
33
13
- let command = Action :: augment_subcommands ( clap:: Command :: new ( "repl" ) . no_binary_name ( true ) ) ;
14
-
15
34
loop {
16
35
let readline = rl. readline ( ">> " ) ;
36
+
17
37
match readline {
18
38
Ok ( line) => {
19
39
rl. add_history_entry ( line. as_str ( ) ) ?;
20
- let args = shlex:: split ( & line) . ok_or ( "failed to parse input" ) ?;
21
-
22
- let action = command
23
- . clone ( )
24
- . try_get_matches_from ( args)
25
- . and_then ( |matches| Action :: from_arg_matches ( & matches) )
26
- . map_err ( |e| e. to_string ( ) ) ;
27
-
28
- match action {
29
- Ok ( Action :: Repl ) => {
30
- println ! ( "Running repl inside repl doesn't make sense" ) ;
31
- continue ;
32
- }
33
- Ok ( action) => {
34
- let _ = action. run ( dvd_bnd) ;
40
+
41
+ match process_input ( & line, dvd_bnd) {
42
+ Ok ( _) => println ! ( "Command successful" ) ,
43
+ Err ( e) => {
44
+ println ! ( "{:#?}" , e) ;
35
45
}
36
- Err ( e) => println ! ( "{}" , e) ,
37
- } ;
46
+ }
38
47
}
39
48
Err ( ReadlineError :: Interrupted ) => {
40
- println ! ( "CTRL-C" ) ;
49
+ println ! ( "^C" ) ;
50
+ continue ;
41
51
}
42
52
Err ( ReadlineError :: Eof ) => {
43
- println ! ( "CTRL- D" ) ;
53
+ println ! ( "^ D" ) ;
44
54
break ;
45
55
}
46
56
Err ( err) => {
0 commit comments