More concise implementation of stderr capture.
This commit is contained in:
parent
dd40937cfa
commit
0b8c590dea
44
src/main.rs
44
src/main.rs
|
@ -1,7 +1,4 @@
|
||||||
use std::{
|
use std::{io::Write, path::Path};
|
||||||
io::{Read, Write},
|
|
||||||
path::Path,
|
|
||||||
};
|
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use editor::Editor;
|
use editor::Editor;
|
||||||
|
@ -37,38 +34,27 @@ fn main() {
|
||||||
Editor::new(ConsoleLineFormatter::new(4))
|
Editor::new(ConsoleLineFormatter::new(4))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Redirect stderr to an internal buffer, so we can print it after exiting.
|
// Holds stderr output in an internal buffer, and prints it when dropped.
|
||||||
let stderr_buf = gag::BufferRedirect::stderr().unwrap();
|
// This keeps stderr from being swallowed by the TUI.
|
||||||
|
let stderr_hold = gag::Hold::stderr().unwrap();
|
||||||
|
|
||||||
// Initialize and start UI
|
// Initialize and start UI.
|
||||||
let exec_result = std::panic::catch_unwind(|| {
|
let exec_result = std::panic::catch_unwind(|| {
|
||||||
let mut ui = TermUI::new_from_editor(editor);
|
let mut ui = TermUI::new_from_editor(editor);
|
||||||
ui.main_ui_loop();
|
ui.main_ui_loop();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check for panics. If we did panic, exit from raw mode and the alternate
|
// If we panicked, ensure that we've exited from raw mode and the alternate
|
||||||
// screen before propagating the panic, so that the panic printout actually
|
// screen before printing the error and resuming the panic.
|
||||||
// goes to a visible and scrollable screen.
|
if let Err(e) = exec_result {
|
||||||
match exec_result {
|
// Exit raw alt screen.
|
||||||
Ok(_) => {
|
crossterm::terminal::disable_raw_mode().unwrap();
|
||||||
// Print captured stderr.
|
crossterm::execute!(std::io::stdout(), crossterm::terminal::LeaveAlternateScreen).unwrap();
|
||||||
let mut msg = String::new();
|
|
||||||
stderr_buf.into_inner().read_to_string(&mut msg).unwrap();
|
|
||||||
eprint!("{}", msg);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
// Exit raw alt screen.
|
|
||||||
crossterm::terminal::disable_raw_mode().unwrap();
|
|
||||||
crossterm::execute!(std::io::stdout(), crossterm::terminal::LeaveAlternateScreen)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Print captured stderr.
|
// Print captured stderr.
|
||||||
let mut msg = String::new();
|
drop(stderr_hold);
|
||||||
stderr_buf.into_inner().read_to_string(&mut msg).unwrap();
|
|
||||||
eprint!("{}", msg);
|
|
||||||
|
|
||||||
// Resume panic unwind.
|
// Resume panic unwind.
|
||||||
std::panic::resume_unwind(e);
|
std::panic::resume_unwind(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user