Files
llmx/codex-rs/tui/src/tui.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

use std::io::Result;
use std::io::Stdout;
use std::io::stdout;
use codex_core::config::Config;
use crossterm::event::DisableBracketedPaste;
use crossterm::event::EnableBracketedPaste;
use ratatui::backend::CrosstermBackend;
use ratatui::crossterm::execute;
use ratatui::crossterm::terminal::disable_raw_mode;
use ratatui::crossterm::terminal::enable_raw_mode;
use crate::custom_terminal::Terminal;
/// A type alias for the terminal type used in this application
pub type Tui = Terminal<CrosstermBackend<Stdout>>;
/// Initialize the terminal (inline viewport; history stays in normal scrollback)
pub fn init(_config: &Config) -> Result<Tui> {
execute!(stdout(), EnableBracketedPaste)?;
enable_raw_mode()?;
set_panic_hook();
let backend = CrosstermBackend::new(stdout());
let tui = Terminal::with_options(backend)?;
Ok(tui)
}
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() -> Result<()> {
execute!(stdout(), DisableBracketedPaste)?;
disable_raw_mode()?;
Ok(())
}