diff --git a/Cargo.lock b/Cargo.lock index 6911e683..263817f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3393,6 +3393,7 @@ dependencies = [ "regex", "serde", "toml", + "toml_edit", "tracing", "yazi-codegen", "yazi-fs", diff --git a/yazi-config/Cargo.toml b/yazi-config/Cargo.toml index 682b37d3..613cd068 100644 --- a/yazi-config/Cargo.toml +++ b/yazi-config/Cargo.toml @@ -26,6 +26,7 @@ ratatui = { workspace = true } regex = { workspace = true } serde = { workspace = true } toml = { workspace = true } +toml_edit = "0.22.26" tracing = { workspace = true } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index 124f3e9f..397b2ec1 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -2,7 +2,7 @@ # If you encounter any issues, please make an issue at https://github.com/yazi-rs/schemas. "$schema" = "https://yazi-rs.github.io/schemas/keymap.json" -[manager] +[mgr] keymap = [ { on = "", run = "escape", desc = "Exit visual mode, clear selection, or cancel search" }, diff --git a/yazi-config/preset/theme-dark.toml b/yazi-config/preset/theme-dark.toml index 33b03599..126f962b 100644 --- a/yazi-config/preset/theme-dark.toml +++ b/yazi-config/preset/theme-dark.toml @@ -18,7 +18,7 @@ light = "" # : Manager {{{ -[manager] +[mgr] cwd = { fg = "cyan" } # Hovered diff --git a/yazi-config/preset/theme-light.toml b/yazi-config/preset/theme-light.toml index 6509173a..eae8bb9a 100644 --- a/yazi-config/preset/theme-light.toml +++ b/yazi-config/preset/theme-light.toml @@ -18,7 +18,7 @@ light = "" # : Manager {{{ -[manager] +[mgr] cwd = { fg = "cyan" } # Hovered diff --git a/yazi-config/preset/yazi-default.toml b/yazi-config/preset/yazi-default.toml index 024ea0d0..9a7e24c2 100644 --- a/yazi-config/preset/yazi-default.toml +++ b/yazi-config/preset/yazi-default.toml @@ -2,7 +2,7 @@ # If you encounter any issues, please make an issue at https://github.com/yazi-rs/schemas. "$schema" = "https://yazi-rs.github.io/schemas/yazi.json" -[manager] +[mgr] ratio = [ 1, 4, 3 ] sort_by = "alphabetical" sort_sensitive = false diff --git a/yazi-config/src/keymap/keymap.rs b/yazi-config/src/keymap/keymap.rs index ce34689f..8158c634 100644 --- a/yazi-config/src/keymap/keymap.rs +++ b/yazi-config/src/keymap/keymap.rs @@ -7,7 +7,6 @@ use super::{Chord, KeymapRules}; #[derive(Deserialize, DeserializeOver1)] pub struct Keymap { - #[serde(rename = "manager")] pub mgr: KeymapRules, pub tasks: KeymapRules, pub spot: KeymapRules, diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index f5ccbdb7..3f3b915b 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -28,12 +28,8 @@ fn try_init(merge: bool) -> anyhow::Result<()> { if merge { let dir = yazi_fs::Xdg::config_dir(); - yazi = yazi.deserialize_over(toml::Deserializer::new( - &std::fs::read_to_string(dir.join("yazi.toml")).unwrap_or_default(), - ))?; - keymap = keymap.deserialize_over(toml::Deserializer::new( - &std::fs::read_to_string(dir.join("keymap.toml")).unwrap_or_default(), - ))?; + yazi = yazi.deserialize_over(toml::Deserializer::new(&migrate(dir.join("yazi.toml"))))?; + keymap = keymap.deserialize_over(toml::Deserializer::new(&migrate(dir.join("keymap.toml"))))?; } YAZI.init(yazi.reshape()?); @@ -53,9 +49,9 @@ fn try_init_flavor(light: bool, merge: bool) -> anyhow::Result<()> { let mut theme = Preset::theme(light)?; if merge { - let shadow = theme::Theme::deserialize_shadow(toml::Deserializer::new( - &std::fs::read_to_string(yazi_fs::Xdg::config_dir().join("theme.toml")).unwrap_or_default(), - ))?; + let shadow = theme::Theme::deserialize_shadow(toml::Deserializer::new(&migrate( + yazi_fs::Xdg::config_dir().join("theme.toml"), + )))?; let flavor = shadow.flavor.as_ref().map(theme::Flavor::from).unwrap_or_default().read(light)?; theme = theme.deserialize_over(toml::Deserializer::new(&flavor))?; @@ -87,3 +83,45 @@ fn wait_for_key(e: anyhow::Error) -> anyhow::Result<()> { TTY.reader().read_exact(&mut [0])?; Ok(()) } + +// TODO: remove this in the future +fn migrate(p: std::path::PathBuf) -> String { + let Ok(old) = std::fs::read_to_string(&p) else { + return String::new(); + }; + let Ok(mut doc) = old.parse::() else { + return old; + }; + if doc.get("mgr").is_some() { + return old; + } + let Some(manager) = doc.remove("manager") else { + return old; + }; + + doc.insert("mgr", manager); + let new = doc.to_string(); + + let mut backup = p.clone(); + backup.set_file_name(format!( + "{}-{}", + p.file_name().unwrap().to_str().unwrap(), + yazi_shared::timestamp_us() + )); + + if let Err(e) = std::fs::copy(&p, backup) { + _ = TTY.writer().write_all( + format!("WARNING: `[manager]` has been deprecated in favor of the new `[mgr]`, see #2803 for more details: https://github.com/sxyazi/yazi/pull/2803\n +Trying to migrate your config automatically failed, please edit the file manually, error while backuping {p:?}: \n{e:?}\n").as_bytes(), + ); + return new; + } + + if let Err(e) = std::fs::write(&p, &new) { + _ = TTY.writer().write_all( + format!("WARNING: `[manager]` has been deprecated in favor of the new `[mgr]`, see #2803 for more details: https://github.com/sxyazi/yazi/pull/2803\n +Trying to migrate your config automatically failed, please edit the file manually, error while writing {p:?}: \n{e:?}\n").as_bytes(), + ); + } + new +} diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index 5c5fa419..a32427e6 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -11,8 +11,7 @@ use super::{Filetype, Flavor, Icon}; #[derive(Deserialize, DeserializeOver1, Serialize)] pub struct Theme { pub flavor: Flavor, - #[serde(rename = "manager")] - pub mgr: Mgr, // TODO: Remove `serde(rename)` + pub mgr: Mgr, pub tabs: Tabs, pub mode: Mode, pub status: Status, diff --git a/yazi-config/src/yazi.rs b/yazi-config/src/yazi.rs index 9c889371..931f18d7 100644 --- a/yazi-config/src/yazi.rs +++ b/yazi-config/src/yazi.rs @@ -6,8 +6,7 @@ use crate::{mgr, open, opener, plugin, popup, preview, tasks, which}; #[derive(Deserialize, DeserializeOver1)] pub struct Yazi { - #[serde(rename = "manager")] - pub mgr: mgr::Mgr, // TODO: remove serde(rename) + pub mgr: mgr::Mgr, pub preview: preview::Preview, pub opener: opener::Opener, pub open: open::Open,