rusty-bubbletea 2.0.8

Cleanroom Rust port of Charmbracelet's Bubble Tea (v2.0.8) TUI Elm architecture framework
Documentation
//! Cleanroom Rust port of upstream Go source file: `termcap.go`
//! Upstream Target Tag / Version: `v2.0.8`
//!
//! <public-docs>
//! # Termcap / Terminfo Capabilities
//!
//! Terminal capability query requests (`request_capability`) and responses (`CapabilityMsg`).
//! </public-docs>

use crate::model::Cmd;
use std::fmt;

/// RequestCapabilityMsg is an internal message that requests the terminal to
/// send its Termcap/Terminfo response.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequestCapabilityMsg(pub String);

/// RequestCapability is a command that requests the terminal to send its
/// Termcap/Terminfo response for the given capability.
///
/// Bubble Tea recognizes the following capabilities and will use them to
/// upgrade the program's color profile:
/// - `"RGB"` Xterm direct color
/// - `"Tc"` True color support
///
/// Note: that some terminal's like Apple's Terminal.app do not support this and
/// will send the wrong response to the terminal breaking the program's output.
///
/// When the Bubble Tea advertises a non-TrueColor profile, you can use this
/// command to query the terminal for its color capabilities.
pub fn request_capability(s: &str) -> Cmd {
    let cap = s.to_string();
    Some(Box::new(move || Some(Box::new(RequestCapabilityMsg(cap)))))
}

/// CapabilityMsg represents a Termcap/Terminfo response event. Termcap
/// responses are generated by the terminal in response to `request_capability`
/// (XTGETTCAP) requests.
///
/// See: <https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands>
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapabilityMsg {
    /// Capability string content.
    pub content: String,
}

impl fmt::Display for CapabilityMsg {
    /// Returns the capability content as a string.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.content)
    }
}