Skip to content

Commit 2330534

Browse files
committed
improve multiline command behavior + setup for using sys.ps1 and sys.ps
1 parent c95caa1 commit 2330534

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/main.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,6 @@ fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool
120120
true
121121
}
122122

123-
fn read_until_empty_line(input: &mut String) -> Result<i32, std::io::Error> {
124-
loop {
125-
print!("..... ");
126-
io::stdout().flush().expect("Could not flush stdout");
127-
let mut line = String::new();
128-
match io::stdin().read_line(&mut line) {
129-
Ok(_) => {
130-
line = line
131-
.trim_right_matches(|c| c == '\r' || c == '\n')
132-
.to_string();
133-
if line.len() == 0 {
134-
return Ok(0); // DOne
135-
} else {
136-
input.push_str(&line);
137-
input.push_str("\n");
138-
}
139-
}
140-
Err(msg) => {
141-
return Err(msg);
142-
}
143-
}
144-
}
145-
}
146-
147123
fn run_shell() {
148124
println!(
149125
"Welcome to the magnificent Rust Python {} interpreter",
@@ -156,7 +132,13 @@ fn run_shell() {
156132
// Read a single line:
157133
let mut input = String::new();
158134
loop {
159-
print!(">>>>> "); // Use 5 items. pypy has 4, cpython has 3.
135+
// TODO: modules dont support getattr / setattr yet
136+
//let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps1") {
137+
// Ok(value) => objstr::get_value(&value),
138+
// Err(_) => ">>>>> ".to_string(),
139+
//};
140+
print!(">>>>> ");
141+
160142
io::stdout().flush().expect("Could not flush stdout");
161143
match io::stdin().read_line(&mut input) {
162144
Ok(0) => {
@@ -168,11 +150,32 @@ fn run_shell() {
168150
// Line was complete.
169151
input = String::new();
170152
} else {
171-
match read_until_empty_line(&mut input) {
172-
Ok(_) => {
173-
shell_exec(&mut vm, &input, vars.clone());
153+
loop {
154+
// until an empty line is pressed AND the code is complete
155+
//let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps2") {
156+
// Ok(value) => objstr::get_value(&value),
157+
// Err(_) => "..... ".to_string(),
158+
//};
159+
print!("..... ");
160+
io::stdout().flush().expect("Could not flush stdout");
161+
let mut line = String::new();
162+
match io::stdin().read_line(&mut line) {
163+
Ok(_) => {
164+
line = line
165+
.trim_right_matches(|c| c == '\r' || c == '\n')
166+
.to_string();
167+
if line.len() == 0 {
168+
if shell_exec(&mut vm, &input, vars.clone()) {
169+
input = String::new();
170+
break;
171+
}
172+
} else {
173+
input.push_str(&line);
174+
input.push_str("\n");
175+
}
176+
}
177+
Err(msg) => panic!("Error: {:?}", msg),
174178
}
175-
Err(msg) => panic!("Error: {:?}", msg),
176179
}
177180
}
178181
}

0 commit comments

Comments
 (0)