-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make the REPL handle line continuation better #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
88d6926
2c93c79
5c8c4a8
36ff4e3
18be23a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,6 +153,14 @@ fn get_history_path() -> PathBuf { | |
xdg_dirs.place_cache_file("repl_history.txt").unwrap() | ||
} | ||
|
||
fn get_prompt(vm: &mut VirtualMachine, prompt_name: &str) -> String { | ||
vm.get_attribute(vm.sys_module.clone(), prompt_name) | ||
.ok() | ||
.as_ref() | ||
.map(objstr::get_value) | ||
.unwrap_or_else(String::new) | ||
} | ||
|
||
fn run_shell(vm: &mut VirtualMachine) -> PyResult { | ||
println!( | ||
"Welcome to the magnificent Rust Python {} interpreter", | ||
|
@@ -170,33 +178,44 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { | |
println!("No previous history."); | ||
} | ||
|
||
let ps1 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps1").unwrap()); | ||
let ps2 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps2").unwrap()); | ||
let mut prompt = ps1; | ||
let mut continuing = false; | ||
|
||
loop { | ||
match repl.readline(prompt) { | ||
let prompt = if continuing { | ||
get_prompt(vm, "ps2") | ||
} else { | ||
get_prompt(vm, "ps1") | ||
}; | ||
match repl.readline(&prompt) { | ||
Ok(line) => { | ||
debug!("You entered {:?}", line); | ||
input.push_str(&line); | ||
input.push_str("\n"); | ||
input.push('\n'); | ||
repl.add_history_entry(line.trim_end()); | ||
|
||
if continuing { | ||
if line.is_empty() { | ||
continuing = false; | ||
} else { | ||
continue; | ||
} | ||
} | ||
|
||
match shell_exec(vm, &input, vars.clone()) { | ||
Err(CompileError::Parse(ParseError::EOF(_))) => { | ||
prompt = ps2; | ||
continuing = true; | ||
continue; | ||
} | ||
_ => { | ||
prompt = ps1; | ||
input = String::new(); | ||
} | ||
} | ||
} | ||
Err(ReadlineError::Interrupted) => { | ||
// TODO: Raise a real KeyboardInterrupt exception | ||
println!("^C"); | ||
break; | ||
continuing = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should quit when hitting CTR+C, right? Now we continue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, right. I should've discussed this before making the change, but usually in shells (iPython, bash/zsh/sh/sshshshshshs), ^C just cancels what you're currently typing, it doesn't completely end the shell like ^D does. I found it to be sort of annoying when testing something in the REPL, so I changed it here; if you disagree with it I can change it back though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, in that case, nice job! |
||
continue; | ||
} | ||
Err(ReadlineError::Eof) => { | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, this also fixes changing
sys.ps1
on the fly!