From 9e617dde12565ba4e0ec737893b204a5e5271689 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Thu, 5 Mar 2026 08:45:45 +0100 Subject: some parsing, also add panic that does not spill --- src/panic.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/panic.rs (limited to 'src/panic.rs') diff --git a/src/panic.rs b/src/panic.rs new file mode 100644 index 0000000..0df0a4e --- /dev/null +++ b/src/panic.rs @@ -0,0 +1,48 @@ +use std::backtrace::{Backtrace, BacktraceStatus}; +use std::io::{self, Write}; +use std::panic::{self, PanicHookInfo}; +use std::sync::atomic::{AtomicBool, Ordering}; + +static EMIT_CARRIAGE_RETURN: AtomicBool = AtomicBool::new(false); + +pub fn enable_cr() { + EMIT_CARRIAGE_RETURN.store(true, Ordering::SeqCst); +} + +pub fn disable_cr() { + EMIT_CARRIAGE_RETURN.store(false, Ordering::SeqCst); +} + +// thread 'main' panicked at src/main.rs:59:25: +// not yet implemented: homedir +// stack backtrace: +// 0: __rustc::rust_begin_unwind at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/std/src/panicking.rs:697:5 +// 1: core::panicking::panic_fmt at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:75:14 +// 2: pish::run_command at ./src/main.rs:59:25 +// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace + +fn panic_hook(info: &PanicHookInfo<'_>) { + let mut msg = format!("{}\n", info); + + let bt = Backtrace::capture(); + match bt.status() { + BacktraceStatus::Unsupported => msg += "backtrace unsupported\n", + BacktraceStatus::Disabled => msg += "no backtrace", + _ => { + msg += &format!("{bt}\n"); + }, + } + + // TODO: add link to repo or sth + msg += "\n -== this is a bug ==-\n"; + + if EMIT_CARRIAGE_RETURN.load(Ordering::SeqCst) { + msg = msg.replace("\n", "\r\n"); + } + + let _ = io::stderr().write_all(msg.as_bytes()); +} + +pub fn hook() { + panic::set_hook(Box::new(panic_hook)); +} -- cgit v1.2.3