Skip to main content

pulldown_cmark_mdcat/terminal/
capabilities.rs

1// Copyright 2018-2020 Sebastian Wiesner <sebastian@swsnr.de>
2
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Capabilities of terminal emulators.
8
9use crate::resources::InlineImageProtocol;
10
11pub mod iterm2;
12pub mod kitty;
13#[cfg(feature = "sixel")]
14pub mod sixel;
15
16/// The capability of basic styling.
17#[derive(Debug, Copy, Clone, Eq, PartialEq)]
18pub enum StyleCapability {
19    /// The terminal supports ANSI styles including OSC 8.
20    Ansi,
21}
22
23/// The capability of the terminal to set marks.
24#[derive(Debug, Copy, Clone)]
25pub enum MarkCapability {
26    /// The terminal supports iTerm2 jump marks.
27    ITerm2(iterm2::ITerm2Protocol),
28}
29
30/// The capability of the terminal to write images inline.
31#[derive(Debug, Copy, Clone)]
32pub enum ImageCapability {
33    /// The terminal understands the iterm2 image protocol.
34    ITerm2(iterm2::ITerm2Protocol),
35    /// The terminal understands the kitty graphics protocol.
36    Kitty(kitty::KittyGraphicsProtocol),
37    /// The terminal renders images in the sixel protocol.
38    #[cfg(feature = "sixel")]
39    Sixel(sixel::SixelProtocol),
40}
41
42impl ImageCapability {
43    pub(crate) fn image_protocol(&self) -> &dyn InlineImageProtocol {
44        match self {
45            ImageCapability::ITerm2(t) => t,
46            ImageCapability::Kitty(t) => t,
47            #[cfg(feature = "sixel")]
48            ImageCapability::Sixel(t) => t,
49        }
50    }
51}
52
53/// The capabilities of a terminal.
54///
55/// See [`crate::TerminalProgram`] for a way to detect a terminal and derive known capabilities.
56/// To obtain capabilities for the current terminal program use [`crate::TerminalProgram::detect`]
57/// to detect the terminal and then [`crate::TerminalProgram::capabilities`] to get its
58/// capabilities.
59#[derive(Debug)]
60pub struct TerminalCapabilities {
61    /// Whether the terminal supports basic ANSI styling.
62    pub style: Option<StyleCapability>,
63    /// How the terminal supports images.
64    pub image: Option<ImageCapability>,
65    /// How the terminal supports marks.
66    pub marks: Option<MarkCapability>,
67}
68
69impl Default for TerminalCapabilities {
70    /// A terminal which supports nothing.
71    fn default() -> Self {
72        TerminalCapabilities {
73            style: None,
74            image: None,
75            marks: None,
76        }
77    }
78}
79
80impl TerminalCapabilities {
81    pub(crate) fn with_image_capability(mut self, cap: ImageCapability) -> Self {
82        self.image = Some(cap);
83        self
84    }
85
86    pub(crate) fn with_mark_capability(mut self, cap: MarkCapability) -> Self {
87        self.marks = Some(cap);
88        self
89    }
90}