Skip to content

Commit 88d6926

Browse files
committed
Make the REPL handle continuation better
1 parent 08520a6 commit 88d6926

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ fn get_history_path() -> PathBuf {
159159
xdg_dirs.place_cache_file("repl_history.txt").unwrap()
160160
}
161161

162+
fn get_prompt(vm: &mut VirtualMachine, prompt_name: &str) -> String {
163+
vm.sys_module
164+
.get_attr(prompt_name)
165+
.as_ref()
166+
.map(objstr::get_value)
167+
.unwrap_or_else(String::new)
168+
}
169+
162170
fn run_shell(vm: &mut VirtualMachine) -> PyResult {
163171
println!(
164172
"Welcome to the magnificent Rust Python {} interpreter",
@@ -176,33 +184,45 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
176184
println!("No previous history.");
177185
}
178186

179-
let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
180-
let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
181-
let mut prompt = ps1;
187+
let mut prompt = get_prompt(vm, "ps1");
188+
189+
let mut continuing = false;
182190

183191
loop {
184-
match repl.readline(prompt) {
192+
if !continuing {
193+
prompt = get_prompt(vm, "ps1");
194+
}
195+
match repl.readline(&prompt) {
185196
Ok(line) => {
186197
debug!("You entered {:?}", line);
187198
input.push_str(&line);
188-
input.push_str("\n");
199+
input.push('\n');
189200
repl.add_history_entry(line.trim_end());
190201

202+
if continuing {
203+
if line.is_empty() {
204+
continuing = false;
205+
} else {
206+
continue;
207+
}
208+
}
209+
191210
match shell_exec(vm, &input, vars.clone()) {
192211
Err(CompileError::Parse(ParseError::EOF(_))) => {
193-
prompt = ps2;
212+
prompt = get_prompt(vm, "ps2");
213+
continuing = true;
194214
continue;
195215
}
196216
_ => {
197-
prompt = ps1;
198217
input = String::new();
199218
}
200219
}
201220
}
202221
Err(ReadlineError::Interrupted) => {
203222
// TODO: Raise a real KeyboardInterrupt exception
204223
println!("^C");
205-
break;
224+
continuing = false;
225+
continue;
206226
}
207227
Err(ReadlineError::Eof) => {
208228
break;

0 commit comments

Comments
 (0)