Skip to content

Commit 3053acb

Browse files
author
Roman Stingler
committed
fix: handle EOF in user input prompts
Add proper EOF detection in ask() function Print " -> EOF" message when EOF is received Treat EOF as negative response Handle input read errors explicitly closes Morganamilo#1190
1 parent e9016d2 commit 3053acb

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/util.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,27 @@ pub fn ask(config: &Config, question: &str, default: bool) -> bool {
134134
}
135135
let stdin = stdin();
136136
let mut input = String::new();
137-
let _ = stdin.read_line(&mut input);
138-
let input = input.to_lowercase();
139-
let input = input.trim();
140-
141-
if input == tr!("y") || input == tr!("yes") {
142-
true
143-
} else if input.trim().is_empty() {
144-
default
145-
} else {
146-
false
137+
match stdin.read_line(&mut input) {
138+
Ok(0) => {
139+
println!();
140+
false
141+
}
142+
Ok(_) => {
143+
let input = input.to_lowercase();
144+
let input = input.trim();
145+
146+
if input == tr!("y") || input == tr!("yes") {
147+
true
148+
} else if input.trim().is_empty() {
149+
default
150+
} else {
151+
false
152+
}
153+
}
154+
Err(_) => {
155+
println!(" -> Error reading input");
156+
false
157+
}
147158
}
148159
}
149160

0 commit comments

Comments
 (0)