use std::io::{self, IsTerminal, Write};
use buildl::approver::Console;
const fn both_terminals(stdin: bool, stderr: bool) -> bool {
stdin && stderr
}
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct StdConsole;
impl Console for StdConsole {
fn is_terminal(&self) -> bool {
both_terminals(io::stdin().is_terminal(), io::stderr().is_terminal())
}
fn ask(&mut self, prompt: &str) -> io::Result<String> {
let mut stderr = io::stderr().lock();
stderr.write_all(prompt.as_bytes())?;
stderr.flush()?;
let mut line = String::new();
io::stdin().read_line(&mut line)?;
Ok(line)
}
}
#[cfg(test)]
mod tests {
use super::both_terminals;
#[test]
fn one_terminal_alone_is_not_enough_to_ask() {
assert!(both_terminals(true, true));
assert!(!both_terminals(true, false));
assert!(!both_terminals(false, true));
assert!(!both_terminals(false, false));
}
}