Skip to content

fix: show panic output when fullscreen app panic#114

Open
hsqStephenZhang wants to merge 3 commits into
ccbrown:mainfrom
hsqStephenZhang:main
Open

fix: show panic output when fullscreen app panic#114
hsqStephenZhang wants to merge 3 commits into
ccbrown:mainfrom
hsqStephenZhang:main

Conversation

@hsqStephenZhang
Copy link
Copy Markdown
Contributor

What It Does

setup panic handler before enter fullscreen mode
in the handler, leave current screen before printing the backtrace, so that panic output own't get lost along with that screen

Related Issues

#103

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.92%. Comparing base (4c8bf90) to head (c55a637).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #114   +/-   ##
=======================================
  Coverage   87.92%   87.92%           
=======================================
  Files          35       35           
  Lines        5617     5617           
  Branches     5617     5617           
=======================================
  Hits         4939     4939           
  Misses        569      569           
  Partials      109      109           

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccbrown
Copy link
Copy Markdown
Owner

ccbrown commented Jul 23, 2025

Thanks for the PR! I like the idea, but I don't love exposing crossterm and requiring users to use it directly. If the stack is allowed to unwind, fullscreen will be exited normally, so I wonder if something like this would be better:

diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs
index 41d4424..ebf87fb 100644
--- a/examples/fullscreen.rs
+++ b/examples/fullscreen.rs
@@ -1,6 +1,10 @@
 use chrono::Local;
 use iocraft::prelude::*;
-use std::time::Duration;
+use std::{backtrace::Backtrace, cell::Cell, time::Duration};
+
+thread_local! {
+    static BACKTRACE: Cell<Option<Backtrace>> = const { Cell::new(None) };
+}
 
 #[component]
 fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
@@ -21,6 +25,7 @@ fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
             TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => {
                 match code {
                     KeyCode::Char('q') => should_exit.set(true),
+                    KeyCode::Char('p') => panic!("oh no!"),
                     _ => {}
                 }
             }
@@ -54,11 +59,23 @@ fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
             ) {
                 Text(content: format!("Current Time: {}", time.get().format("%r")))
             }
-            Text(content: "Press \"q\" to quit.")
+            Text(content: "Press \"q\" to quit or \"p\" to panic.")
         }
     }
 }
 
 fn main() {
-    smol::block_on(element!(Example).fullscreen()).unwrap();
+    std::panic::set_hook(Box::new(|_| {
+        let trace = Backtrace::capture();
+        BACKTRACE.with(move |b| b.set(Some(trace)));
+    }));
+
+    if std::panic::catch_unwind(|| {
+        smol::block_on(element!(Example).fullscreen()).unwrap();
+    })
+    .is_err()
+    {
+        let b = BACKTRACE.with(|b| b.take()).unwrap();
+        println!("panic:\n\n{b}");
+    }
 }

@hsqStephenZhang
Copy link
Copy Markdown
Contributor Author

yeah agreed, catching and print the panic explictly info a better approach without exposing crossterm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants