@@ -16,7 +16,6 @@ use rustpython_vm::{
1616 util, VirtualMachine ,
1717} ;
1818
19- //use rustyline::{error::ReadlineError, Editor};
2019use std:: io:: { self , BufRead , Write } ;
2120use std:: path:: PathBuf ;
2221use std:: process;
@@ -299,76 +298,90 @@ fn get_prompt(vm: &VirtualMachine, prompt_name: &str) -> String {
299298 . unwrap_or_else ( String :: new)
300299}
301300
301+ #[ cfg( not( target_os = "redox" ) ) ]
302302fn run_shell ( vm : & VirtualMachine ) -> PyResult {
303+ use rustyline:: { error:: ReadlineError , Editor } ;
304+
303305 println ! (
304306 "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596} " ,
305307 crate_version!( )
306308 ) ;
307309 let vars = vm. new_scope_with_builtins ( ) ;
308310
309- // // Read a single line:
310- // let mut input = String::new();
311- // let mut repl = Editor::<()>::new();
312-
313- // // Retrieve a `history_path_str` dependent on the OS
314- // let repl_history_path_str = &get_history_path();
315- // if repl.load_history(repl_history_path_str).is_err() {
316- // println!("No previous history.");
317- // }
318-
319- // let mut continuing = false;
320-
321- // loop {
322- // let prompt = if continuing {
323- // get_prompt(vm, "ps2")
324- // } else {
325- // get_prompt(vm, "ps1")
326- // };
327- // match repl.readline(&prompt) {
328- // Ok(line) => {
329- // debug!("You entered {:?}", line);
330- // input.push_str(&line);
331- // input.push('\n');
332- // repl.add_history_entry(line.trim_end());
333-
334- // if continuing {
335- // if line.is_empty() {
336- // continuing = false;
337- // } else {
338- // continue;
339- // }
340- // }
341-
342- // match shell_exec(vm, &input, vars.clone()) {
343- // Err(CompileError {
344- // error: CompileErrorType::Parse(ParseErrorType::EOF),
345- // ..
346- // }) => {
347- // continuing = true;
348- // continue;
349- // }
350- // _ => {
351- // input = String::new();
352- // }
353- // }
354- // }
355- // Err(ReadlineError::Interrupted) => {
356- // // TODO: Raise a real KeyboardInterrupt exception
357- // println!("^C");
358- // continuing = false;
359- // continue;
360- // }
361- // Err(ReadlineError::Eof) => {
362- // break;
363- // }
364- // Err(err) => {
365- // println!("Error: {:?}", err);
366- // break;
367- // }
368- // };
369- // }
370- // repl.save_history(repl_history_path_str).unwrap();
311+ // Read a single line:
312+ let mut input = String :: new ( ) ;
313+ let mut repl = Editor :: < ( ) > :: new ( ) ;
371314
315+ // Retrieve a `history_path_str` dependent on the OS
316+ let repl_history_path_str = & get_history_path ( ) ;
317+ if repl. load_history ( repl_history_path_str) . is_err ( ) {
318+ println ! ( "No previous history." ) ;
319+ }
320+
321+ let mut continuing = false ;
322+
323+ loop {
324+ let prompt = if continuing {
325+ get_prompt ( vm, "ps2" )
326+ } else {
327+ get_prompt ( vm, "ps1" )
328+ } ;
329+ match repl. readline ( & prompt) {
330+ Ok ( line) => {
331+ debug ! ( "You entered {:?}" , line) ;
332+ input. push_str ( & line) ;
333+ input. push ( '\n' ) ;
334+ repl. add_history_entry ( line. trim_end ( ) ) ;
335+
336+ if continuing {
337+ if line. is_empty ( ) {
338+ continuing = false ;
339+ } else {
340+ continue ;
341+ }
342+ }
343+
344+ match shell_exec ( vm, & input, vars. clone ( ) ) {
345+ Err ( CompileError {
346+ error : CompileErrorType :: Parse ( ParseErrorType :: EOF ) ,
347+ ..
348+ } ) => {
349+ continuing = true ;
350+ continue ;
351+ }
352+ _ => {
353+ input = String :: new ( ) ;
354+ }
355+ }
356+ }
357+ Err ( ReadlineError :: Interrupted ) => {
358+ // TODO: Raise a real KeyboardInterrupt exception
359+ println ! ( "^C" ) ;
360+ continuing = false ;
361+ continue ;
362+ }
363+ Err ( ReadlineError :: Eof ) => {
364+ break ;
365+ }
366+ Err ( err) => {
367+ println ! ( "Error: {:?}" , err) ;
368+ break ;
369+ }
370+ } ;
371+ }
372+ repl. save_history ( repl_history_path_str) . unwrap ( ) ;
373+
374+ Ok ( vm. get_none ( ) )
375+ }
376+
377+ #[ cfg( target_os = "redox" ) ]
378+ fn run_shell ( vm : & VirtualMachine ) -> PyResult {
379+ println ! (
380+ "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596} " ,
381+ crate_version!( )
382+ ) ;
383+ let vars = vm. new_scope_with_builtins ( ) ;
384+
372385 let stdin = io:: stdin ( ) ;
373386 let stdout = io:: stdout ( ) ;
374387 let mut stdout = stdout. lock ( ) ;
0 commit comments