freya_terminal/
lib.rs

1//! # Freya Terminal 🖥️
2//!
3//! Terminal emulator integration for Freya applications.
4//!
5//! This crate provides a way to embed interactive terminal emulators in your Freya applications.
6//! It uses PTY (pseudo-terminal) to spawn shell processes and renders VT100-compatible terminal output
7//! with full 256-color support.
8//!
9//! ## Features
10//!
11//! - **PTY Integration**: Spawn and interact with shell processes
12//! - **VT100 Rendering**: Full terminal emulation with cursor, colors, and text attributes
13//! - **256-Color Support**: ANSI 16 colors, 6x6x6 RGB cube, and 24-level grayscale
14//! - **Keyboard Input**: Handle all standard terminal key sequences
15//! - **Auto-resize**: Terminal automatically resizes based on available space
16//!
17//! ## Basic Usage
18//!
19//! ```rust,no_run
20//! use freya::prelude::*;
21//! use freya_terminal::prelude::*;
22//!
23//! fn main() {
24//!     launch(LaunchConfig::new().with_window(WindowConfig::new(app)))
25//! }
26//!
27//! fn app() -> impl IntoElement {
28//!     let mut handle = use_state(|| {
29//!         let mut cmd = CommandBuilder::new("bash");
30//!         cmd.env("TERM", "xterm-256color");
31//!         TerminalHandle::new(TerminalId::new(), cmd, None).ok()
32//!     });
33//!
34//!     let focus = use_focus();
35//!
36//!     rect().expanded().background((30, 30, 30)).child(
37//!         if let Some(handle) = handle.read().clone() {
38//!             rect()
39//!                 .child(
40//!                     Terminal::new(handle.clone())
41//!                         .a11y_id(focus.a11y_id())
42//!                         .on_mouse_down(move |_| focus.request_focus())
43//!                         .on_key_down(move |e: Event<KeyboardEventData>| {
44//!                             let _ = handle.write_key(&e.key, e.modifiers);
45//!                         }),
46//!                 )
47//!                 .expanded()
48//!                 .into_element()
49//!         } else {
50//!             "Failed to start Terminal.".into_element()
51//!         },
52//!     )
53//! }
54//! ```
55//!
56//! ## Handling Terminal Exit
57//!
58//! You can detect when the terminal/PTY closes using `TerminalHandle::closed`:
59//!
60//! ```rust,ignore
61//! use_future(move || async move {
62//!     terminal_handle.closed().await;
63//!     // Terminal has exited, update UI state
64//! });
65//! ```
66//!
67//! ## Advance usage
68//!
69//! Check the `feature_terminal.rs` example in the repository.
70pub mod buffer;
71pub mod colors;
72pub mod element;
73pub mod handle;
74pub mod parser;
75pub mod pty;
76
77/// Prelude module for convenient imports.
78pub mod prelude {
79    pub use portable_pty::CommandBuilder;
80
81    pub use crate::{
82        buffer::{
83            TerminalBuffer,
84            TerminalSelection,
85        },
86        element::Terminal,
87        handle::{
88            TerminalError,
89            TerminalHandle,
90            TerminalId,
91        },
92        parser::TerminalMouseButton,
93    };
94}