Skip to content

Commit

Permalink
feat: Expose additional two-face themes (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Jan 24, 2024
1 parent 40f590d commit c58b2be
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 38 deletions.
14 changes: 10 additions & 4 deletions inlyne.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down
98 changes: 75 additions & 23 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -67,7 +68,7 @@ impl Theme {
static CACHED_CODE_HIGHLIGHTER: OnceLock<SyntectTheme> = 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,
Expand Down Expand Up @@ -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),
]
}

Expand All @@ -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<ThemeDefaults> 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<ThemeDefaults> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<pre style=\"background-color:#f6f8fa;\"><code><span style=\"color:#323232;\">Fenced code block with no language tag\n</span></code></pre>\n<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust\"><span style=\"font-style:italic;color:#969896;\">// Rust code\n</span><span style=\"font-weight:bold;color:#a71d5d;\">fn </span><span style=\"font-weight:bold;color:#795da3;\">main</span><span style=\"color:#323232;\">() {}\n</span></code></pre>\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<pre style=\"background-color:#f6f8fa;\"><code><span style=\"color:#333333;\">Fenced code block with no language tag\n</span></code></pre>\n<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust\"><span style=\"color:#969896;\">// Rust code\n</span><span style=\"color:#a71d5d;\">fn </span><span style=\"color:#795da3;\">main</span><span style=\"color:#333333;\">() {}\n</span></code></pre>\n"
expression: interpret_md(text)
---
[
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ol>\n<li>\n<p>1st item</p>\n<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust\"><span style=\"font-weight:bold;color:#a71d5d;\">fn </span><span style=\"font-weight:bold;color:#795da3;\">main</span><span style=\"color:#323232;\">() {}\n</span></code></pre>\n</li>\n<li>\n<p>2nd item</p>\n</li>\n</ol>\n"
description: " --- md\n\n1. 1st item\n\n ```rust\n fn main() {}\n ```\n\n2. 2nd item\n\n\n --- html\n\n<ol>\n<li>\n<p>1st item</p>\n<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust\"><span style=\"color:#a71d5d;\">fn </span><span style=\"color:#795da3;\">main</span><span style=\"color:#333333;\">() {}\n</span></code></pre>\n</li>\n<li>\n<p>2nd item</p>\n</li>\n</ol>\n"
expression: interpret_md(text)
---
[
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust,ignore\"><span style=\"font-weight:bold;color:#a71d5d;\">let</span><span style=\"color:#323232;\"> v </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#0086b3;\">1</span><span style=\"color:#323232;\">;\n</span></code></pre>\n"
description: " --- md\n\n```rust,ignore\nlet v = 1;\n```\n\n\n --- html\n\n<pre style=\"background-color:#f6f8fa;\"><code class=\"language-rust,ignore\"><span style=\"color:#a71d5d;\">let</span><span style=\"color:#333333;\"> v </span><span style=\"color:#a71d5d;\">= </span><span style=\"color:#0086b3;\">1</span><span style=\"color:#333333;\">;\n</span></code></pre>\n"
expression: interpret_md(text)
---
[
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit c58b2be

Please sign in to comment.