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