TerminalView

Struct TerminalView 

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

The main terminal view component for GPUI applications.

TerminalView is a GPUI entity that implements the Render trait, providing a complete terminal emulator that can be embedded in any GPUI application.

§Responsibilities

  • Terminal State: Manages the grid, cursor, and colors via TerminalState
  • I/O Streams: Reads from PTY stdout and writes to PTY stdin
  • Event Handling: Processes keyboard, mouse, and resize events
  • Rendering: Paints text, backgrounds, and cursor via TerminalRenderer
  • Callbacks: Dispatches events to user-provided callbacks

§Creating a Terminal

Use TerminalView::new within a GPUI entity context:

let terminal = cx.new(|cx| {
    TerminalView::new(writer, reader, config, cx)
        .with_resize_callback(resize_callback)
        .with_exit_callback(|_, cx| cx.quit())
});

§Focus

The terminal must be focused to receive keyboard input:

terminal.read(cx).focus_handle().focus(window);

§Callbacks

Configure behavior through builder methods:

§Thread Safety

TerminalView is not Send as it contains GPUI handles. The stdin writer is internally wrapped in Arc<parking_lot::Mutex<>> for safe concurrent access.

Implementations§

Source§

impl TerminalView

Source

pub fn new<W, R>( stdin_writer: W, stdout_reader: R, config: TerminalConfig, cx: &mut Context<'_, Self>, ) -> Self
where W: Write + Send + 'static, R: Read + Send + 'static,

Create a new terminal with provided I/O streams.

This method initializes a new terminal emulator with the given stdin writer and stdout reader. It spawns a background task to read from stdout and process incoming bytes through the VTE parser.

§Arguments
  • stdin_writer - Writer for sending input bytes to the terminal process
  • stdout_reader - Reader for receiving output bytes from the terminal process
  • config - Terminal configuration (dimensions, font, etc.)
  • cx - GPUI context for this view
§Returns

A new TerminalView instance ready to be rendered.

§Examples
// In a GPUI window context:
let terminal = cx.new(|cx| {
    TerminalView::new(stdin_writer, stdout_reader, TerminalConfig::default(), cx)
});
Source

pub fn with_resize_callback( self, callback: impl Fn(usize, usize) + Send + Sync + 'static, ) -> Self

Set a callback to be invoked when the terminal is resized.

This callback should resize the underlying PTY to match the new dimensions. The callback receives (cols, rows) as arguments.

§Arguments
  • callback - A function that will be called with (cols, rows) on resize
Source

pub fn with_key_handler( self, handler: impl Fn(&KeyDownEvent) -> bool + Send + Sync + 'static, ) -> Self

Set a callback to intercept key events before terminal processing.

The callback receives the key event and should return true to consume the event (prevent the terminal from processing it), or false to allow normal terminal processing.

§Arguments
  • handler - A function that receives key events and returns whether to consume them
§Example
terminal.with_key_handler(|event| {
    // Handle Ctrl++ to increase font size
    if event.keystroke.modifiers.control && event.keystroke.key == "+" {
        // Handle the event
        return true; // Consume the event
    }
    false // Let terminal handle it
})
Source

pub fn with_bell_callback( self, callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>) + 'static, ) -> Self

Set a callback to be invoked when the terminal bell is triggered.

The callback receives a mutable reference to the window and context, allowing you to play a sound or show a visual indicator.

§Arguments
  • callback - A function that will be called when the bell is triggered
§Example
terminal.with_bell_callback(|window, cx| {
    // Play a sound or flash the screen
})
Source

pub fn with_title_callback( self, callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>, &str) + 'static, ) -> Self

Set a callback to be invoked when the terminal title changes.

The callback receives a mutable reference to the window and context, along with the new title string.

§Arguments
  • callback - A function that will be called with the new title
§Example
terminal.with_title_callback(|window, cx, title| {
    // Update window title or tab title
})
Source

pub fn with_clipboard_store_callback( self, callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>, &str) + 'static, ) -> Self

Set a callback to be invoked when the terminal wants to store data to the clipboard.

The callback receives a mutable reference to the window and context, along with the text to store. This is typically triggered by OSC 52 escape sequences.

§Arguments
  • callback - A function that will be called with the text to store
§Example
terminal.with_clipboard_store_callback(|window, cx, text| {
    // Store text to system clipboard
})
Source

pub fn with_exit_callback( self, callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>) + 'static, ) -> Self

Set a callback to be invoked when the terminal process exits.

The callback receives a mutable reference to the window and context, allowing you to close the terminal view or show an exit message.

§Arguments
  • callback - A function that will be called when the process exits
§Example
terminal.with_exit_callback(|window, cx| {
    // Close the terminal tab or show exit message
})
Source

pub fn dimensions(&self) -> (usize, usize)

Get the current terminal dimensions.

§Returns

A tuple of (columns, rows).

Source

pub fn resize(&mut self, cols: usize, rows: usize)

Resize the terminal to new dimensions.

This method should be called when the terminal view size changes. It updates the internal grid and notifies the terminal process of the new size.

§Arguments
  • cols - New number of columns
  • rows - New number of rows
Source

pub fn config(&self) -> &TerminalConfig

Get the current terminal configuration.

§Returns

A reference to the current configuration.

Source

pub fn focus_handle(&self) -> &FocusHandle

Get the focus handle for this terminal view.

§Returns

A reference to the focus handle.

Source

pub fn update_config( &mut self, config: TerminalConfig, cx: &mut Context<'_, Self>, )

Update the terminal configuration.

This method updates the terminal’s configuration, including font settings, padding, and color palette. Changes take effect on the next render.

§Arguments
  • config - The new configuration to apply
  • cx - The context for triggering a repaint

Trait Implementations§

Source§

impl Render for TerminalView

Source§

fn render( &mut self, window: &mut Window, cx: &mut Context<'_, Self>, ) -> impl IntoElement

Render this view into an element tree.

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> Downcast for T
where T: Any,

Source§

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

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

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

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.

Source§

impl<T> Instrument for T

Source§

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

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

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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