Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Make the REPL handle continuation better
  • Loading branch information
coolreader18 committed Mar 22, 2019
commit 88d6926f786874060b378110329181817572c157
36 changes: 28 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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.sys_module
.get_attr(prompt_name)
Comment thread
coolreader18 marked this conversation as resolved.
Outdated
.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 @@ -176,33 +184,45 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
println!("No previous history.");
}

let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
let mut prompt = ps1;
let mut prompt = get_prompt(vm, "ps1");

let mut continuing = false;

loop {
match repl.readline(prompt) {
if !continuing {
Comment thread
coolreader18 marked this conversation as resolved.
Outdated
prompt = 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;
prompt = get_prompt(vm, "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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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