phink_lib/cli/
mod.rs

1use crossterm::style::Stylize;
2use std::backtrace::BacktraceStatus;
3
4pub mod config;
5pub mod env;
6pub mod ui;
7pub mod ziggy;
8
9/// Prints `anyhow` errors correctly in the Phink style
10///
11/// # Argument
12/// * `e`: The `anyhow::Error` to pretty-print
13///
14/// returns: `String`
15///
16/// # Example
17/// `eprintln!("{}", format_error(e));`
18pub fn format_error(e: anyhow::Error) -> String {
19    let maybe_backtrace = if std::env::var("RUST_BACKTRACE").is_err() {
20        format!(
21            "\nUse {} to make it more verbose",
22            "RUST_BACKTRACE=1".italic().yellow()
23        )
24    } else {
25        "".to_string()
26    };
27
28    let mut message = format!(
29        "\n{}: {e}\n{maybe_backtrace}\n",
30        "Phink got an error...".red().bold(),
31    );
32
33    if e.backtrace().status() == BacktraceStatus::Captured {
34        message = format!(
35            "{}\n{}\n{}",
36            message,
37            "More informations —>".yellow(),
38            e.backtrace()
39        )
40    }
41
42    let mut source = e.source();
43    while let Some(cause) = source {
44        let arrow = "—> ".cyan().bold();
45        message = format!("{message}\n{arrow} {cause}\n");
46        source = cause.source();
47    }
48
49    message
50}