prefs.reset resets a single colorSettings key (#445) (#467)

`prefs.get` and `prefs.set` route `colorSettings.*` to the colour state,
but `prefs.reset` only special-cased the whole section. A nested path
fell through to `Preferences::reset`, which has no colorSettings, so it
failed with "unknown preference". Take the default from
`ColorSettings::default()` and set it the way `prefs.set` does.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
HEOJUNFO
2026-10-07 10:34:30 +07:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 778e8e187d
commit 533566c5ad
2 changed files with 20 additions and 0 deletions
+8
View File
@@ -1351,6 +1351,14 @@ fn prefs_reset(s: &mut Session, p: &Value) -> Result<Value> {
let path = p.get("path").or_else(|| p.get("section")).and_then(Value::as_str);
match path {
Some("colorSettings") => s.color.settings = Default::default(),
// One colour setting: `Preferences` has no `colorSettings`, so take the default
// from `ColorSettings` and route it like `prefs.set` does.
Some(path) if path.starts_with("colorSettings.") => {
let defaults = serde_json::to_value(crate::color_cmds::ColorSettings::default()).map_err(|e| bad("prefs.reset", e.to_string()))?;
let key = path.strip_prefix("colorSettings.").unwrap_or(path);
let def = get_path(&defaults, key).cloned().ok_or_else(|| bad("prefs.reset", format!("unknown preference `{path}`")))?;
s.set_pref(path, def).map_err(|e| bad("prefs.reset", e))?;
}
None => {
s.color.settings = Default::default();
s.edit_prefs(|p| p.reset(None)).map_err(|e| bad("prefs.reset", e))?;
+12
View File
@@ -45,6 +45,18 @@ fn get_set_reset_by_path() {
assert_eq!(s.active().unwrap().history.max_states, 50);
}
#[test]
fn reset_one_colour_setting() {
let mut s = session();
s.execute("prefs.set", json!({"values": {"colorSettings.bpc": false, "colorSettings.workingRgb": "display-p3"}})).unwrap();
assert_eq!(s.execute("prefs.reset", json!({"path": "colorSettings.bpc"})).unwrap(), json!(true));
assert!(s.color.settings.bpc);
assert_eq!(s.color.settings.working_rgb, "display-p3", "other colour settings are untouched");
s.execute("prefs.reset", json!({"path": "colorSettings.workingRgb"})).unwrap();
assert_eq!(s.color.settings, crate::color_cmds::ColorSettings::default());
assert!(s.execute("prefs.reset", json!({"path": "colorSettings.notAField"})).is_err());
}
#[test]
fn set_validates_and_is_all_or_nothing() {
let mut s = session();