Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

A theme file saved as UTF-16 is read, and the log says what a theme loaded (997)

gelenmedya782334581f53
diff --git a/crates/rf-app/src/themes.rs b/crates/rf-app/src/themes.rs
index 68d8afc4..cbccc341 100644
--- a/crates/rf-app/src/themes.rs
+++ b/crates/rf-app/src/themes.rs
@@ -80,11 +80,18 @@ fn current() -> Option<Arc<Theme>> {
/// Reads a theme folder. Colours that cannot be read are left out and logged.
pub fn load(folder: &Path, name: &str) -> Theme {
let mut t = Theme { name: name.to_string(), ..Default::default() };
- if let Ok(text) = std::fs::read_to_string(folder.join("theme.toml")) {
- match parse_colors(&text) {
- Ok((dark, light)) => (t.dark, t.light) = (dark, light),
- Err(e) => log::warn!("theme {name}: theme.toml: {e}"),
- }
+ let file = folder.join("theme.toml");
+ match std::fs::read(&file) {
+ Ok(bytes) => match toml_text(&bytes) {
+ Some(text) => match parse_colors(&text) {
+ Ok((dark, light)) => (t.dark, t.light) = (dark, light),
+ Err(e) => log::warn!("theme {name}: theme.toml: {e}"),
+ },
+ None => log::warn!("theme {name}: theme.toml is not UTF-8 or UTF-16 text; its colours are not used"),
+ },
+ // (a theme of icons only has no theme.toml)
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
+ Err(e) => log::warn!("theme {name}: theme.toml: {e}"),
}
for e in std::fs::read_dir(folder.join("icons")).into_iter().flatten().flatten() {
let p = e.path();
@@ -98,9 +105,23 @@ pub fn load(folder: &Path, name: &str) -> Theme {
}
}
}
+ // what was found, in the log, so that a theme that "does nothing" says why (issue 997: "Nothing is written to rustyfiles.log")
+ log::info!(target: "rustyfiles::start", "theme {name}: {} dark and {} light colours, {} icons", t.dark.len(), t.light.len(), t.icons.len());
t
}
+/// The text of a theme file: UTF-8, or UTF-16 with its byte order mark, which Windows PowerShell's `>` writes. A byte order mark
+/// is dropped. Issue 997: a theme.toml written that way was read as no file at all, without a word in the log. Pure.
+fn toml_text(bytes: &[u8]) -> Option<String> {
+ let units = |be: bool| bytes[2..].chunks_exact(2).map(|c| if be { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }).collect::<Vec<u16>>();
+ match bytes {
+ [0xFF, 0xFE, ..] => String::from_utf16(&units(false)).ok(),
+ [0xFE, 0xFF, ..] => String::from_utf16(&units(true)).ok(),
+ [0xEF, 0xBB, 0xBF, rest @ ..] => String::from_utf8(rest.to_vec()).ok(),
+ _ => String::from_utf8(bytes.to_vec()).ok(),
+ }
+}
+
fn rank(ext: &str) -> usize {
ICON_EXTS.iter().position(|e| *e == ext).map_or(usize::MAX, |i| [1, 0, 2][i])
}
@@ -111,7 +132,13 @@ pub fn parse_colors(text: &str) -> Result<(Vec<(String, Hsla)>, Vec<(String, Hsl
let part = |key: &str| -> Vec<(String, Hsla)> {
let Some(toml::Value::Table(t)) = table.get(key) else { return Vec::new() };
t.iter()
- .filter(|(k, _)| COLOR_KEYS.contains(&k.as_str()))
+ .filter(|(k, _)| {
+ let known = COLOR_KEYS.contains(&k.as_str());
+ if !known {
+ log::warn!("theme: {key}.{k} is no colour key of the palette (docs/themes.md lists them)");
+ }
+ known
+ })
.filter_map(|(k, v)| match v.as_str().and_then(parse_hex) {
Some(c) => Some((k.clone(), c)),
None => {
@@ -196,6 +223,27 @@ fn pick_icon(icons: &HashMap<String, PathBuf>, is_dir: bool, ext: &str, own_pict
mod tests {
use super::*;
+ /// Issue 997: a theme.toml saved as UTF-16 (Windows PowerShell's `>`) or with a UTF-8 byte order mark is read like a plain one.
+ #[test]
+ fn a_theme_file_in_utf16_or_with_a_byte_order_mark_is_read() {
+ let text = "[dark]
+accent = \"#4CC3FF\"
+";
+ let mut le = vec![0xFF, 0xFE];
+ le.extend(text.encode_utf16().flat_map(u16::to_le_bytes));
+ let mut be = vec![0xFE, 0xFF];
+ be.extend(text.encode_utf16().flat_map(u16::to_be_bytes));
+ let mut bom = vec![0xEF, 0xBB, 0xBF];
+ bom.extend(text.as_bytes());
+ for bytes in [le, be, bom, text.as_bytes().to_vec()] {
+ let read = toml_text(&bytes).expect("read");
+ assert_eq!(read, text);
+ let (dark, _) = parse_colors(&read).unwrap();
+ assert_eq!(dark.len(), 1);
+ }
+ assert!(toml_text(&[0xC3, 0x28]).is_none(), "bytes that are no text are refused, not read as an empty file");
+ }
+
#[test]
fn colours_are_read_by_the_palettes_names() {
let (dark, light) = parse_colors("[dark]\naccent = \"#88C0D0\"\nwindow_bg = \"#2E3440CC\"\nnot_a_colour = \"#FFFFFF\"\ntext = \"blue\"\n[light]\naccent = \"#5E81AC\"\n").unwrap();