Skip to content

Commit 162c2dc

Browse files
committed
Merge branch 'coolreader18/multiline-repl'
Make the REPL handle line continuation better RustPython#711
2 parents 7ed4bc4 + 18be23a commit 162c2dc

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/main.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ fn get_history_path() -> PathBuf {
153153
xdg_dirs.place_cache_file("repl_history.txt").unwrap()
154154
}
155155

156+
fn get_prompt(vm: &VirtualMachine, prompt_name: &str) -> String {
157+
vm.get_attribute(vm.sys_module.clone(), prompt_name)
158+
.ok()
159+
.as_ref()
160+
.map(objstr::get_value)
161+
.unwrap_or_else(String::new)
162+
}
163+
156164
fn run_shell(vm: &VirtualMachine) -> PyResult {
157165
println!(
158166
"Welcome to the magnificent Rust Python {} interpreter",
@@ -170,33 +178,44 @@ fn run_shell(vm: &VirtualMachine) -> PyResult {
170178
println!("No previous history.");
171179
}
172180

173-
let ps1 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps1").unwrap());
174-
let ps2 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps2").unwrap());
175-
let mut prompt = ps1;
181+
let mut continuing = false;
176182

177183
loop {
178-
match repl.readline(prompt) {
184+
let prompt = if continuing {
185+
get_prompt(vm, "ps2")
186+
} else {
187+
get_prompt(vm, "ps1")
188+
};
189+
match repl.readline(&prompt) {
179190
Ok(line) => {
180191
debug!("You entered {:?}", line);
181192
input.push_str(&line);
182-
input.push_str("\n");
193+
input.push('\n');
183194
repl.add_history_entry(line.trim_end());
184195

196+
if continuing {
197+
if line.is_empty() {
198+
continuing = false;
199+
} else {
200+
continue;
201+
}
202+
}
203+
185204
match shell_exec(vm, &input, vars.clone()) {
186205
Err(CompileError::Parse(ParseError::EOF(_))) => {
187-
prompt = ps2;
206+
continuing = true;
188207
continue;
189208
}
190209
_ => {
191-
prompt = ps1;
192210
input = String::new();
193211
}
194212
}
195213
}
196214
Err(ReadlineError::Interrupted) => {
197215
// TODO: Raise a real KeyboardInterrupt exception
198216
println!("^C");
199-
break;
217+
continuing = false;
218+
continue;
200219
}
201220
Err(ReadlineError::Eof) => {
202221
break;

0 commit comments

Comments
 (0)