use std::{env::{args, Args},
error::Error,
io::{stdin, stdout, Write}};
use r3bl_rs_utils_core::{style_error, style_prompt};
use super::with;
pub fn readline() -> (usize, String) {
let mut temp_string_buffer: String = String::new();
match stdin().read_line(&mut temp_string_buffer) {
Ok(bytes_read) => {
let guess: String = temp_string_buffer.trim().to_string(); (bytes_read, guess)
}
Err(_) => {
println!(
"{}",
style_error("Something went wrong when reading input from terminal.")
);
(0, "".to_string())
}
}
}
pub fn print_prompt(prompt: &str) -> Result<(), Box<dyn Error>> {
print!("{}", style_prompt(prompt));
stdout().lock().flush()?;
Ok(())
}
pub fn readline_with_prompt(prompt: &str) -> Result<String, Box<dyn Error>> {
print_prompt(prompt)?;
Ok(readline().1)
}
pub fn is_stdin_piped() -> bool { atty::isnt(atty::Stream::Stdin) }
pub fn is_stdout_piped() -> bool { atty::isnt(atty::Stream::Stdout) }
pub fn is_tty() -> bool {
atty::is(atty::Stream::Stdin)
&& atty::is(atty::Stream::Stdout)
&& atty::is(atty::Stream::Stderr)
}
pub trait ArgsToStrings {
fn filter_and_convert_to_strings(&self) -> Vec<String>;
}
impl ArgsToStrings for Args {
fn filter_and_convert_to_strings(&self) -> Vec<String> {
with(args().collect::<Vec<String>>(), |mut list| {
if !list.is_empty() {
list.remove(0);
}
list
})
}
}