mirror of
https://github.com/openai/codex.git
synced 2026-04-24 22:54:54 +00:00
Some effects of this change: - New formatting changes across many files. No functionality changes should occur from that. - Calls to `set_env` are considered unsafe, since this only happens in tests we wrap them in `unsafe` blocks
42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
use std::io::Stdout;
|
|
use std::io::stdout;
|
|
use std::io::{self};
|
|
|
|
use crossterm::event::DisableMouseCapture;
|
|
use crossterm::event::EnableMouseCapture;
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::CrosstermBackend;
|
|
use ratatui::crossterm::execute;
|
|
use ratatui::crossterm::terminal::EnterAlternateScreen;
|
|
use ratatui::crossterm::terminal::LeaveAlternateScreen;
|
|
use ratatui::crossterm::terminal::disable_raw_mode;
|
|
use ratatui::crossterm::terminal::enable_raw_mode;
|
|
|
|
/// A type alias for the terminal type used in this application
|
|
pub type Tui = Terminal<CrosstermBackend<Stdout>>;
|
|
|
|
/// Initialize the terminal
|
|
pub fn init() -> io::Result<Tui> {
|
|
execute!(stdout(), EnterAlternateScreen)?;
|
|
execute!(stdout(), EnableMouseCapture)?;
|
|
enable_raw_mode()?;
|
|
set_panic_hook();
|
|
Terminal::new(CrosstermBackend::new(stdout()))
|
|
}
|
|
|
|
fn set_panic_hook() {
|
|
let hook = std::panic::take_hook();
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
|
let _ = restore(); // ignore any errors as we are already failing
|
|
hook(panic_info);
|
|
}));
|
|
}
|
|
|
|
/// Restore the terminal to its original state
|
|
pub fn restore() -> io::Result<()> {
|
|
execute!(stdout(), DisableMouseCapture)?;
|
|
execute!(stdout(), LeaveAlternateScreen)?;
|
|
disable_raw_mode()?;
|
|
Ok(())
|
|
}
|