Skip to main content

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    /// Style for footnote references and definitions.
33    pub(crate) footnote_style: Style,
34    /// Style for math expressions.
35    pub(crate) math_style: Style,
36}
37
38impl Default for Theme {
39    /// The default theme from mdcat 1.x
40    fn default() -> Self {
41        Self {
42            html_block_style: Style::new().fg_color(Some(AnsiColor::Green.into())),
43            inline_html_style: Style::new().fg_color(Some(AnsiColor::Green.into())),
44            code_style: Style::new().fg_color(Some(AnsiColor::Yellow.into())),
45            link_style: Style::new().fg_color(Some(AnsiColor::Blue.into())),
46            image_link_style: Style::new().fg_color(Some(AnsiColor::Magenta.into())),
47            rule_color: AnsiColor::Green.into(),
48            code_block_border_color: AnsiColor::Green.into(),
49            heading_style: Style::new().fg_color(Some(AnsiColor::Blue.into())).bold(),
50            footnote_style: Style::new().fg_color(Some(AnsiColor::Cyan.into())),
51            math_style: Style::new().fg_color(Some(AnsiColor::Yellow.into())),
52        }
53    }
54}
55
56/// Combine styles.
57pub trait CombineStyle {
58    /// Put this style on top of the other style.
59    ///
60    /// Return a new style which falls back to the `other` style for all style attributes not
61    /// specified in this style.
62    fn on_top_of(self, other: &Self) -> Self;
63}
64
65impl CombineStyle for Style {
66    /// Put this style on top of the `other` style.
67    fn on_top_of(self, other: &Style) -> Style {
68        Style::new()
69            .fg_color(self.get_fg_color().or(other.get_fg_color()))
70            .bg_color(self.get_bg_color().or(other.get_bg_color()))
71            .effects(other.get_effects() | self.get_effects())
72            .underline_color(self.get_underline_color().or(other.get_underline_color()))
73    }
74}