1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2018-2020 Sebastian Wiesner <sebastian@swsnr.de>

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Terminal utilities.

// Support modules for terminal writing.

mod ansi;
pub mod highlighting;
mod size;

mod iterm2;
mod kitty;
mod osc;
mod terminology;

pub use self::ansi::AnsiStyle;
pub use self::size::TerminalSize;

/// The capability of basic styling.
#[derive(Debug, Copy, Clone)]
pub enum StyleCapability {
    /// The terminal supports ANSI styles.
    Ansi(AnsiStyle),
}

/// How the terminal supports inline links.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum LinkCapability {
    /// The terminal supports [OSC 8] inline links.
    ///
    /// [OSC 8]: https://git.io/vd4ee
    Osc8(self::osc::Osc8Links),
}

/// The capability of the terminal to set marks.
#[derive(Debug, Copy, Clone)]
pub enum MarkCapability {
    /// The terminal supports iTerm2 jump marks.
    ITerm2(self::iterm2::ITerm2Marks),
}

/// The capability of the terminal to write images inline.
#[derive(Debug, Copy, Clone)]
pub enum ImageCapability {
    /// The terminal understands the terminology way of inline images.
    Terminology(self::terminology::TerminologyImages),
    /// The terminal understands the iterm2 way of inline images.
    ITerm2(self::iterm2::ITerm2Images),
    /// The terminal understands the Kitty way of inline images.
    Kitty(self::kitty::KittyImages),
}

/// The capabilities of a terminal.
#[derive(Debug)]
pub struct TerminalCapabilities {
    /// How do we call this terminal?
    pub name: String,
    /// How the terminal supports basic styling.
    pub style: Option<StyleCapability>,
    /// How the terminal supports links.
    pub links: Option<LinkCapability>,
    /// How the terminal supports images.
    pub image: Option<ImageCapability>,
    /// How the terminal supports marks.
    pub marks: Option<MarkCapability>,
}

/// Get the version of the underlying VTE terminal if any.
fn get_vte_version() -> Option<(u8, u8)> {
    std::env::var("VTE_VERSION").ok().and_then(|value| {
        value[..2]
            .parse::<u8>()
            .into_iter()
            .zip(value[2..4].parse::<u8>())
            .next()
    })
}

/// Checks if the current terminal is WezTerm.
fn is_wezterm() -> bool {
    std::env::var("TERM_PROGRAM").map_or(false, |value| value == "WezTerm")
        || std::env::var("TERM").map_or(false, |value| value == "wezterm")
}

/// Checks if the current terminal is foot.
fn is_foot() -> bool {
    std::env::var("TERM").map_or(false, |value| value == "foot")
}

impl TerminalCapabilities {
    /// A terminal which supports nothing.
    pub fn none() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "dumb".to_string(),
            style: None,
            links: None,
            image: None,
            marks: None,
        }
    }

    /// A terminal with basic ANSI formatting only.
    pub fn ansi() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "Ansi".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: None,
            image: None,
            marks: None,
        }
    }

    /// Terminal capabilities of iTerm2.
    pub fn iterm2() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "iTerm2".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: Some(ImageCapability::ITerm2(self::iterm2::ITerm2Images)),
            marks: Some(MarkCapability::ITerm2(self::iterm2::ITerm2Marks)),
        }
    }

    /// Terminal capabilities of Terminology.
    pub fn terminology() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "Terminology".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: Some(ImageCapability::Terminology(
                self::terminology::TerminologyImages,
            )),
            marks: None,
        }
    }

    /// Terminal capabilities of Kitty.
    pub fn kitty() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "Kitty".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: Some(ImageCapability::Kitty(self::kitty::KittyImages)),
            marks: None,
        }
    }

    /// Terminal capabilities of VET 0.50 or newer.
    pub fn vte50() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "VTE 50".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: None,
            marks: None,
        }
    }

    /// Terminal capabilities of WezTerm (Wez's Terminal Emulator).
    ///
    /// WezTerm is a GPU-accelerated cross-platform
    /// terminal emulator and multiplexer written by @wez
    /// and implemented in Rust.
    ///
    /// See <https://wezfurlong.org/wezterm/> for more details.
    pub fn wezterm() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "WezTerm".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: Some(ImageCapability::ITerm2(self::iterm2::ITerm2Images)),
            marks: None,
        }
    }

    /// Terminal capabilities of foot
    /// Foot is a fast, lightweight and minimalistic Wayland terminal emulator
    pub fn foot() -> TerminalCapabilities {
        TerminalCapabilities {
            name: "foot".to_string(),
            style: Some(StyleCapability::Ansi(AnsiStyle)),
            links: Some(LinkCapability::Osc8(self::osc::Osc8Links)),
            image: None,
            marks: None,
        }
    }

    /// Detect the capabilities of the current terminal.
    pub fn detect() -> TerminalCapabilities {
        if self::iterm2::is_iterm2() {
            Self::iterm2()
        } else if self::terminology::is_terminology() {
            Self::terminology()
        } else if self::kitty::is_kitty() {
            Self::kitty()
        } else if is_wezterm() {
            Self::wezterm()
        } else if is_foot() {
            Self::foot()
        } else if get_vte_version().filter(|&v| v >= (50, 0)).is_some() {
            Self::vte50()
        } else {
            Self::ansi()
        }
    }
}