@@ -6,9 +6,9 @@ use std::fs;
66use std:: io;
77use std:: fmt;
88
9- use termion :: raw :: IntoRawMode ;
10- use termion :: input :: TermRead ;
11- use termion :: event :: Key ;
9+ use crossterm :: terminal ;
10+ use crossterm :: event :: { self , Event , KeyCode , KeyModifiers } ;
11+ use crossterm :: cursor :: MoveToColumn ;
1212
1313#[ macro_use]
1414mod lexer;
@@ -1000,48 +1000,57 @@ fn start_new_cool_repl() {
10001000 // TODO: check if the stdin is tty
10011001 // If it is not maybe switch to the old/simplified REPL
10021002 let prompt = "new> " ;
1003- let mut stdout = stdout ( ) . into_raw_mode ( ) . unwrap ( ) ;
1004- let stdin = stdin ( ) ;
1005- write ! ( stdout, "{}" , prompt) . unwrap ( ) ;
1006- stdout. flush ( ) . unwrap ( ) ;
1003+ terminal:: enable_raw_mode ( ) . expect ( "failed to enable raw mode" ) ;
1004+ print ! ( "{}" , prompt) ;
1005+ stdout ( ) . flush ( ) . unwrap ( ) ;
10071006
10081007 let mut new_cool_repl: NewCoolRepl = Default :: default ( ) ;
10091008
1010- for key in stdin. keys ( ) {
1011- match key. unwrap ( ) {
1012- Key :: Char ( '\n' ) => {
1013- write ! ( stdout, "\r \n " ) . unwrap ( ) ;
1014- if & new_cool_repl. take ( ) == "quit" {
1015- break
1016- }
1017- }
1018- Key :: Ctrl ( 'a' ) | Key :: Home => new_cool_repl. home ( ) ,
1019- Key :: Ctrl ( 'e' ) | Key :: End => new_cool_repl. end ( ) ,
1020- Key :: Ctrl ( 'b' ) | Key :: Left => new_cool_repl. left_char ( ) ,
1021- Key :: Ctrl ( 'f' ) | Key :: Right => new_cool_repl. right_char ( ) ,
1022- Key :: Ctrl ( 'n' ) | Key :: Down => new_cool_repl. down ( ) ,
1023- Key :: Ctrl ( 'p' ) | Key :: Up => new_cool_repl. up ( ) ,
1024- Key :: Ctrl ( 'c' ) => {
1025- write ! ( stdout, "^C\r \n " ) . unwrap ( ) ;
1026- break ;
1027- }
1028- Key :: Alt ( 'b' ) => new_cool_repl. left_word ( ) ,
1029- Key :: Alt ( 'f' ) => new_cool_repl. right_word ( ) ,
1030- Key :: Char ( key) => {
1031- new_cool_repl. insert_char ( key) ;
1032- new_cool_repl. popup . clear ( ) ;
1033- if let Ok ( ( head, body) ) = parse_match ( & mut Lexer :: new ( new_cool_repl. buffer . clone ( ) , None ) ) {
1034- let subexprs = find_all_subexprs ( & head, & body) ;
1035- for subexpr in subexprs {
1036- new_cool_repl. popup . push ( format ! ( "{}" , HighlightedSubexpr { expr: & body, subexpr} ) ) ;
1037- }
1038- }
1039- } ,
1040- Key :: Backspace => new_cool_repl. backspace ( ) ,
1041- _ => { } ,
1042- }
1043- new_cool_repl. render ( prompt, & mut stdout) . unwrap ( ) ;
1044- stdout. flush ( ) . unwrap ( ) ;
1009+
1010+ while let Ok ( Event :: Key ( key) ) = event:: read ( ) {
1011+ match key. modifiers {
1012+ KeyModifiers :: CONTROL => match key. code {
1013+ KeyCode :: Char ( 'a' ) | KeyCode :: Home => new_cool_repl. home ( ) ,
1014+ KeyCode :: Char ( 'e' ) | KeyCode :: End => new_cool_repl. end ( ) ,
1015+ KeyCode :: Char ( 'b' ) | KeyCode :: Left => new_cool_repl. left_char ( ) ,
1016+ KeyCode :: Char ( 'f' ) | KeyCode :: Right => new_cool_repl. right_char ( ) ,
1017+ KeyCode :: Char ( 'n' ) | KeyCode :: Down => new_cool_repl. down ( ) ,
1018+ KeyCode :: Char ( 'p' ) | KeyCode :: Up => new_cool_repl. up ( ) ,
1019+ KeyCode :: Char ( 'c' ) => {
1020+ print ! ( "^C\r \n {}" , MoveToColumn ( 0 ) ) ;
1021+ break ;
1022+ }
1023+ _ => { }
1024+ }
1025+ KeyModifiers :: ALT => match key. code {
1026+ KeyCode :: Char ( 'b' ) => new_cool_repl. left_word ( ) ,
1027+ KeyCode :: Char ( 'f' ) => new_cool_repl. right_word ( ) ,
1028+ _ => { }
1029+ }
1030+ KeyModifiers :: NONE => match key. code {
1031+ KeyCode :: Char ( '\n' ) => {
1032+ print ! ( "\r \n {}" , MoveToColumn ( 0 ) ) ;
1033+ if & new_cool_repl. take ( ) == "quit" {
1034+ break
1035+ }
1036+ }
1037+ KeyCode :: Char ( key) => {
1038+ new_cool_repl. insert_char ( key) ;
1039+ new_cool_repl. popup . clear ( ) ;
1040+ if let Ok ( ( head, body) ) = parse_match ( & mut Lexer :: new ( new_cool_repl. buffer . clone ( ) , None ) ) {
1041+ let subexprs = find_all_subexprs ( & head, & body) ;
1042+ for subexpr in subexprs {
1043+ new_cool_repl. popup . push ( format ! ( "{}" , HighlightedSubexpr { expr: & body, subexpr} ) ) ;
1044+ }
1045+ }
1046+ } ,
1047+ KeyCode :: Backspace => new_cool_repl. backspace ( ) ,
1048+ _ => { }
1049+ }
1050+ _ => { } ,
1051+ }
1052+ new_cool_repl. render ( prompt, & mut stdout ( ) . lock ( ) ) . unwrap ( ) ;
1053+ stdout ( ) . flush ( ) . unwrap ( ) ;
10451054 }
10461055}
10471056
0 commit comments