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