pub struct Tui {
terminal: Option<DefaultTerminal>,
event_rx: UnboundedReceiver<Event>,
event_tx: UnboundedSender<Event>,
task: Option<JoinHandle<()>>,
cancellation_token: CancellationToken,
tick_rate: f64,
frame_rate: f64,
}Expand description
Terminal UI wrapper managing lifecycle and event delivery.
The lifecycle is split into two phases:
Tui::new()— creates channels and config; no terminal side effectsTui::enter()— initializes the terminal, enters raw mode, spawns event task
This split ensures config loading and stderr output happen before the terminal is taken over.
Fields§
§terminal: Option<DefaultTerminal>The ratatui terminal instance (initialized in enter()).
event_rx: UnboundedReceiver<Event>Receiver for events from the event handler task.
event_tx: UnboundedSender<Event>Sender for events (cloned into the event task).
task: Option<JoinHandle<()>>Handle to the spawned event handler task.
cancellation_token: CancellationTokenCancellation token to stop the event handler task.
tick_rate: f64Target tick rate (ticks per second).
frame_rate: f64Target frame rate (frames per second).
Implementations§
Source§impl Tui
impl Tui
Sourcepub fn new(tick_rate: f64, frame_rate: f64) -> Self
pub fn new(tick_rate: f64, frame_rate: f64) -> Self
Create a new TUI instance without initializing the terminal.
No side effects — the terminal remains in normal mode.
Call Tui::enter to activate raw mode and start the event
handler task.
Sourcepub fn enter(&mut self) -> Result<()>
pub fn enter(&mut self) -> Result<()>
Initialize the terminal and enter raw mode.
This enables raw mode, alternate screen, mouse capture, focus change events, and bracketed paste. It also installs a panic hook that restores the terminal before displaying the error report, and spawns the async event handler task.
§Errors
Returns an error if terminal setup fails (instead of panicking).
Sourcepub async fn next(&mut self) -> Result<Event>
pub async fn next(&mut self) -> Result<Event>
Wait for the next event from the event handler task.
§Errors
Returns an error if the event channel is closed unexpectedly.
Sourcepub fn exit(&mut self) -> Result<()>
pub fn exit(&mut self) -> Result<()>
Exit raw mode and restore the terminal.
Cancels the event handler task and restores the terminal state.
§Errors
Returns an error if terminal restoration fails.
Sourcepub fn draw(&mut self, f: impl FnOnce(&mut Frame<'_>)) -> Result<()>
pub fn draw(&mut self, f: impl FnOnce(&mut Frame<'_>)) -> Result<()>
Draw a frame using the provided rendering closure.
§Errors
Returns an error if the terminal is not initialized or drawing fails.
Sourcepub fn suspend(&mut self) -> Result<()>
pub fn suspend(&mut self) -> Result<()>
Temporarily suspend the TUI for an external process (e.g., editor).
Leaves alternate screen and disables raw mode so the subprocess
can interact with the terminal normally. Call resume() after
the subprocess exits.
§Errors
Returns an error if terminal state cannot be changed.
Sourcepub fn resume(&mut self) -> Result<()>
pub fn resume(&mut self) -> Result<()>
Resume the TUI after a suspend() call.
Re-enters raw mode and alternate screen. The event handler task is still running — it will resume delivering events.
§Errors
Returns an error if terminal state cannot be restored.
Sourcefn drain_events(&mut self)
fn drain_events(&mut self)
Discard all queued events from the event channel.
Called by resume to prevent stale input that
accumulated during a suspend from being
interpreted as TUI commands. The background event handler task
keeps running during suspend, so its EventStream can race
with the child process for stdin and capture keypresses (e.g.,
q to quit an editor) that would otherwise cause unintended
actions like quitting the app.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tui
impl RefUnwindSafe for Tui
impl Send for Tui
impl Sync for Tui
impl Unpin for Tui
impl UnsafeUnpin for Tui
impl UnwindSafe for Tui
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more