Commit 97dafa4629

97dafa46291ea814a3fe94352d5ac07eaa4198b6

parent: a2ea6a09af

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-10-12 19:31 UTC
committer: <noreply@github.com>

Support loading themes from `$XDG_DATA_DIRS` (#810)

Layout: unified · split

Cargo.lock +7
@@ -2589,6 +2589,7 @@ dependencies = [
2589 "unicode-width", 2589 "unicode-width",
2590 "url", 2590 "url",
2591 "vergen-gitcl", 2591 "vergen-gitcl",
2592 "xdg",
2592] 2593]
2593 2594
2594[[package]] 2595[[package]]
@@ -7814,6 +7815,12 @@ dependencies = [
7814 "zeroize", 7815 "zeroize",
7815] 7816]
7816 7817
7818[[package]]
7819name = "xdg"
7820version = "3.0.0"
7821source = "registry+https://github.com/rust-lang/crates.io-index"
7822checksum = "2fb433233f2df9344722454bc7e96465c9d03bff9d77c248f9e7523fe79585b5"
7823
7817[[package]] 7824[[package]]
7818name = "xml5ever" 7825name = "xml5ever"
7819version = "0.39.0" 7826version = "0.39.0"
Cargo.toml +5
@@ -71,6 +71,7 @@ unicode-segmentation = "1.7"
71unicode-width = "0.2" 71unicode-width = "0.2"
72url = {version = "2.5.8", features = ["serde"]} 72url = {version = "2.5.8", features = ["serde"]}
73percent-encoding = "2.3.2" 73percent-encoding = "2.3.2"
74xdg = "3.0.0"
74 75
75[dependencies.comrak] 76[dependencies.comrak]
76version = "0.55.0" 77version = "0.55.0"
@@ -135,6 +136,8 @@ assets = [
135 # Manual pages: 136 # Manual pages:
136 ["docs/iamb.1", "usr/share/man/man1/iamb.1", "644"], 137 ["docs/iamb.1", "usr/share/man/man1/iamb.1", "644"],
137 ["docs/iamb.5", "usr/share/man/man5/iamb.5", "644"], 138 ["docs/iamb.5", "usr/share/man/man5/iamb.5", "644"],
139 # Packaged themes:
140 [ "themes/*.toml", "usr/share/iamb/themes/", "644" ],
138 # Other assets: 141 # Other assets:
139 ["iamb.desktop", "usr/share/applications/iamb.desktop", "644"], 142 ["iamb.desktop", "usr/share/applications/iamb.desktop", "644"],
140 ["config.example.toml", "usr/share/iamb/config.example.toml", "644"], 143 ["config.example.toml", "usr/share/iamb/config.example.toml", "644"],
@@ -151,6 +154,8 @@ assets = [
151 # Manual pages: 154 # Manual pages:
152 { source = "docs/iamb.1", dest = "/usr/share/man/man1/iamb.1", mode = "644" }, 155 { source = "docs/iamb.1", dest = "/usr/share/man/man1/iamb.1", mode = "644" },
153 { source = "docs/iamb.5", dest = "/usr/share/man/man5/iamb.5", mode = "644" }, 156 { source = "docs/iamb.5", dest = "/usr/share/man/man5/iamb.5", mode = "644" },
157 # Packaged themes:
158 { source = "themes/*.toml", dest = "/usr/share/iamb/themes/", mode = "644" },
154 # Other assets: 159 # Other assets:
155 { source = "iamb.desktop", dest = "/usr/share/applications/iamb.desktop", mode = "644" }, 160 { source = "iamb.desktop", dest = "/usr/share/applications/iamb.desktop", mode = "644" },
156 { source = "config.example.toml", dest = "/usr/share/iamb/config.example.toml", mode = "644"}, 161 { source = "config.example.toml", dest = "/usr/share/iamb/config.example.toml", mode = "644"},
src/config/mod.rs +18 −3
@@ -225,11 +225,26 @@ fn load_themes(
225) -> anyhow::Result<CompletionMap<String, theme::Theme>> { 225) -> anyhow::Result<CompletionMap<String, theme::Theme>> {
226 let mut themes = CompletionMap::default(); 226 let mut themes = CompletionMap::default();
227 227
228 // Load from the `themes` directory in the user's configuration directory: 228 let mut insert = |n, t| {
229 theme::find_themes(themes_dir)?.into_iter().for_each(|(n, t)| {
230 let theme = theme_overrides.clone().merge(t).merge(theme::default_theme()); 229 let theme = theme_overrides.clone().merge(t).merge(theme::default_theme());
231 themes.insert(n, theme); 230 themes.insert(n, theme);
232 }); 231 };
232
233 let mut load_dir = |d: &Path| -> anyhow::Result<()> {
234 theme::find_themes(d)?.into_iter().for_each(|(n, t)| insert(n, t));
235 Ok(())
236 };
237
238 // Load themes from the shared directory first for systems with XDG_DATA_DIRS:
239 #[cfg(any(unix, target_os = "redox"))]
240 xdg::BaseDirectories::new()
241 .get_data_dirs()
242 .into_iter()
243 .map(|p| p.join("iamb/themes"))
244 .try_for_each(|p| load_dir(&p))?;
245
246 // Load from the `themes` directory in the user's configuration directory:
247 load_dir(themes_dir)?;
233 248
234 // Insert a default theme that defaults to using terminal colors: 249 // Insert a default theme that defaults to using terminal colors:
235 let default_theme = theme_overrides.clone().merge(theme::default_theme()); 250 let default_theme = theme_overrides.clone().merge(theme::default_theme());
src/config/theme.rs +30
@@ -1120,4 +1120,34 @@ mod tests {
1120 let res: LineName = serde_json::from_str(r#""double-dashed-heavy""#).unwrap(); 1120 let res: LineName = serde_json::from_str(r#""double-dashed-heavy""#).unwrap();
1121 assert_eq!(res, LineName::DoubleDashedHeavy); 1121 assert_eq!(res, LineName::DoubleDashedHeavy);
1122 } 1122 }
1123
1124 #[test]
1125 fn test_load_packaged_themes() {
1126 let dir = Path::new("themes");
1127 let mut count = 0;
1128
1129 for entry in std::fs::read_dir(dir).expect("can read themes") {
1130 let path = entry.expect("can read theme file").path();
1131
1132 if path.extension().is_none_or(|ext| ext != "toml") {
1133 continue;
1134 }
1135
1136 let file = ThemeFile::load(&path)
1137 .with_context(|| format!("failed to parse {}", path.display()))
1138 .expect("parsed theme");
1139 assert!(file.theme.default.background.is_some());
1140 assert!(file.theme.default.color.is_some());
1141
1142 count += 1;
1143 }
1144
1145 // We don't hardcode the theme count so that adding new themes doesn't
1146 // require updating this test every time, but we do check that we have
1147 // seen enough that if something major changes (like renaming every
1148 // file to no longer end in `.toml`) then we will notice and fix this
1149 // test to account for it and continue asserting that the tests remain
1150 // valid.
1151 assert!(count > 40, "we should process >40 themes");
1152 }
1123} 1153}