From c58b2be8dfac90b32442a0a202b666cc16a1b0a8 Mon Sep 17 00:00:00 2001 From: CosmicHorror Date: Tue, 23 Jan 2024 20:00:05 -0600 Subject: [PATCH] feat: Expose additional `two-face` themes (#219) --- inlyne.default.toml | 14 ++- src/color.rs | 98 ++++++++++++++----- ...terpreter__tests__code_block_bg_color.snap | 5 +- ...erpreter__tests__code_in_ordered_list.snap | 4 +- ...ter__tests__handles_comma_in_info_str.snap | 4 +- ...opts__tests__error_msg__unknown_theme.snap | 2 +- 6 files changed, 89 insertions(+), 38 deletions(-) diff --git a/inlyne.default.toml b/inlyne.default.toml index 957345c8..67ecab7a 100644 --- a/inlyne.default.toml +++ b/inlyne.default.toml @@ -39,9 +39,15 @@ select-color = 0x3675cb checkbox-color = 0x0a5301 # Syntax highlighting theme. All of `syntect`s default themes are supported # Possible values: [ -# "base16-ocean-dark", "base16-eighties-dark", "base16-mocha-dark", -# "base16-ocean-light", "inspired-github", "solarized-dark", -# "solarized-light" +# "base16-eighties-dark", "base16-mocha-dark", "base16-ocean-dark", +# "base16-ocean-light", "coldark-cold", "coldark-dark", +# "dark-neon", "dracula", "github", +# "gruvbox-dark", "gruvbox-light", "leet", +# "monokai-extended", "monokai-extended-light", +# "nord", "one-half-dark", "one-half-light", +# "solarized-dark", "solarized-light", "sublime-snazzy", +# "two-dark", "visual-studio-dark-plus", +# "zenburn" # ] # You can also pass a path to a `.tmTheme` file for a custom theme instead # Example: @@ -57,7 +63,7 @@ quote-block-color = 0xeef9fe link-color = 0x5466ff select-color = 0xcde8f0 checkbox-color = 0x96ecae -code-highlighter = "inspired-github" +code-highlighter = "github" # Specify the main and monospace font families [font-options] diff --git a/src/color.rs b/src/color.rs index 7ca22e34..cf46697c 100644 --- a/src/color.rs +++ b/src/color.rs @@ -8,6 +8,7 @@ use serde::Deserialize; use syntect::highlighting::{ Color as SyntectColor, Theme as SyntectTheme, ThemeSet as SyntectThemeSet, }; +use two_face::theme::EmbeddedThemeName; use wgpu::TextureFormat; fn hex_to_linear_rgba(c: u32) -> [f32; 4] { @@ -67,7 +68,7 @@ impl Theme { static CACHED_CODE_HIGHLIGHTER: OnceLock = OnceLock::new(); // Initializing this is non-trivial. Cache so it only runs once let code_highlighter = CACHED_CODE_HIGHLIGHTER - .get_or_init(|| ThemeDefaults::InspiredGithub.into()) + .get_or_init(|| ThemeDefaults::Github.into()) .to_owned(); Self { text_color: 0x000000, @@ -162,25 +163,57 @@ impl<'de> Deserialize<'de> for SyntaxTheme { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ThemeDefaults { - Base16OceanDark, Base16EightiesDark, Base16MochaDark, + Base16OceanDark, Base16OceanLight, - InspiredGithub, + ColdarkCold, + ColdarkDark, + DarkNeon, + Dracula, + Github, + GruvboxDark, + GruvboxLight, + Leet, + MonokaiExtended, + MonokaiExtendedLight, + Nord, + OneHalfDark, + OneHalfLight, SolarizedDark, SolarizedLight, + SublimeSnazzy, + TwoDark, + VisualStudioDarkPlus, + Zenburn, } impl ThemeDefaults { fn kebab_pairs() -> &'static [(&'static str, Self)] { &[ - ("base16-ocean-dark", Self::Base16OceanDark), ("base16-eighties-dark", Self::Base16EightiesDark), ("base16-mocha-dark", Self::Base16MochaDark), + ("base16-ocean-dark", Self::Base16OceanDark), ("base16-ocean-light", Self::Base16OceanLight), - ("inspired-github", Self::InspiredGithub), + ("coldark-cold", Self::ColdarkCold), + ("coldark-dark", Self::ColdarkDark), + ("dark-neon", Self::DarkNeon), + ("dracula", Self::Dracula), + ("github", Self::Github), + ("gruvbox-dark", Self::GruvboxDark), + ("gruvbox-light", Self::GruvboxLight), + ("leet", Self::Leet), + ("monokai-extended", Self::MonokaiExtended), + ("monokai-extended-light", Self::MonokaiExtendedLight), + ("nord", Self::Nord), + ("one-half-dark", Self::OneHalfDark), + ("one-half-light", Self::OneHalfLight), ("solarized-dark", Self::SolarizedDark), ("solarized-light", Self::SolarizedLight), + ("sublime-snazzy", Self::SublimeSnazzy), + ("two-dark", Self::TwoDark), + ("visual-studio-dark-plus", Self::VisualStudioDarkPlus), + ("zenburn", Self::Zenburn), ] } @@ -191,30 +224,49 @@ impl ThemeDefaults { } pub fn as_syntect_name(self) -> &'static str { - match self { - Self::Base16OceanDark => "base16-ocean.dark", - Self::Base16EightiesDark => "base16-eighties.dark", - Self::Base16MochaDark => "base16-mocha.dark", - Self::Base16OceanLight => "base16-ocean.light", - Self::InspiredGithub => "InspiredGitHub", - Self::SolarizedDark => "Solarized (dark)", - Self::SolarizedLight => "Solarized (light)", + EmbeddedThemeName::from(self).as_name() + } +} + +impl From for EmbeddedThemeName { + fn from(default: ThemeDefaults) -> Self { + match default { + ThemeDefaults::Base16EightiesDark => Self::Base16EightiesDark, + ThemeDefaults::Base16MochaDark => Self::Base16MochaDark, + ThemeDefaults::Base16OceanDark => Self::Base16OceanDark, + ThemeDefaults::Base16OceanLight => Self::Base16OceanLight, + ThemeDefaults::ColdarkCold => Self::ColdarkCold, + ThemeDefaults::ColdarkDark => Self::ColdarkDark, + ThemeDefaults::DarkNeon => Self::DarkNeon, + ThemeDefaults::Dracula => Self::Dracula, + ThemeDefaults::Github => Self::Github, + ThemeDefaults::GruvboxDark => Self::GruvboxDark, + ThemeDefaults::GruvboxLight => Self::GruvboxLight, + ThemeDefaults::Leet => Self::Leet, + ThemeDefaults::MonokaiExtended => Self::MonokaiExtended, + ThemeDefaults::MonokaiExtendedLight => Self::MonokaiExtendedLight, + ThemeDefaults::Nord => Self::Nord, + ThemeDefaults::OneHalfDark => Self::OneHalfDark, + ThemeDefaults::OneHalfLight => Self::OneHalfLight, + ThemeDefaults::SolarizedDark => Self::SolarizedDark, + ThemeDefaults::SolarizedLight => Self::SolarizedLight, + ThemeDefaults::SublimeSnazzy => Self::SubmlimeSnazzy, + ThemeDefaults::TwoDark => Self::TwoDark, + ThemeDefaults::VisualStudioDarkPlus => Self::VisualStudioDarkPlus, + ThemeDefaults::Zenburn => Self::Zenburn, } } } impl From for SyntectTheme { fn from(default: ThemeDefaults) -> Self { - let mut default_themes = SyntectThemeSet::load_defaults(); - let mut theme = default_themes - .themes - .remove(default.as_syntect_name()) - .expect("Included with defaults"); - - // InspiredGitHub's background color is 0xfff which is the same as the default light theme - // background. We match GitHub's light theme code blocks instead to distinguish code blocks - // from the background - if default == ThemeDefaults::InspiredGithub { + let default_themes = two_face::theme::extra(); + let mut theme = default_themes.get(default.into()).to_owned(); + + // Github's background color is 0xfff which is the same as the default light theme + // background. We match GitHub's website light theme code blocks instead to distinguish + // code blocks from the background + if default == ThemeDefaults::Github { theme.settings.background = Some(SyntectColor { r: 0xf6, g: 0xf8, diff --git a/src/interpreter/snapshots/inlyne__interpreter__tests__code_block_bg_color.snap b/src/interpreter/snapshots/inlyne__interpreter__tests__code_block_bg_color.snap index 249e59a6..d6b751a4 100644 --- a/src/interpreter/snapshots/inlyne__interpreter__tests__code_block_bg_color.snap +++ b/src/interpreter/snapshots/inlyne__interpreter__tests__code_block_bg_color.snap @@ -1,6 +1,6 @@ --- source: src/interpreter/tests.rs -description: " --- md\n\n```\nFenced code block with no language tag\n```\n\n```rust\n// Rust code\nfn main() {}\n```\n\n --- html\n\n
Fenced code block with no language tag\n
\n
// Rust code\nfn main() {}\n
\n" +description: " --- md\n\n```\nFenced code block with no language tag\n```\n\n```rust\n// Rust code\nfn main() {}\n```\n\n --- html\n\n
Fenced code block with no language tag\n
\n
// Rust code\nfn main() {}\n
\n" expression: interpret_md(text) --- [ @@ -36,7 +36,6 @@ expression: interpret_md(text) text: "// Rust code", font_family: Monospace, color: Some(Color { r: 0.30, g: 0.31, b: 0.30 }), - style: ITALIC , .. }, Text { @@ -48,14 +47,12 @@ expression: interpret_md(text) text: "fn ", font_family: Monospace, color: Some(Color { r: 0.39, g: 0.01, b: 0.11 }), - style: BOLD , .. }, Text { text: "main", font_family: Monospace, color: Some(Color { r: 0.19, g: 0.11, b: 0.37 }), - style: BOLD , .. }, Text { diff --git a/src/interpreter/snapshots/inlyne__interpreter__tests__code_in_ordered_list.snap b/src/interpreter/snapshots/inlyne__interpreter__tests__code_in_ordered_list.snap index 3a66e89c..6cf098b2 100644 --- a/src/interpreter/snapshots/inlyne__interpreter__tests__code_in_ordered_list.snap +++ b/src/interpreter/snapshots/inlyne__interpreter__tests__code_in_ordered_list.snap @@ -1,6 +1,6 @@ --- source: src/interpreter/tests.rs -description: " --- md\n\n1. 1st item\n\n ```rust\n fn main() {}\n ```\n\n2. 2nd item\n\n\n --- html\n\n
    \n
  1. \n

    1st item

    \n
    fn main() {}\n
    \n
  2. \n
  3. \n

    2nd item

    \n
  4. \n
\n" +description: " --- md\n\n1. 1st item\n\n ```rust\n fn main() {}\n ```\n\n2. 2nd item\n\n\n --- html\n\n
    \n
  1. \n

    1st item

    \n
    fn main() {}\n
    \n
  2. \n
  3. \n

    2nd item

    \n
  4. \n
\n" expression: interpret_md(text) --- [ @@ -36,14 +36,12 @@ expression: interpret_md(text) text: "fn ", font_family: Monospace, color: Some(Color { r: 0.39, g: 0.01, b: 0.11 }), - style: BOLD , .. }, Text { text: "main", font_family: Monospace, color: Some(Color { r: 0.19, g: 0.11, b: 0.37 }), - style: BOLD , .. }, Text { diff --git a/src/interpreter/snapshots/inlyne__interpreter__tests__handles_comma_in_info_str.snap b/src/interpreter/snapshots/inlyne__interpreter__tests__handles_comma_in_info_str.snap index 8662f026..152d1fbc 100644 --- a/src/interpreter/snapshots/inlyne__interpreter__tests__handles_comma_in_info_str.snap +++ b/src/interpreter/snapshots/inlyne__interpreter__tests__handles_comma_in_info_str.snap @@ -1,6 +1,6 @@ --- source: src/interpreter/tests.rs -description: " --- md\n\n```rust,ignore\nlet v = 1;\n```\n\n\n --- html\n\n
let v = 1;\n
\n" +description: " --- md\n\n```rust,ignore\nlet v = 1;\n```\n\n\n --- html\n\n
let v = 1;\n
\n" expression: interpret_md(text) --- [ @@ -13,7 +13,6 @@ expression: interpret_md(text) text: "let", font_family: Monospace, color: Some(Color { r: 0.39, g: 0.01, b: 0.11 }), - style: BOLD , .. }, Text { @@ -26,7 +25,6 @@ expression: interpret_md(text) text: "= ", font_family: Monospace, color: Some(Color { r: 0.39, g: 0.01, b: 0.11 }), - style: BOLD , .. }, Text { diff --git a/src/opts/tests/snapshots/inlyne__opts__tests__error_msg__unknown_theme.snap b/src/opts/tests/snapshots/inlyne__opts__tests__error_msg__unknown_theme.snap index 91886ab2..aa6fdd76 100644 --- a/src/opts/tests/snapshots/inlyne__opts__tests__error_msg__unknown_theme.snap +++ b/src/opts/tests/snapshots/inlyne__opts__tests__error_msg__unknown_theme.snap @@ -7,5 +7,5 @@ TOML parse error at line 1, column 32 | 1 | light-theme.code-highlighter = "doesnt-exist" | ^^^^^^^^^^^^^^ -"doesnt-exist" didn't match any of the expected variants: ["base16-ocean-dark", "base16-eighties-dark", "base16-mocha-dark", "base16-ocean-light", "inspired-github", "solarized-dark", "solarized-light"] +"doesnt-exist" didn't match any of the expected variants: ["base16-eighties-dark", "base16-mocha-dark", "base16-ocean-dark", "base16-ocean-light", "coldark-cold", "coldark-dark", "dark-neon", "dracula", "github", "gruvbox-dark", "gruvbox-light", "leet", "monokai-extended", "monokai-extended-light", "nord", "one-half-dark", "one-half-light", "solarized-dark", "solarized-light", "sublime-snazzy", "two-dark", "visual-studio-dark-plus", "zenburn"]