merge custom config with default instead of replacing

This commit is contained in:
alice pellerin
2026-04-26 00:25:00 -05:00
parent adfb780521
commit fd63c6bd57
2 changed files with 37 additions and 6 deletions
+37 -2
View File
@@ -45,7 +45,7 @@ impl Config {
#[cfg(windows)] #[cfg(windows)]
fn default_path() -> Option<PathBuf> { fn default_path() -> Option<PathBuf> {
// this isn't technically the right way but it should be good enough // this isn't technically the right way but it should be good enough
home_dir().map(|home| home.join("AppData").join("Roaming")) home_dir().map(|home| home.join("AppData").join("Roaming").join("hexapoda.toml"))
} }
pub fn path(override_path: Option<PathBuf>) -> Option<PathBuf> { pub fn path(override_path: Option<PathBuf>) -> Option<PathBuf> {
@@ -58,7 +58,21 @@ impl Config {
let raw_config = read_to_string(path)?; let raw_config = read_to_string(path)?;
Ok(toml::from_str(&raw_config)?) Ok(Self::default().combined_with(toml::from_str(&raw_config)?))
}
fn combined_with(mut self, other: Self) -> Self {
for (mode, mode_config) in other.0 {
match self.0.entry(mode) {
Entry::Occupied(mut occupied_entry) => {
occupied_entry.get_mut().combine_with(mode_config);
}
Entry::Vacant(vacant_entry) => {
vacant_entry.insert(mode_config);
}
}
}
self
} }
} }
@@ -78,6 +92,27 @@ impl From<toml::de::Error> for ConfigInitError {
} }
} }
impl ModeConfig {
fn combine_with(&mut self, other: Self) {
for (partial_action, keybinds) in other.0 {
match self.0.entry(partial_action) {
Entry::Occupied(mut occupied_entry) => {
occupied_entry.get_mut().combine_with(keybinds);
}
Entry::Vacant(vacant_entry) => {
vacant_entry.insert(keybinds);
}
}
}
}
}
impl Keybinds {
fn combine_with(&mut self, other: Self) {
self.0.extend(other.0);
}
}
impl Serialize for ModeConfig { impl Serialize for ModeConfig {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?; let mut map = serializer.serialize_map(None)?;
-4
View File
@@ -73,10 +73,6 @@ const BYTES_OF_PADDING: usize = LINES_OF_PADDING * BYTES_PER_LINE;
// - [/] to cycle view offset? // - [/] to cycle view offset?
// - gj jump to entered offset // - gj jump to entered offset
// future directions
// - 'views' for bytes (i8/16/etc u8/16/etc 20.12/8.4/etc)
// - how to fit??! `-128` longer than `80`
fn main() { fn main() {
let arguments = Arguments::parse(); let arguments = Arguments::parse();