mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-29 22:11:34 +00:00

This is a simplification of the public API that is helpful for new users that are not familiar with how rust re-exports work, and helps avoid clashes with other modules in the backends that are named terminal. BREAKING CHANGE: The `terminal` module is now private and can not be used directly. The types under this module are exported from the root of the crate. ```diff - use ratatui::terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, ViewPort}; + use ratatui::{CompletedFrame, Frame, Terminal, TerminalOptions, ViewPort}; ``` Fixes: https://github.com/ratatui-org/ratatui/issues/1210
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use std::{
|
|
io::{self, stdout},
|
|
time::Duration,
|
|
};
|
|
|
|
use color_eyre::{eyre::WrapErr, Result};
|
|
use ratatui::{
|
|
backend::{Backend, CrosstermBackend},
|
|
crossterm::{
|
|
event::{self, Event},
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
ExecutableCommand,
|
|
},
|
|
layout::Rect,
|
|
Terminal, TerminalOptions, Viewport,
|
|
};
|
|
|
|
pub fn init() -> Result<Terminal<impl Backend>> {
|
|
// this size is to match the size of the terminal when running the demo
|
|
// using vhs in a 1280x640 sized window (github social preview size)
|
|
let options = TerminalOptions {
|
|
viewport: Viewport::Fixed(Rect::new(0, 0, 81, 18)),
|
|
};
|
|
let terminal = Terminal::with_options(CrosstermBackend::new(io::stdout()), options)?;
|
|
enable_raw_mode().context("enable raw mode")?;
|
|
stdout()
|
|
.execute(EnterAlternateScreen)
|
|
.wrap_err("enter alternate screen")?;
|
|
Ok(terminal)
|
|
}
|
|
|
|
pub fn restore() -> Result<()> {
|
|
disable_raw_mode().context("disable raw mode")?;
|
|
stdout()
|
|
.execute(LeaveAlternateScreen)
|
|
.wrap_err("leave alternate screen")?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn next_event(timeout: Duration) -> Result<Option<Event>> {
|
|
if !event::poll(timeout)? {
|
|
return Ok(None);
|
|
}
|
|
let event = event::read()?;
|
|
Ok(Some(event))
|
|
}
|