use std::{io::Write, process::exit};
pub fn panic_handler() {
let prev = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
let error_message = indoc::formatdoc! {
r#"{fatal}
Whoops! The Amaru process panicked, rather than handling the error it encountered gracefully.
This is almost certainly a bug, and we'd appreciate a report so we can improve Amaru.
Please report this error at https://github.com/pragma-org/amaru/issues/new.
In your bug report please provide the information below and if possible the code
that produced it.
{info}
"#,
info = node_info(),
fatal = "amaru::fatal::error",
};
eprintln!("\n{}", indent(&error_message, 3));
prev(info);
std::io::stderr().flush().ok();
exit(1);
}));
}
pub fn indent(lines: &str, n: usize) -> String {
let tab = pad_left(String::new(), n, " ");
lines.lines().map(|line| format!("{tab}{line}")).collect::<Vec<_>>().join("\n")
}
pub fn pad_left(mut text: String, n: usize, delimiter: &str) -> String {
let diff = n as i32 - text.len() as i32;
if diff.is_positive() {
for _ in 0..diff {
text.insert_str(0, delimiter);
}
}
text
}
mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
pub fn node_info() -> String {
format!(
r#"
Operating System: {}
Architecture: {}
Version: {}"#,
built_info::CFG_OS,
built_info::CFG_TARGET_ARCH,
node_version(true),
)
}
pub fn node_version(include_commit_hash: bool) -> String {
let version = built_info::PKG_VERSION;
let suffix = if include_commit_hash {
format!("+{}", built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown"))
} else {
"".to_string()
};
format!("v{version}{suffix}")
}