pulldown_cmark_mdcat/
theme.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//! Provide a colour theme for mdcat.
8
9use anstyle::{AnsiColor, Color, Style};
10
11/// A colour theme for mdcat.
12///
13/// Currently you cannot create custom styles, but only use the default theme via [`Theme::default`].
14#[derive(Debug, Clone)]
15pub struct Theme {
16    /// Style for HTML blocks.
17    pub(crate) html_block_style: Style,
18    /// Style for inline HTML.
19    pub(crate) inline_html_style: Style,
20    /// Style for code, unless the code is syntax-highlighted.
21    pub(crate) code_style: Style,
22    /// Style for links.
23    pub(crate) link_style: Style,
24    /// Color for image links (unless the image is rendered inline)
25    pub(crate) image_link_style: Style,
26    /// Color for rulers.
27    pub(crate) rule_color: Color,
28    /// Color for borders around code blocks.
29    pub(crate) code_block_border_color: Color,
30    /// Color for headings
31    pub(crate) heading_style: Style,
32}
33
34impl Default for Theme {
35    /// The default theme from mdcat 1.x
36    fn default() -> Self {
37        Self {
38            html_block_style: Style::new().fg_color(Some(AnsiColor::Green.into())),
39            inline_html_style: Style::new().fg_color(Some(AnsiColor::Green.into())),
40            code_style: Style::new().fg_color(Some(AnsiColor::Yellow.into())),
41            link_style: Style::new().fg_color(Some(AnsiColor::Blue.into())),
42            image_link_style: Style::new().fg_color(Some(AnsiColor::Magenta.into())),
43            rule_color: AnsiColor::Green.into(),
44            code_block_border_color: AnsiColor::Green.into(),
45            heading_style: Style::new().fg_color(Some(AnsiColor::Blue.into())).bold(),
46        }
47    }
48}
49
50/// Combine styles.
51pub trait CombineStyle {
52    /// Put this style on top of the other style.
53    ///
54    /// Return a new style which falls back to the `other` style for all style attributes not
55    /// specified in this style.
56    fn on_top_of(self, other: &Self) -> Self;
57}
58
59impl CombineStyle for Style {
60    /// Put this style on top of the `other` style.
61    fn on_top_of(self, other: &Style) -> Style {
62        Style::new()
63            .fg_color(self.get_fg_color().or(other.get_fg_color()))
64            .bg_color(self.get_bg_color().or(other.get_bg_color()))
65            .effects(other.get_effects() | self.get_effects())
66            .underline_color(self.get_underline_color().or(other.get_underline_color()))
67    }
68}