Skip to content

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

Merged
merged 5 commits into from
Mar 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Copy link
Contributor

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!

} 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, in that case, nice job!

continue;
}
Err(ReadlineError::Eof) => {
break;
Expand Down