use colored::Colorize;
use std::io;
use std::io::Write;
use std::process::ExitCode;
#[macro_export]
macro_rules! println {
() => (print!("\n"));
($fmt:expr) => ({
writeln!(std::io::stdout(), $fmt)
});
($fmt:expr, $($arg:tt)*) => ({
writeln!(std::io::stdout(), $fmt, $($arg)*)
})
}
#[macro_export]
macro_rules! print {
() => (print!("\n"));
($fmt:expr) => ({
write!(std::io::stdout(), $fmt)
});
($fmt:expr, $($arg:tt)*) => ({
write!(std::io::stdout(), $fmt, $($arg)*)
})
}
#[macro_export]
macro_rules! eprintln {
() => (eprint!("\n"));
($fmt:expr) => ({
writeln!(&mut std::io::stderr(), $fmt)
});
($fmt:expr, $($arg:tt)*) => ({
writeln!(&mut std::io::stderr(), $fmt, $($arg)*)
})
}
#[macro_export]
macro_rules! eprint {
() => (eprint!("\n"));
($fmt:expr) => ({
write!(&mut std::io::stderr(), $fmt)
});
($fmt:expr, $($arg:tt)*) => ({
write!(&mut std::io::stderr(), $fmt, $($arg)*)
})
}
pub fn safe_main(main: fn() -> io::Result<ExitCode>) -> ExitCode {
match main() {
Err(err) if err.kind() == io::ErrorKind::BrokenPipe => {
ExitCode::SUCCESS
}
Err(err) => {
let warning = format!("{err}").red();
eprintln!("{warning}").ok();
ExitCode::FAILURE
}
Ok(exit_code) => exit_code, }
}