Skip to main content

jolt_theme/
lib.rs

1mod builtin;
2pub mod cache;
3pub mod contrast;
4pub mod iterm2;
5mod loader;
6mod types;
7pub mod validation;
8
9pub use builtin::get_builtin_themes;
10pub use loader::{load_themes_from_dir, parse_theme_toml};
11pub use types::{Color, NamedTheme, ThemeColors, ThemeVariants};
12
13use std::path::Path;
14
15pub fn get_all_themes(user_themes_dir: Option<&Path>) -> Vec<NamedTheme> {
16    let mut themes = get_builtin_themes();
17    if let Some(dir) = user_themes_dir {
18        themes.extend(load_themes_from_dir(dir, false));
19    }
20    themes
21}
22
23pub fn get_theme_by_id(id: &str, user_themes_dir: Option<&Path>) -> Option<NamedTheme> {
24    get_all_themes(user_themes_dir)
25        .into_iter()
26        .find(|t| t.id == id)
27}
28
29pub fn color_to_hex(color: &Color) -> String {
30    color.to_hex()
31}
32
33pub fn generate_theme_toml(name: &str, base: &NamedTheme) -> String {
34    let mut content = format!("name = \"{}\"\n\n", name);
35
36    if let Some(ref dark) = base.variants.dark {
37        content.push_str("[dark]\n");
38        content.push_str(&colors_to_toml(dark, ""));
39        content.push_str("\n\n");
40    }
41
42    if let Some(ref light) = base.variants.light {
43        content.push_str("[light]\n");
44        content.push_str(&colors_to_toml(light, ""));
45        content.push('\n');
46    }
47
48    content
49}
50
51pub fn generate_blank_theme_toml(name: &str) -> String {
52    format!(
53        r##"name = "{}"
54
55[dark]
56bg = "#1e1e2e"
57dialog_bg = "#313244"
58fg = "#cdd6f4"
59accent = "#89b4fa"
60accent_secondary = "#cba6f7"
61highlight = "#f9e2af"
62muted = "#6c7086"
63success = "#a6e3a1"
64warning = "#fab387"
65danger = "#f38ba8"
66border = "#45475a"
67selection_bg = "#585b70"
68selection_fg = "#cdd6f4"
69graph_line = "#89b4fa"
70
71[light]
72bg = "#eff1f5"
73dialog_bg = "#e6e9ef"
74fg = "#4c4f69"
75accent = "#1e66f5"
76accent_secondary = "#8839ef"
77highlight = "#df8e1d"
78muted = "#6c6f85"
79success = "#40a02b"
80warning = "#fe640b"
81danger = "#d20f39"
82border = "#bcc0cc"
83selection_bg = "#acb0be"
84selection_fg = "#4c4f69"
85graph_line = "#1e66f5"
86"##,
87        name
88    )
89}
90
91fn colors_to_toml(colors: &ThemeColors, indent: &str) -> String {
92    format!(
93        r#"{i}bg = "{}"
94{i}dialog_bg = "{}"
95{i}fg = "{}"
96{i}accent = "{}"
97{i}accent_secondary = "{}"
98{i}highlight = "{}"
99{i}muted = "{}"
100{i}success = "{}"
101{i}warning = "{}"
102{i}danger = "{}"
103{i}border = "{}"
104{i}selection_bg = "{}"
105{i}selection_fg = "{}"
106{i}graph_line = "{}""#,
107        colors.bg.to_hex(),
108        colors.dialog_bg.to_hex(),
109        colors.fg.to_hex(),
110        colors.accent.to_hex(),
111        colors.accent_secondary.to_hex(),
112        colors.highlight.to_hex(),
113        colors.muted.to_hex(),
114        colors.success.to_hex(),
115        colors.warning.to_hex(),
116        colors.danger.to_hex(),
117        colors.border.to_hex(),
118        colors.selection_bg.to_hex(),
119        colors.selection_fg.to_hex(),
120        colors.graph_line.to_hex(),
121        i = indent,
122    )
123}