use super::theme::Theme;
use serde::{Deserialize, Serialize};
use std::{fs, path::Path};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Look {
pub theme: Theme,
pub icons: bool,
pub keep_going: bool,
pub snapshots: bool,
pub set_up: bool,
}
impl Default for Look {
fn default() -> Self {
Self {
theme: Theme::Solid,
icons: true,
keep_going: false,
snapshots: true,
set_up: false,
}
}
}
pub use helix_toolbox_engine::settings::{favorites_path, load_favorites, path, save_favorites};
pub fn load(path: &Path) -> Look {
let Some(table) = fs::read_to_string(path)
.ok()
.and_then(|text| text.parse::<toml::Table>().ok())
else {
return Look::default();
};
let mut look = Look {
set_up: true,
..Look::default()
};
let get = |name: &str| table.get(name).cloned();
if let Some(theme) = get("theme").and_then(|value| value.try_into().ok()) {
look.theme = theme;
}
for (name, field) in [
("icons", &mut look.icons),
("keep_going", &mut look.keep_going),
("snapshots", &mut look.snapshots),
("set_up", &mut look.set_up),
] {
if let Some(value) = get(name).and_then(|value| value.as_bool()) {
*field = value;
}
}
look
}
pub fn save(path: &Path, look: &Look) -> std::io::Result<()> {
let ours = toml::Table::try_from(look).map_err(std::io::Error::other)?;
helix_toolbox_engine::settings::update(path, |table| table.extend(ours))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_look_round_trips_and_falls_back_to_the_default() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("helix-toolbox/settings.toml");
assert_eq!(load(&path), Look::default());
assert_eq!(Look::default().theme, Theme::Solid);
let look = Look {
theme: Theme::Transparent,
icons: false,
keep_going: true,
snapshots: false,
set_up: false,
};
save(&path, &look).unwrap();
assert_eq!(load(&path), look);
assert!(
fs::read_to_string(&path)
.unwrap()
.contains("theme = \"transparent\"")
);
fs::write(&path, "theme = \"default\"\n").unwrap();
assert_eq!(load(&path).theme, Theme::Transparent);
fs::write(&path, "theme = 3\n").unwrap();
assert_eq!(
load(&path),
Look {
set_up: true,
..Look::default()
}
);
assert!(
!Look::default().set_up,
"with no settings at all, it is the first run"
);
fs::write(
&path,
"theme = \"aurora\"\nicons = false\nanimations = true\n[future]\nx = 1\n",
)
.unwrap();
assert_eq!(
load(&path),
Look {
icons: false,
set_up: true,
..Look::default()
}
);
save(&path, &Look::default()).unwrap();
let text = fs::read_to_string(&path).unwrap();
assert!(text.contains("animations = true"), "{text}");
assert!(text.contains("[future]"), "{text}");
assert!(text.contains("theme = \"solid\""), "{text}");
}
}