pub struct TerminalState { /* private fields */ }Expand description
Thread-safe terminal state wrapper.
This struct wraps alacritty’s Term in an
Arc<parking_lot::Mutex<>> to allow safe concurrent access from multiple threads.
It also manages the VTE parser for processing incoming bytes from the PTY.
§Thread Safety
The terminal state can be safely shared across threads:
- Use
term_arcto get a clonedArcfor sharing - Use
with_termfor read access to the grid - Use
with_term_mutfor write access
The mutex is held only for the duration of the closure, minimizing contention.
§Grid Access
The terminal grid is accessed through the Term structure:
terminal_state.with_term(|term| {
let grid = term.grid();
let cursor = grid.cursor.point;
let cell = &grid[cursor];
// Read cell content, colors, flags, etc.
});§Performance Notes
parking_lot::Mutexis used for faster locking thanstd::sync::Mutex- Lock contention is minimized by keeping critical sections short
- The VTE parser state is kept outside the mutex (only accessed from one thread)
Implementations§
Source§impl TerminalState
impl TerminalState
Sourcepub fn new(cols: usize, rows: usize, event_proxy: GpuiEventProxy) -> Self
pub fn new(cols: usize, rows: usize, event_proxy: GpuiEventProxy) -> Self
Create a new terminal state with the given dimensions.
§Arguments
cols- The number of columns (character width) of the terminalrows- The number of rows (lines) of the terminalevent_proxy- The event proxy for forwarding terminal events to GPUI
§Returns
A new TerminalState instance.
§Examples
use std::sync::mpsc::channel;
use gpui_terminal::event::GpuiEventProxy;
use gpui_terminal::terminal::TerminalState;
let (tx, rx) = channel();
let event_proxy = GpuiEventProxy::new(tx);
let terminal = TerminalState::new(80, 24, event_proxy);Sourcepub fn process_bytes(&mut self, bytes: &[u8])
pub fn process_bytes(&mut self, bytes: &[u8])
Process incoming bytes from the PTY.
This method feeds the bytes through the VTE parser, which will call the appropriate handler methods on the terminal to update its state.
§Arguments
bytes- The bytes received from the PTY
§Examples
// Process some output from the PTY
terminal.process_bytes(b"Hello, world!\r\n");Sourcepub fn mode(&self) -> TermMode
pub fn mode(&self) -> TermMode
Get the current terminal mode.
The terminal mode affects how certain key sequences are interpreted, particularly arrow keys in application cursor mode.
§Returns
The current TermMode flags.
§Examples
use alacritty_terminal::term::TermMode;
let mode = terminal.mode();
if mode.contains(TermMode::APP_CURSOR) {
println!("Application cursor mode is enabled");
}Sourcepub fn with_term<F, R>(&self, f: F) -> R
pub fn with_term<F, R>(&self, f: F) -> R
Execute a function with read access to the terminal.
This method provides safe read access to the underlying Term structure.
The terminal is locked for the duration of the function call.
§Arguments
f- A function that takes a reference to theTermand returns a value
§Returns
The value returned by the function f.
§Examples
let cursor_pos = terminal.with_term(|term| {
term.grid().cursor.point
});Sourcepub fn with_term_mut<F, R>(&self, f: F) -> R
pub fn with_term_mut<F, R>(&self, f: F) -> R
Execute a function with mutable access to the terminal.
This method provides safe write access to the underlying Term structure.
The terminal is locked for the duration of the function call.
§Arguments
f- A function that takes a mutable reference to theTermand returns a value
§Returns
The value returned by the function f.
§Examples
terminal.with_term_mut(|term| {
// Perform some mutation on the term
term.scroll_display(alacritty_terminal::grid::Scroll::Delta(5));
});Auto Trait Implementations§
impl Freeze for TerminalState
impl !RefUnwindSafe for TerminalState
impl Send for TerminalState
impl Sync for TerminalState
impl Unpin for TerminalState
impl !UnwindSafe for TerminalState
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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