aboutsummaryrefslogtreecommitdiffstats
path: root/src/panic.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-03-05 08:45:45 +0100
committerJonas Maier <>2026-03-05 08:45:45 +0100
commit9e617dde12565ba4e0ec737893b204a5e5271689 (patch)
tree69ed1d0fb097e5df0101871904f7fefc4519adf2 /src/panic.rs
parentdb1d0bb7c37e2c2943fe0fd8b6857110fdd00087 (diff)
downloadpish-9e617dde12565ba4e0ec737893b204a5e5271689.tar.gz
some parsing, also add panic that does not spill
Diffstat (limited to 'src/panic.rs')
-rw-r--r--src/panic.rs48
1 files changed, 48 insertions, 0 deletions
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));
+}