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:
with_resize_callback- PTY size changeswith_exit_callback- Process exitwith_key_handler- Key event interceptionwith_bell_callback- Terminal bellwith_title_callback- Title changeswith_clipboard_store_callback- Clipboard writes
§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
impl TerminalView
Sourcepub fn new<W, R>(
stdin_writer: W,
stdout_reader: R,
config: TerminalConfig,
cx: &mut Context<'_, Self>,
) -> Self
pub fn new<W, R>( stdin_writer: W, stdout_reader: R, config: TerminalConfig, cx: &mut Context<'_, Self>, ) -> Self
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 processstdout_reader- Reader for receiving output bytes from the terminal processconfig- 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)
});Sourcepub fn with_resize_callback(
self,
callback: impl Fn(usize, usize) + Send + Sync + 'static,
) -> Self
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
Sourcepub fn with_key_handler(
self,
handler: impl Fn(&KeyDownEvent) -> bool + Send + Sync + 'static,
) -> Self
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
})Sourcepub fn with_bell_callback(
self,
callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>) + 'static,
) -> Self
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
})Sourcepub fn with_title_callback(
self,
callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>, &str) + 'static,
) -> Self
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
})Sourcepub fn with_clipboard_store_callback(
self,
callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>, &str) + 'static,
) -> Self
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
})Sourcepub fn with_exit_callback(
self,
callback: impl Fn(&mut Window, &mut Context<'_, TerminalView>) + 'static,
) -> Self
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
})Sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Sourcepub fn resize(&mut self, cols: usize, rows: usize)
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 columnsrows- New number of rows
Sourcepub fn config(&self) -> &TerminalConfig
pub fn config(&self) -> &TerminalConfig
Sourcepub fn focus_handle(&self) -> &FocusHandle
pub fn focus_handle(&self) -> &FocusHandle
Sourcepub fn update_config(
&mut self,
config: TerminalConfig,
cx: &mut Context<'_, Self>,
)
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 applycx- The context for triggering a repaint
Trait Implementations§
Source§impl Render for TerminalView
impl Render for TerminalView
Auto Trait Implementations§
impl Freeze for TerminalView
impl !RefUnwindSafe for TerminalView
impl !Send for TerminalView
impl !Sync for TerminalView
impl Unpin for TerminalView
impl !UnwindSafe for TerminalView
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> 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