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 | 2589 | "unicode-width", |
| 2590 | 2590 | "url", |
| 2591 | 2591 | "vergen-gitcl", |
| 2592 | "xdg", |
| 2592 | 2593 | ] |
| 2593 | 2594 | |
| 2594 | 2595 | [[package]] |
| @@ -7814,6 +7815,12 @@ dependencies = [ |
| 7814 | 7815 | "zeroize", |
| 7815 | 7816 | ] |
| 7816 | 7817 | |
| 7818 | [[package]] |
| 7819 | name = "xdg" |
| 7820 | version = "3.0.0" |
| 7821 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 7822 | checksum = "2fb433233f2df9344722454bc7e96465c9d03bff9d77c248f9e7523fe79585b5" |
| 7823 | |
| 7817 | 7824 | [[package]] |
| 7818 | 7825 | name = "xml5ever" |
| 7819 | 7826 | version = "0.39.0" |
Cargo.toml
+5
| @@ -71,6 +71,7 @@ unicode-segmentation = "1.7" |
| 71 | 71 | unicode-width = "0.2" |
| 72 | 72 | url = {version = "2.5.8", features = ["serde"]} |
| 73 | 73 | percent-encoding = "2.3.2" |
| 74 | xdg = "3.0.0" |
| 74 | 75 | |
| 75 | 76 | [dependencies.comrak] |
| 76 | 77 | version = "0.55.0" |
| @@ -135,6 +136,8 @@ assets = [ |
| 135 | 136 | # Manual pages: |
| 136 | 137 | ["docs/iamb.1", "usr/share/man/man1/iamb.1", "644"], |
| 137 | 138 | ["docs/iamb.5", "usr/share/man/man5/iamb.5", "644"], |
| 139 | # Packaged themes: |
| 140 | [ "themes/*.toml", "usr/share/iamb/themes/", "644" ], |
| 138 | 141 | # Other assets: |
| 139 | 142 | ["iamb.desktop", "usr/share/applications/iamb.desktop", "644"], |
| 140 | 143 | ["config.example.toml", "usr/share/iamb/config.example.toml", "644"], |
| @@ -151,6 +154,8 @@ assets = [ |
| 151 | 154 | # Manual pages: |
| 152 | 155 | { source = "docs/iamb.1", dest = "/usr/share/man/man1/iamb.1", mode = "644" }, |
| 153 | 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 | 159 | # Other assets: |
| 155 | 160 | { source = "iamb.desktop", dest = "/usr/share/applications/iamb.desktop", mode = "644" }, |
| 156 | 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 | 225 | ) -> anyhow::Result<CompletionMap<String, theme::Theme>> { |
| 226 | 226 | let mut themes = CompletionMap::default(); |
| 227 | 227 | |
| 228 | | // Load from the `themes` directory in the user's configuration directory: |
| 229 | | theme::find_themes(themes_dir)?.into_iter().for_each(|(n, t)| { |
| 228 | let mut insert = |n, t| { |
| 230 | 229 | let theme = theme_overrides.clone().merge(t).merge(theme::default_theme()); |
| 231 | 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 | 249 | // Insert a default theme that defaults to using terminal colors: |
| 235 | 250 | let default_theme = theme_overrides.clone().merge(theme::default_theme()); |
src/config/theme.rs
+30
| @@ -1120,4 +1120,34 @@ mod tests { |
| 1120 | 1120 | let res: LineName = serde_json::from_str(r#""double-dashed-heavy""#).unwrap(); |
| 1121 | 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 | } |