TerminalHandle

Struct TerminalHandle 

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

Handle to a running terminal instance.

The handle allows you to write input to the terminal and resize it. Multiple Terminal components can share the same handle.

The PTY is automatically closed when the handle is dropped.

Implementations§

Source§

impl TerminalHandle

Source

pub fn new( id: TerminalId, command: CommandBuilder, scrollback_length: Option<usize>, ) -> Result<Self, TerminalError>

Create a new terminal with the specified command and default scrollback size (1000 lines).

§Example
use freya_terminal::prelude::*;
use portable_pty::CommandBuilder;

let mut cmd = CommandBuilder::new("bash");
cmd.env("TERM", "xterm-256color");

let handle = TerminalHandle::new(TerminalId::new(), cmd, None).unwrap();
Source

pub fn write(&self, data: &[u8]) -> Result<(), TerminalError>

Write data to the terminal.

§Example
handle.write(b"ls -la\n").unwrap();
Source

pub fn resize(&self, rows: u16, cols: u16)

Resize the terminal to the specified rows and columns.

§Example
handle.resize(24, 80);
Source

pub fn scroll(&self, delta: i32)

Scroll the terminal by the specified delta.

§Example
handle.scroll(-3); // Scroll up 3 lines
handle.scroll(3); // Scroll down 3 lines
Source

pub fn scroll_to_bottom(&self)

Scroll the terminal to the bottom.

§Example
handle.scroll_to_bottom();
Source

pub fn scrollback_position(&self) -> usize

Get the current scrollback position (scroll offset from buffer).

§Example
let position = handle.scrollback_position();
Source

pub fn send_wheel_to_pty(&self, row: usize, col: usize, delta_y: f64)

Send a wheel event to the PTY.

This sends mouse wheel events as escape sequences to the running process. Uses the currently active mouse protocol encoding based on what the application has requested via DECSET sequences.

Source

pub fn mouse_move(&self, row: usize, col: usize)

Send a mouse move/drag event to the PTY based on the active mouse mode.

  • AnyMotion (DECSET 1003): sends motion events regardless of button state.
  • ButtonMotion (DECSET 1002): sends motion events only while a button is held.

When dragging, the held button is encoded in the motion event so TUI apps can implement their own text selection.

If shift is held and a button is pressed, updates the text selection instead of sending events to the PTY.

Source

pub fn mouse_down(&self, row: usize, col: usize, button: TerminalMouseButton)

Handle a mouse button press event.

When the running application has enabled mouse tracking (e.g. vim, helix, htop), this sends the press escape sequence to the PTY. Otherwise it starts a text selection.

If shift is held, text selection is always performed regardless of the application’s mouse tracking state.

Source

pub fn mouse_up(&self, row: usize, col: usize, button: TerminalMouseButton)

Handle a mouse button release event.

When the running application has enabled mouse tracking, this sends the release escape sequence to the PTY. Only PressRelease, ButtonMotion, and AnyMotion modes receive release events — Press mode does not. Otherwise it ends the current text selection.

If shift is held, always ends the text selection instead of sending events to the PTY.

Source

pub fn wheel(&self, delta_y: f64, row: usize, col: usize)

Handle a wheel event intelligently.

The behavior depends on the terminal state:

  • If viewing scrollback history: scrolls the scrollback buffer.
  • If mouse tracking is enabled (e.g., vim, helix): sends wheel escape sequences to the PTY.
  • If on the alternate screen without mouse tracking (e.g., gitui, less): sends arrow key sequences to the PTY (alternate scroll mode, like wezterm/kitty/alacritty).
  • Otherwise (normal shell): scrolls the scrollback buffer.
Source

pub fn read_buffer(&self) -> Ref<'_, TerminalBuffer>

Read the current terminal buffer.

Source

pub fn output_received(&self) -> impl Future<Output = ()> + '_

Returns a future that completes when new output is received from the PTY.

Can be called repeatedly in a loop to detect ongoing output activity.

Source

pub fn last_write_elapsed(&self) -> Duration

Source

pub fn closed(&self) -> impl Future<Output = ()> + '_

Returns a future that completes when the terminal/PTY closes.

This can be used to detect when the shell process exits and update the UI accordingly.

§Example
use_future(move || async move {
    terminal_handle.closed().await;
    // Terminal has exited, update UI state
});
Source

pub fn id(&self) -> TerminalId

Returns the unique identifier for this terminal instance.

Source

pub fn shift_pressed(&self, pressed: bool)

Track whether shift is currently pressed.

This should be called from your key event handlers to track shift state for shift+drag text selection.

Source

pub fn get_selection(&self) -> Option<TerminalSelection>

Get the current text selection.

Source

pub fn set_selection(&self, selection: Option<TerminalSelection>)

Set the text selection.

Source

pub fn start_selection(&self, row: usize, col: usize)

Source

pub fn update_selection(&self, row: usize, col: usize)

Source

pub fn end_selection(&self)

Source

pub fn clear_selection(&self)

Clear the current selection.

Source

pub fn get_selected_text(&self) -> Option<String>

Trait Implementations§

Source§

impl Clone for TerminalHandle

Source§

fn clone(&self) -> TerminalHandle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl PartialEq for TerminalHandle

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> ComponentProps for T
where T: Any + PartialEq,

§

fn changed(&self, other: &(dyn ComponentProps + 'static)) -> bool

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more