use super::{Binding, Keymap};
use crate::{actions::Chord, Id};
use std::collections::HashMap;
const HEADER: &str = "# zaxis keymap 1";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeymapIssue {
pub line: usize,
pub message: String,
}
impl Keymap {
pub fn save(&self) -> String {
let mut text = format!("{HEADER}\n");
for (id, name) in &self.names {
let Some(bindings) = self.overrides.get(id) else {
continue;
};
if bindings.is_empty() {
text.push_str(&format!("{name} =\n"));
continue;
}
let mut scopes: Vec<Option<&str>> = Vec::new();
for binding in bindings {
if !scopes.contains(&binding.context.as_deref()) {
scopes.push(binding.context.as_deref());
}
}
for scope in scopes {
let chords: Vec<String> = bindings
.iter()
.filter(|binding| binding.context.as_deref() == scope)
.map(|binding| binding.chord.to_string())
.collect();
match scope {
Some(scope) => {
text.push_str(&format!("{name}@{scope} = {}\n", chords.join(", ")))
}
None => text.push_str(&format!("{name} = {}\n", chords.join(", "))),
}
}
}
text
}
pub fn load(&mut self, text: &str) -> Vec<KeymapIssue> {
let by_name: HashMap<&str, Id> = self
.names
.iter()
.map(|(id, name)| (name.as_str(), *id))
.collect();
let mut issues = Vec::new();
let mut loaded: Vec<(Id, Vec<Binding>)> = Vec::new();
for (index, line) in text.lines().enumerate() {
let line = line.split('#').next().unwrap_or("").trim();
if line.is_empty() {
continue;
}
let mut issue = |message: String| {
issues.push(KeymapIssue {
line: index + 1,
message,
})
};
let Some((left, right)) = line.split_once('=') else {
issue("expected `action = chord`".into());
continue;
};
let (name, context) = match left.trim().split_once('@') {
Some((name, context)) => (name.trim(), Some(context.trim().to_owned())),
None => (left.trim(), None),
};
let Some(&id) = by_name.get(name) else {
issue(format!("unknown action `{name}`"));
continue;
};
let slot = match loaded.iter().position(|(known, _)| *known == id) {
Some(slot) => slot,
None => {
loaded.push((id, Vec::new()));
loaded.len() - 1
}
};
for text in right
.split(',')
.map(str::trim)
.filter(|text| !text.is_empty())
{
match text.parse::<Chord>() {
Ok(chord) => loaded[slot].1.push(Binding {
action: id,
context: context.clone(),
chord,
}),
Err(error) => issue(format!("`{text}`: {error}")),
}
}
}
self.overrides = loaded.into_iter().collect();
self.rebuild();
issues
}
}