-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The documented panic handler does not exit raw mode correctly in the Termion backend #1005
Comments
I pointed out this issue while adding this code to the website: ratatui-org/ratatui-website#69 It might be an issue with termion and I'm thinking if we should move this issue to the website repository or not... |
https://gitlab.redox-os.org/redox-os/termion/-/blob/master/src/raw.rs?ref_type=heads#L44-48 Also note that That is to say, the website doc is wrong and untested and should be fixed. Would you want to submit a PR for this? BTW, I'm curious about your rationale for choosing termion over crossterm for your app. Do you have any thoughts you can share on this choice? |
From what I understand from the source that you linked ( https://gitlab.redox-os.org/redox-os/termion/-/blob/master/src/raw.rs?ref_type=heads#L44-48 ), creating a new io::stdout() / io::stderr() sets the One more reason to implement what I requested in #1006 , so that I can switch back and forth from raw mode at least during normal program execution, if not in the panic handler😉
I chose Termion for 3 main reasons:
|
yep, that's basically how the underlying libc call works: man cfmakeraw:
...
yep agreed
Makes sense :) I think there's still likely a problem with having a panic handler and using raw mode with termion, and that can probably only really be fixed by termion (or by providing your own code to do basically the same thing as termion does earlier on in the calls (save the termios value and restore it when panicking).. |
See https://gitlab.redox-os.org/redox-os/termion/-/issues/176 for more discussion on this. |
I managed to write a panic handler that works: use std::{
io::{self, Write},
panic,
};
use anyhow::{Context, Result};
use ratatui::prelude::*;
use termion::{input::MouseTerminal, raw::IntoRawMode, screen::IntoAlternateScreen};
fn initialize_panic_handler() -> Result<()> {
let raw_output = io::stdout().into_raw_mode()?;
raw_output.suspend_raw_mode()?;
let panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic| {
let panic_cleanup = || -> Result<()> {
let mut output = io::stdout();
write!(
output,
"{}{}{}",
termion::clear::All,
termion::screen::ToMainScreen,
termion::cursor::Show
)?;
raw_output.suspend_raw_mode()?;
output.flush()?;
Ok(())
};
panic_cleanup().expect("failed to clean up for panic");
panic_hook(panic);
}));
Ok(())
}
fn main() -> Result<()> {
initialize_panic_handler().context("failed to initialize panic handler")?;
let stdout = MouseTerminal::from(
io::stdout()
.into_raw_mode()
.context("failed to enable raw mode")?
.into_alternate_screen()
.context("unable to enter alternate screen")?,
);
let mut terminal =
Terminal::new(TermionBackend::new(stdout)).context("creating terminal failed")?; In the So if you want to update the docs with a working implementation, there you have it😉 |
see ratatui-org/ratatui#1005 move how-to-panic code to rust project and rewrite article --------- Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
Description
Following the code on : https://ratatui.rs/how-to/develop-apps/panic-hooks/
I'm using the Termion backend.
The line in the docs
should in theory disable raw mode from the terminal, but in reality it does not.
The '\n' characters on strdout/stderrr go to a new line, but they don't place the cursor at the beginning of the line, thus resulting in a weird output.
To Reproduce
painc!()
anywhere in the app and see the stdout/stderr with weird newline behaviour.Expected behavior
Screenshots
Environment
Additional context
I don't know if this should be a bug in Termion itself.
The text was updated successfully, but these errors were encountered: