Proof of concept for a resizable viewport. The general approach here is to duplicate the `Terminal` struct from ratatui, but with our own logic. This is a "light fork" in that we are still using all the base ratatui functions (`Buffer`, `Widget` and so on), but we're doing our own bookkeeping at the top level to determine where to draw everything. This approach could use improvement—e.g, when the window is resized to a smaller size, if the UI wraps, we don't correctly clear out the artifacts from wrapping. This is possible with a little work (i.e. tracking what parts of our UI would have been wrapped), but this behavior is at least at par with the existing behavior. https://github.com/user-attachments/assets/4eb17689-09fd-4daa-8315-c7ebc654986d cc @joshka who might have Thoughts™
44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
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(())
|
|
}
|