Skip to main content

rusty_bubbletea/
termcap.rs

1//! Cleanroom Rust port of upstream Go source file: `termcap.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Termcap / Terminfo Capabilities
6//!
7//! Terminal capability query requests (`request_capability`) and responses (`CapabilityMsg`).
8//! </public-docs>
9
10use crate::model::Cmd;
11use std::fmt;
12
13/// RequestCapabilityMsg is an internal message that requests the terminal to
14/// send its Termcap/Terminfo response.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct RequestCapabilityMsg(pub String);
17
18/// RequestCapability is a command that requests the terminal to send its
19/// Termcap/Terminfo response for the given capability.
20///
21/// Bubble Tea recognizes the following capabilities and will use them to
22/// upgrade the program's color profile:
23/// - `"RGB"` Xterm direct color
24/// - `"Tc"` True color support
25///
26/// Note: that some terminal's like Apple's Terminal.app do not support this and
27/// will send the wrong response to the terminal breaking the program's output.
28///
29/// When the Bubble Tea advertises a non-TrueColor profile, you can use this
30/// command to query the terminal for its color capabilities.
31pub fn request_capability(s: &str) -> Cmd {
32    let cap = s.to_string();
33    Some(Box::new(move || Some(Box::new(RequestCapabilityMsg(cap)))))
34}
35
36/// CapabilityMsg represents a Termcap/Terminfo response event. Termcap
37/// responses are generated by the terminal in response to `request_capability`
38/// (XTGETTCAP) requests.
39///
40/// See: <https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands>
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct CapabilityMsg {
43    /// Capability string content.
44    pub content: String,
45}
46
47impl fmt::Display for CapabilityMsg {
48    /// Returns the capability content as a string.
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(f, "{}", self.content)
51    }
52}