Skip to main content

TerminalGuard

Struct TerminalGuard 

Source
pub struct TerminalGuard { /* private fields */ }
Expand description

An RAII guard that turns on terminal mouse capture (and optionally the keyboard-enhancement protocol) for as long as it is alive, and guarantees the terminal is put back the way it was — both on the normal exit path (via Drop) and on any panic (via a wrapped panic hook).

Create it after your terminal has been put into raw mode / the alternate screen (e.g. after ratatui::init()), and drop it before you tear that down (e.g. before ratatui::restore()):

use tui_panel_select::TerminalGuard;

let mut terminal = ratatui::init();
let guard = TerminalGuard::install(true)?;
let enhanced = guard.keyboard_enhancement_active();

// ... run your event loop, using `enhanced` to decide whether modifier
// combinations like Ctrl+Enter are reported distinctly ...

drop(guard); // restore mouse capture / keyboard flags
ratatui::restore();

Implementations§

Source§

impl TerminalGuard

Source

pub fn install(keyboard_enhancement: bool) -> Result<Self>

Enable mouse capture and install the panic-safe restore hook.

When keyboard_enhancement is true and the terminal advertises support, the keyboard-enhancement (disambiguate-escape-codes) protocol is pushed as well, so modifier combinations such as Ctrl+Enter are reported distinctly from a plain Enter. Use keyboard_enhancement_active to learn whether it actually took effect.

The current panic hook is taken and wrapped: on any panic the wrapper disables mouse capture (and pops the keyboard-enhancement flags, if they were pushed) before delegating to the previous hook.

Source

pub fn keyboard_enhancement_active(&self) -> bool

Whether the keyboard-enhancement protocol was actually enabled (it is only enabled when requested and supported by the terminal).

Trait Implementations§

Source§

impl Debug for TerminalGuard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for TerminalGuard

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.