Skip to main content

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;
15pub mod terminology;
16
17/// The capability of basic styling.
18#[derive(Debug, Copy, Clone, Eq, PartialEq)]
19pub enum StyleCapability {
20    /// The terminal supports ANSI styles including OSC 8.
21    Ansi,
22}
23
24/// The capability of the terminal to set marks.
25#[derive(Debug, Copy, Clone)]
26pub enum MarkCapability {
27    /// The terminal supports iTerm2 jump marks.
28    ITerm2(iterm2::ITerm2Protocol),
29}
30
31/// The capability of the terminal to write images inline.
32#[derive(Debug, Copy, Clone)]
33pub enum ImageCapability {
34    /// The terminal understands the terminology image protocol.
35    Terminology(terminology::Terminology),
36    /// The terminal understands the iterm2 image protocol.
37    ITerm2(iterm2::ITerm2Protocol),
38    /// The terminal understands the kitty graphics protocol.
39    Kitty(kitty::KittyGraphicsProtocol),
40    /// The terminal understands the Sixel graphics protocol.
41    #[cfg(feature = "sixel")]
42    Sixel(sixel::SixelProtocol),
43}
44
45impl ImageCapability {
46    pub(crate) fn image_protocol(&self) -> &dyn InlineImageProtocol {
47        match self {
48            ImageCapability::Terminology(t) => t,
49            ImageCapability::ITerm2(t) => t,
50            ImageCapability::Kitty(t) => t,
51            #[cfg(feature = "sixel")]
52            ImageCapability::Sixel(t) => t,
53        }
54    }
55}
56
57/// The capabilities of a terminal.
58///
59/// See [`crate::TerminalProgram`] for a way to detect a terminal and derive known capabilities.
60/// To obtain capabilities for the current terminal program use [`crate::TerminalProgram::detect`]
61/// to detect the terminal and then [`crate::TerminalProgram::capabilities`] to get its
62/// capabilities.
63#[derive(Debug)]
64pub struct TerminalCapabilities {
65    /// Whether the terminal supports basic ANSI styling.
66    pub style: Option<StyleCapability>,
67    /// How the terminal supports images.
68    pub image: Option<ImageCapability>,
69    /// How the terminal supports marks.
70    pub marks: Option<MarkCapability>,
71}
72
73impl Default for TerminalCapabilities {
74    /// A terminal which supports nothing.
75    fn default() -> Self {
76        TerminalCapabilities {
77            style: None,
78            image: None,
79            marks: None,
80        }
81    }
82}
83
84impl TerminalCapabilities {
85    pub(crate) fn with_image_capability(mut self, cap: ImageCapability) -> Self {
86        self.image = Some(cap);
87        self
88    }
89
90    pub(crate) fn with_mark_capability(mut self, cap: MarkCapability) -> Self {
91        self.marks = Some(cap);
92        self
93    }
94}