Skip to content

Commit

Permalink
fix: handle EOF in user input prompts
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Roman Stingler committed Dec 25, 2024
1 parent b186470 commit fb5f72c
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,27 @@ pub fn ask(config: &Config, question: &str, default: bool) -> bool {
}
let stdin = stdin();
let mut input = String::new();
let _ = stdin.read_line(&mut input);
let input = input.to_lowercase();
let input = input.trim();

if input == tr!("y") || input == tr!("yes") {
true
} else if input.trim().is_empty() {
default
} else {
false
match stdin.read_line(&mut input) {
Ok(0) => {
println!(" -> EOF");
false
}
Ok(_) => {
let input = input.to_lowercase();
let input = input.trim();

if input == tr!("y") || input == tr!("yes") {
true
} else if input.trim().is_empty() {
default
} else {
false
}
}
Err(_) => {
println!(" -> Error reading input");
false
}
}
}

Expand Down

0 comments on commit fb5f72c

Please sign in to comment.