libghostty_vt/lib.rs
1//! Idiomatic, safe Rust bindings for
2//! [`libghostty-vt`](https://libghostty.tip.ghostty.org/),
3//! a terminal emulation library extracted from the
4//! [Ghostty](https://ghostty.org) terminal emulator.
5//!
6//! `libghostty-vt` contains the logic for handling the core parts of a
7//! terminal emulator: parsing terminal escape sequences, maintaining terminal
8//! state, encoding input events, etc. It can handle scrollback, line wrapping,
9//! reflow on resize, and more.
10//!
11//! <div class="warning">
12//!
13//! This library is currently in development and the API is not yet stable.
14//! Breaking changes are expected in future versions.
15//! Use with caution in production code.
16//!
17//! </div>
18//!
19//! The core type of `libghostty-vt` is the [`Terminal`] — start there to get a
20//! better sense of how everything is structured. You can also check out
21//! a list of all top-level modules and their functions below.
22//!
23//! # Examples
24//!
25//! [`ghostling-rs`](https://github.com/Uzaaft/libghostty-rs/blob/master/example/ghostling_rs/src/main.rs)
26//! is a minimal yet functional terminal emulator built with `libghostty-vt`,
27//! written as a single file with only around 1000 lines of heavily commented,
28//! easily digestible code. It is based on the original [Ghostling](https://github.com/ghostty-org/ghostling)
29//! which is built on the same underlying C API.
30//!
31//! Other examples are currently work-in-progress — if you have any ideas for
32//! examples, feel free to share them with us as an issue or pull request.
33//!
34//! # Memory management and lifetimes
35//!
36//! When creating the terminal and various other objects, you can control their
37//! memory management via a **custom allocator**, usually specified with
38//! methods like [`Terminal::new_with_alloc`]. Objects that accept allocators
39//! are also bound by the `'alloc` lifetime, since they internally contain
40//! a reference to the allocator. If you do not use a custom allocator,
41//! feel free to always set the lifetime to `'static`.
42//!
43//! ## Using the unstable `Allocator` API
44//!
45//! You can adapt the existing, unstable `Allocator` API into a
46//! [libghostty-friendly allocator](alloc::Allocator) via its `From`
47//! implementation. Note that the `'alloc` lifetime must at least
48//! live as long as the `Allocator` instance itself.
49//!
50//! # Thread safety
51//!
52//! The entire `libghostty-vt` library is **not** thread-safe unless otherwise
53//! noted. This is because the underlying C API is not designed with thread
54//! safety in mind, and we as binding authors must rather conservatively
55//! avoid making any assumptions beyond what is presently guaranteed by the
56//! C API.
57//!
58//! In particular, all `libghostty-vt` types are `!Send`, meaning they cannot
59//! be *transferred* across threads, since the C API is allowed to use
60//! thread-local state; they are also `!Sync`, meaning they cannot be *shared*
61//! across threads, since data races may occur as the C API is not guarded with
62//! mutexes or other synchronization mechanisms.
63//! We currently do not expect to lift these limitations unless the C API starts
64//! to make stronger guarantees regarding thread safety.
65//!
66//! Note that this does *not* mean that `libghostty-vt` can only be used on
67//! the main thread. On the contrary, in a complex program we encourage you to
68//! create the terminal on a separate thread (or task in async programming),
69//! and use [channels](std::sync::mpsc::channel) to communicate between the
70//! terminal emulation thread/task and the main program. Under sufficient load, it is
71//! generally more efficient to offload terminal emulation to its own operating
72//! system-level thread, in order to reduce competition with other business logic.
73#![doc(
74 html_logo_url = "https://raw.githubusercontent.com/ghostty-org/ghostty/2d0fb81751def478e2f8a5f7e2ee91fa9cbf9bff/images/icons/icon_128@2x.png"
75)]
76#![warn(clippy::pedantic)]
77#![warn(missing_docs)]
78#![warn(missing_debug_implementations)]
79#![warn(missing_copy_implementations)]
80#![warn(clippy::allow_attributes)]
81#![warn(clippy::allow_attributes_without_reason)]
82#![allow(
83 clippy::missing_errors_doc,
84 reason = "underlying C API may return any error outside of expected and
85 mitigated situations, and it is not feasible to document them all"
86)]
87#![cfg_attr(docsrs, feature(doc_cfg))]
88
89pub use libghostty_vt_sys as ffi;
90
91// Make sure that `Terminal`'s own impl blocks (i.e. core functions)
92// are placed *before* any extra impl blocks from other modules,
93// e.g. Kitty Graphics extensions, Selection APIs
94pub mod terminal;
95
96pub mod alloc;
97pub mod build_info;
98pub mod error;
99pub mod fmt;
100pub mod focus;
101pub mod key;
102pub mod kitty;
103pub mod log;
104pub mod mouse;
105pub mod osc;
106pub mod paste;
107pub mod render;
108pub mod screen;
109pub mod selection;
110pub mod sgr;
111pub mod style;
112
113#[doc(inline)]
114pub use crate::{
115 error::Error,
116 log::{Logger, set_logger},
117 render::RenderState,
118 terminal::{Options as TerminalOptions, Terminal},
119};
120
121pub(crate) fn sys_set<T>(opt: ffi::SysOption::Type, val: *const T) -> error::Result<()> {
122 let result = unsafe { ffi::ghostty_sys_set(opt, val.cast()) };
123 error::from_result(result)
124}