Skip to main content

TerminalSession

Struct TerminalSession 

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

A terminal session that manages raw mode and cleanup.

This struct owns the terminal configuration and ensures cleanup on drop. It tracks all enabled modes and disables them in reverse order when dropped.

§Contract

  • Exclusive ownership: Only one TerminalSession should exist at a time. Creating multiple sessions will cause undefined terminal behavior.

  • Raw mode entry: Creating a session automatically enters raw mode. This disables line buffering and echo.

  • Cleanup guarantee: When dropped (normally or via panic), all enabled modes are disabled and the terminal is restored to its previous state.

§State Tracking

Each optional mode has a corresponding _enabled flag. These flags are set when a mode is successfully enabled and cleared during cleanup. This ensures we only disable modes that were actually enabled.

§Example

use ftui_core::terminal_session::{TerminalSession, SessionOptions};

fn run_app() -> std::io::Result<()> {
    let session = TerminalSession::new(SessionOptions {
        alternate_screen: true,
        mouse_capture: true,
        ..Default::default()
    })?;

    // Application loop
    loop {
        if session.poll_event(std::time::Duration::from_millis(100))? {
            if let Some(event) = session.read_event()? {
                // Handle event...
            }
        }
    }
    // Session cleaned up when dropped
}

Implementations§

Source§

impl TerminalSession

Source

pub fn new(options: SessionOptions) -> Result<Self>

Enter raw mode and optionally enable additional features.

§Errors

Returns an error if raw mode cannot be enabled.

Source

pub fn minimal() -> Result<Self>

Create a minimal session (raw mode only).

Source

pub fn size(&self) -> Result<(u16, u16)>

Get the current terminal size (columns, rows).

Source

pub fn poll_event(&self, timeout: Duration) -> Result<bool>

Poll for an event with a timeout.

Returns Ok(true) if an event is available, Ok(false) if timeout.

Source

pub fn read_event(&self) -> Result<Option<Event>>

Read the next event (blocking until available).

Returns Ok(None) if the event cannot be represented by the ftui canonical event types (e.g. unsupported key codes).

Source

pub fn show_cursor(&self) -> Result<()>

Show the cursor.

Source

pub fn hide_cursor(&self) -> Result<()>

Hide the cursor.

Source

pub fn options(&self) -> &SessionOptions

Get the session options.

Trait Implementations§

Source§

impl Debug for TerminalSession

Source§

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

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

impl Drop for TerminalSession

Source§

fn drop(&mut self)

Executes the destructor for this type. 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, 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.