Skip to content

Commit 369fedf

Browse files
fix: ignore stray empty input in interactive prompts
When running interactive tests (like `INTERACTIVE=1 cargo test --nocapture`), stray newlines from previous commands or input streams can cause `read_line` to immediately return an empty string. This caused the first prompt (`prompt_microstructure_variable`) to be skipped, and the intended answer was instead consumed by the second prompt (`prompt_cutoff_value`). This commit updates `helpers::prompt_microstructure_variable` and `helpers::prompt_cutoff_value` to repeatedly read from `stdin` until a non-empty line is provided. It also correctly handles EOF (`bytes == 0`) to prevent infinite loops when stdin is exhausted. Co-authored-by: soniapi <396009+soniapi@users.noreply.github.com>
1 parent ad47dfb commit 369fedf

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

src/helpers.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
1111
let mut input = String::new();
1212

1313
// Read user input
14-
io::stdin()
15-
.read_line(&mut input)
16-
.expect("Failed to read input");
14+
loop {
15+
input.clear();
16+
let bytes = io::stdin()
17+
.read_line(&mut input)
18+
.expect("Failed to read input");
19+
20+
if bytes == 0 {
21+
return None; // EOF
22+
}
23+
24+
if !input.trim().is_empty() {
25+
break;
26+
}
27+
}
1728

1829
// Check if input is "1)" or "1"
1930
let trimmed = input.trim();
@@ -52,9 +63,20 @@ pub fn prompt_cutoff_value() -> Option<f32> {
5263
let mut input = String::new();
5364

5465
// Read user input
55-
io::stdin()
56-
.read_line(&mut input)
57-
.expect("Failed to read input");
66+
loop {
67+
input.clear();
68+
let bytes = io::stdin()
69+
.read_line(&mut input)
70+
.expect("Failed to read input");
71+
72+
if bytes == 0 {
73+
return None; // EOF
74+
}
75+
76+
if !input.trim().is_empty() {
77+
break;
78+
}
79+
}
5880

5981
// The user can only enter a number for now, anything else will exit the function.
6082
if let Ok(divide) = input.trim().parse::<f32>() {

0 commit comments

Comments
 (0)