eyre-rs / eyre

A trait object based error handling type for easy idiomatic error handling and reporting in Rust applications
Apache License 2.0
1.35k stars 63 forks source link

enable raw mode compatibility for panic & error reports #142

Closed slordua closed 3 months ago

slordua commented 8 months ago

a project I'm working on utilizes raw mode, invoked via crossterm

this prints reports all funny.. this is my solution in color_eyre for now, but it would be nice to see this integrated more elegantly. an enable_raw_mode: bool in HookBuilder could be ok.

use crossterm::terminal::{disable_raw_mode, enable_raw_mode};

// error
// color_eyre::Handler

impl eyre::EyreHandler for Handler {
    fn debug(
        &self,
        error: &(dyn std::error::Error + 'static),
        f: &mut core::fmt::Formatter<'_>,
    ) -> core::fmt::Result {
        let _ = disable_raw_mode();

        // ..

        let _ = enable_raw_mode();

        Ok(())
    }
}

// panic
pub struct RawModePanic;

impl PanicMessage for RawModePanic {
    fn display(
        &self,
        pi: &std::panic::PanicInfo<'_>,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        let _ = disable_raw_mode();

        // ..

        Ok(())
    }
}

async fn main() -> Result<()> {
    enable_raw_mode()?;

    color_eyre::config::HookBuilder::default()
        .panic_message(RawModePanic)
        .install()?;

    Ok(())
}

for panic, there's no need to re-enable raw mode as we're exiting, but one thing i'm not accounting for here is un-recoverable error, in which case the terminal would be left in raw on exit. need match on ErrorKind perhaps whether to re-enable or something

slordua commented 3 months ago

solved this by just using a guard with drop impl