@@ -159,6 +159,14 @@ fn get_history_path() -> PathBuf {
159
159
xdg_dirs. place_cache_file ( "repl_history.txt" ) . unwrap ( )
160
160
}
161
161
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
+
162
170
fn run_shell ( vm : & mut VirtualMachine ) -> PyResult {
163
171
println ! (
164
172
"Welcome to the magnificent Rust Python {} interpreter" ,
@@ -176,33 +184,45 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
176
184
println ! ( "No previous history." ) ;
177
185
}
178
186
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 ;
182
190
183
191
loop {
184
- match repl. readline ( prompt) {
192
+ if !continuing {
193
+ prompt = get_prompt ( vm, "ps1" ) ;
194
+ }
195
+ match repl. readline ( & prompt) {
185
196
Ok ( line) => {
186
197
debug ! ( "You entered {:?}" , line) ;
187
198
input. push_str ( & line) ;
188
- input. push_str ( " \n " ) ;
199
+ input. push ( '\n' ) ;
189
200
repl. add_history_entry ( line. trim_end ( ) ) ;
190
201
202
+ if continuing {
203
+ if line. is_empty ( ) {
204
+ continuing = false ;
205
+ } else {
206
+ continue ;
207
+ }
208
+ }
209
+
191
210
match shell_exec ( vm, & input, vars. clone ( ) ) {
192
211
Err ( CompileError :: Parse ( ParseError :: EOF ( _) ) ) => {
193
- prompt = ps2;
212
+ prompt = get_prompt ( vm, "ps2" ) ;
213
+ continuing = true ;
194
214
continue ;
195
215
}
196
216
_ => {
197
- prompt = ps1;
198
217
input = String :: new ( ) ;
199
218
}
200
219
}
201
220
}
202
221
Err ( ReadlineError :: Interrupted ) => {
203
222
// TODO: Raise a real KeyboardInterrupt exception
204
223
println ! ( "^C" ) ;
205
- break ;
224
+ continuing = false ;
225
+ continue ;
206
226
}
207
227
Err ( ReadlineError :: Eof ) => {
208
228
break ;
0 commit comments