| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #![allow(dead_code)] |
| 29 | |
| 30 | use std::ffi::OsString; |
| 31 | use std::io::Write; |
| 32 | use std::path::{Path, PathBuf}; |
| 33 | |
| 34 | |
| 35 | const MAGIC: &str = "edith-keys 1"; |
| 36 | |
| 37 | |
| 38 | |
| 39 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 40 | pub enum ActionId { |
| 41 | Play, |
| 42 | Export, |
| 43 | Save, |
| 44 | Copy, |
| 45 | Paste, |
| 46 | Cut, |
| 47 | Regroup, |
| 48 | Delete, |
| 49 | Lift, |
| 50 | Undo, |
| 51 | ToggleMute, |
| 52 | VolumeUp, |
| 53 | VolumeDown, |
| 54 | CancelExport, |
| 55 | } |
| 56 | |
| 57 | impl ActionId { |
| 58 | |
| 59 | |
| 60 | pub const ALL: [ActionId; 14] = [ |
| 61 | ActionId::Play, |
| 62 | ActionId::Export, |
| 63 | ActionId::Save, |
| 64 | ActionId::Copy, |
| 65 | ActionId::Paste, |
| 66 | ActionId::Cut, |
| 67 | ActionId::Regroup, |
| 68 | ActionId::Delete, |
| 69 | ActionId::Lift, |
| 70 | ActionId::Undo, |
| 71 | ActionId::ToggleMute, |
| 72 | ActionId::VolumeUp, |
| 73 | ActionId::VolumeDown, |
| 74 | ActionId::CancelExport, |
| 75 | ]; |
| 76 | |
| 77 | |
| 78 | pub fn label(self) -> &'static str { |
| 79 | match self { |
| 80 | ActionId::Play => "Play / Pause", |
| 81 | ActionId::Export => "Export", |
| 82 | ActionId::Save => "Save", |
| 83 | ActionId::Copy => "Copy", |
| 84 | ActionId::Paste => "Paste", |
| 85 | ActionId::Cut => "Cut", |
| 86 | ActionId::Regroup => "Regroup", |
| 87 | ActionId::Delete => "Delete", |
| 88 | ActionId::Lift => "Lift (leave a gap)", |
| 89 | ActionId::Undo => "Undo", |
| 90 | ActionId::ToggleMute => "Mute / Unmute", |
| 91 | ActionId::VolumeUp => "Volume up", |
| 92 | ActionId::VolumeDown => "Volume down", |
| 93 | ActionId::CancelExport => "Cancel export", |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | fn name(self) -> &'static str { |
| 100 | match self { |
| 101 | ActionId::Play => "play", |
| 102 | ActionId::Export => "export", |
| 103 | ActionId::Save => "save", |
| 104 | ActionId::Copy => "copy", |
| 105 | ActionId::Paste => "paste", |
| 106 | ActionId::Cut => "cut", |
| 107 | ActionId::Regroup => "regroup", |
| 108 | ActionId::Delete => "delete", |
| 109 | ActionId::Lift => "lift", |
| 110 | ActionId::Undo => "undo", |
| 111 | ActionId::ToggleMute => "toggle-mute", |
| 112 | ActionId::VolumeUp => "volume-up", |
| 113 | ActionId::VolumeDown => "volume-down", |
| 114 | ActionId::CancelExport => "cancel-export", |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn from_name(name: &str) -> Option<Self> { |
| 119 | Self::ALL.into_iter().find(|a| a.name() == name) |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | pub fn category(self) -> Category { |
| 126 | match self { |
| 127 | ActionId::Play => Category::Playback, |
| 128 | ActionId::Copy | ActionId::Paste | ActionId::Delete | ActionId::Lift => Category::Clips, |
| 129 | ActionId::Cut | ActionId::Regroup | ActionId::Undo => Category::Editing, |
| 130 | ActionId::ToggleMute | ActionId::VolumeUp | ActionId::VolumeDown => Category::Audio, |
| 131 | ActionId::Save | ActionId::Export | ActionId::CancelExport => Category::File, |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 138 | pub enum Category { |
| 139 | Playback, |
| 140 | Editing, |
| 141 | Clips, |
| 142 | Audio, |
| 143 | File, |
| 144 | View, |
| 145 | } |
| 146 | |
| 147 | impl Category { |
| 148 | pub const ALL: [Category; 6] = [ |
| 149 | Category::Playback, |
| 150 | Category::Editing, |
| 151 | Category::Clips, |
| 152 | Category::Audio, |
| 153 | Category::File, |
| 154 | Category::View, |
| 155 | ]; |
| 156 | |
| 157 | pub fn label(self) -> &'static str { |
| 158 | match self { |
| 159 | Category::Playback => "Playback", |
| 160 | Category::Editing => "Editing", |
| 161 | Category::Clips => "Clips", |
| 162 | Category::Audio => "Audio", |
| 163 | Category::File => "File", |
| 164 | Category::View => "View", |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | pub struct Fixed { |
| 175 | |
| 176 | |
| 177 | pub chord: &'static str, |
| 178 | pub label: &'static str, |
| 179 | pub category: Category, |
| 180 | } |
| 181 | |
| 182 | pub const FIXED: [Fixed; 3] = [ |
| 183 | Fixed { |
| 184 | chord: "esc", |
| 185 | label: "Close this card or clip menu, or cancel a capture", |
| 186 | category: Category::View, |
| 187 | }, |
| 188 | Fixed { |
| 189 | chord: "0–9", |
| 190 | label: "Type a custom export bitrate", |
| 191 | category: Category::File, |
| 192 | }, |
| 193 | Fixed { |
| 194 | chord: "backspace", |
| 195 | label: "Erase a bitrate digit", |
| 196 | category: Category::File, |
| 197 | }, |
| 198 | ]; |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 205 | pub struct Chord { |
| 206 | pub key: String, |
| 207 | pub ctrl: bool, |
| 208 | } |
| 209 | |
| 210 | impl Chord { |
| 211 | |
| 212 | fn text(&self) -> String { |
| 213 | if self.ctrl { |
| 214 | format!("ctrl+{}", self.key) |
| 215 | } else { |
| 216 | self.key.clone() |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | pub fn pretty(&self) -> String { |
| 225 | let key = if self.key == "escape" { |
| 226 | "esc" |
| 227 | } else { |
| 228 | &self.key |
| 229 | }; |
| 230 | if self.ctrl { |
| 231 | format!("ctrl+{key}") |
| 232 | } else { |
| 233 | key.to_string() |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | fn parse(text: &str, n: usize) -> Result<Self, String> { |
| 242 | let (ctrl, key) = match text.strip_prefix("ctrl+") { |
| 243 | Some(key) => (true, key), |
| 244 | None => (false, text), |
| 245 | }; |
| 246 | if key.is_empty() |
| 247 | || key.contains(' ') |
| 248 | || key.contains('+') |
| 249 | || key.chars().any(|c| c.is_ascii_uppercase()) |
| 250 | { |
| 251 | return Err(format!("line {n}: {text:?} is not a chord")); |
| 252 | } |
| 253 | Ok(Chord { |
| 254 | key: key.to_string(), |
| 255 | ctrl, |
| 256 | }) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 262 | pub struct Binding { |
| 263 | pub action: ActionId, |
| 264 | pub chord: Chord, |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 271 | pub struct Keymap { |
| 272 | bindings: Vec<Binding>, |
| 273 | } |
| 274 | |
| 275 | impl Keymap { |
| 276 | |
| 277 | |
| 278 | |
| 279 | pub fn defaults() -> Self { |
| 280 | let b = |action, key: &str, ctrl| Binding { |
| 281 | action, |
| 282 | chord: Chord { |
| 283 | key: key.to_string(), |
| 284 | ctrl, |
| 285 | }, |
| 286 | }; |
| 287 | Keymap { |
| 288 | bindings: vec![ |
| 289 | b(ActionId::Play, "space", false), |
| 290 | b(ActionId::Export, "e", false), |
| 291 | b(ActionId::Save, "s", true), |
| 292 | b(ActionId::Copy, "c", true), |
| 293 | b(ActionId::Paste, "v", true), |
| 294 | b(ActionId::Cut, "c", false), |
| 295 | |
| 296 | |
| 297 | b(ActionId::Regroup, "g", false), |
| 298 | b(ActionId::Delete, "x", false), |
| 299 | b(ActionId::Delete, "delete", false), |
| 300 | b(ActionId::Lift, "l", false), |
| 301 | b(ActionId::Undo, "z", false), |
| 302 | b(ActionId::Undo, "z", true), |
| 303 | b(ActionId::ToggleMute, "m", false), |
| 304 | |
| 305 | |
| 306 | |
| 307 | b(ActionId::VolumeUp, "=", false), |
| 308 | b(ActionId::VolumeDown, "-", false), |
| 309 | b(ActionId::CancelExport, "escape", false), |
| 310 | ], |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | pub fn lookup(&self, key: &str, ctrl: bool) -> Option<ActionId> { |
| 317 | self.bindings |
| 318 | .iter() |
| 319 | .find(|b| b.chord.ctrl == ctrl && b.chord.key == key) |
| 320 | .map(|b| b.action) |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | pub fn display(&self, action: ActionId) -> String { |
| 326 | let strokes: Vec<String> = self |
| 327 | .bindings |
| 328 | .iter() |
| 329 | .filter(|b| b.action == action) |
| 330 | .map(|b| b.chord.pretty()) |
| 331 | .collect(); |
| 332 | if strokes.is_empty() { |
| 333 | "unbound".to_string() |
| 334 | } else { |
| 335 | strokes.join(" or ") |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
| 340 | pub fn entries(&self) -> &[Binding] { |
| 341 | &self.bindings |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | pub fn rebind_action(&mut self, action: ActionId, chord: Chord) -> Result<(), ActionId> { |
| 354 | if let Some(held) = self |
| 355 | .bindings |
| 356 | .iter() |
| 357 | .find(|b| b.action != action && b.chord == chord) |
| 358 | { |
| 359 | return Err(held.action); |
| 360 | } |
| 361 | self.bindings.retain(|b| b.action != action); |
| 362 | |
| 363 | |
| 364 | let at = self |
| 365 | .bindings |
| 366 | .iter() |
| 367 | .position(|b| rank(b.action) > rank(action)) |
| 368 | .unwrap_or(self.bindings.len()); |
| 369 | self.bindings.insert(at, Binding { action, chord }); |
| 370 | Ok(()) |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | pub fn load() -> (Self, Option<String>) { |
| 379 | Self::load_from(&Self::config_path()) |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | fn load_from(path: &Path) -> (Self, Option<String>) { |
| 385 | let text = match std::fs::read_to_string(path) { |
| 386 | Ok(text) => text, |
| 387 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return (Self::defaults(), None), |
| 388 | Err(e) => { |
| 389 | return ( |
| 390 | Self::defaults(), |
| 391 | Some(format!( |
| 392 | "KEYBINDINGS IGNORED — {}: {e} — the defaults are in force", |
| 393 | path.display() |
| 394 | )), |
| 395 | ); |
| 396 | } |
| 397 | }; |
| 398 | match parse(&text) { |
| 399 | Ok(keymap) => (keymap, None), |
| 400 | Err(e) => ( |
| 401 | Self::defaults(), |
| 402 | Some(format!( |
| 403 | "KEYBINDINGS IGNORED — {}, {e} — the defaults are in force", |
| 404 | path.display() |
| 405 | )), |
| 406 | ), |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | pub fn save(&self) -> std::io::Result<()> { |
| 414 | self.save_to(&Self::config_path()) |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | fn save_to(&self, path: &Path) -> std::io::Result<()> { |
| 420 | let dir = path.parent().unwrap_or(Path::new(".")).to_path_buf(); |
| 421 | std::fs::create_dir_all(&dir)?; |
| 422 | let mut part = path.to_path_buf().into_os_string(); |
| 423 | part.push(".part"); |
| 424 | let part = PathBuf::from(part); |
| 425 | let result = std::fs::File::create(&part) |
| 426 | .and_then(|mut f| { |
| 427 | f.write_all(emit(self).as_bytes())?; |
| 428 | f.sync_all() |
| 429 | }) |
| 430 | .and_then(|()| std::fs::rename(&part, path)) |
| 431 | .and_then(|()| std::fs::File::open(&dir)?.sync_all()); |
| 432 | if result.is_err() { |
| 433 | let _ = std::fs::remove_file(&part); |
| 434 | } |
| 435 | result |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | pub fn config_path() -> PathBuf { |
| 441 | config_path_in( |
| 442 | std::env::var_os("XDG_CONFIG_HOME"), |
| 443 | std::env::var_os("HOME"), |
| 444 | ) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | fn rank(action: ActionId) -> usize { |
| 451 | ActionId::ALL |
| 452 | .iter() |
| 453 | .position(|a| *a == action) |
| 454 | .unwrap_or(ActionId::ALL.len()) |
| 455 | } |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | fn config_path_in(xdg: Option<OsString>, home: Option<OsString>) -> PathBuf { |
| 462 | let dir = xdg |
| 463 | .filter(|v| !v.is_empty()) |
| 464 | .map(PathBuf::from) |
| 465 | .or_else(|| { |
| 466 | home.filter(|v| !v.is_empty()) |
| 467 | .map(|h| PathBuf::from(h).join(".config")) |
| 468 | }) |
| 469 | .unwrap_or_else(|| PathBuf::from(".config")); |
| 470 | dir.join("edith").join("keybindings") |
| 471 | } |
| 472 | |
| 473 | fn emit(keymap: &Keymap) -> String { |
| 474 | let mut out = String::from(MAGIC); |
| 475 | out.push('\n'); |
| 476 | for b in &keymap.bindings { |
| 477 | out.push_str(b.action.name()); |
| 478 | out.push(' '); |
| 479 | out.push_str(&b.chord.text()); |
| 480 | out.push('\n'); |
| 481 | } |
| 482 | out |
| 483 | } |
| 484 | |
| 485 | fn parse(text: &str) -> Result<Keymap, String> { |
| 486 | |
| 487 | |
| 488 | let body = text.strip_suffix('\n').unwrap_or(text); |
| 489 | let mut lines = body.split('\n').enumerate(); |
| 490 | let (_, first) = lines.next().unwrap_or((0, "")); |
| 491 | if first != MAGIC { |
| 492 | return Err(match first.strip_prefix("edith-keys ") { |
| 493 | Some(v) => format!("line 1: unsupported version {v}"), |
| 494 | None => "line 1: not a keybindings file".to_string(), |
| 495 | }); |
| 496 | } |
| 497 | |
| 498 | let mut bindings: Vec<Binding> = Vec::new(); |
| 499 | for (i, line) in lines { |
| 500 | let n = i + 1; |
| 501 | let Some((name, chord)) = line.split_once(' ') else { |
| 502 | return Err(format!("line {n}: {line:?} is not `<action> <chord>`")); |
| 503 | }; |
| 504 | let action = ActionId::from_name(name) |
| 505 | .ok_or_else(|| format!("line {n}: unknown action {name:?}"))?; |
| 506 | let chord = Chord::parse(chord, n)?; |
| 507 | |
| 508 | |
| 509 | if let Some(held) = bindings.iter().find(|b| b.chord == chord) { |
| 510 | return Err(format!( |
| 511 | "line {n}: {} is already {}'s", |
| 512 | chord.text(), |
| 513 | held.action.name() |
| 514 | )); |
| 515 | } |
| 516 | bindings.push(Binding { action, chord }); |
| 517 | } |
| 518 | Ok(Keymap { bindings }) |
| 519 | } |
| 520 | |
| 521 | #[cfg(test)] |
| 522 | mod tests { |
| 523 | use super::{ActionId, Chord, Keymap, config_path_in, emit, parse}; |
| 524 | |
| 525 | fn chord(key: &str, ctrl: bool) -> Chord { |
| 526 | Chord { |
| 527 | key: key.to_string(), |
| 528 | ctrl, |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn every_default_stroke_reaches_its_action() { |
| 534 | let k = Keymap::defaults(); |
| 535 | assert_eq!(k.entries().len(), 16); |
| 536 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); |
| 537 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); |
| 538 | assert_eq!(k.lookup("s", true), Some(ActionId::Save)); |
| 539 | assert_eq!(k.lookup("c", true), Some(ActionId::Copy)); |
| 540 | assert_eq!(k.lookup("v", true), Some(ActionId::Paste)); |
| 541 | assert_eq!(k.lookup("c", false), Some(ActionId::Cut)); |
| 542 | assert_eq!(k.lookup("g", false), Some(ActionId::Regroup)); |
| 543 | assert_eq!(k.lookup("x", false), Some(ActionId::Delete)); |
| 544 | assert_eq!(k.lookup("delete", false), Some(ActionId::Delete)); |
| 545 | assert_eq!(k.lookup("l", false), Some(ActionId::Lift)); |
| 546 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); |
| 547 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); |
| 548 | assert_eq!(k.lookup("m", false), Some(ActionId::ToggleMute)); |
| 549 | |
| 550 | |
| 551 | assert_eq!(k.lookup("=", false), Some(ActionId::VolumeUp)); |
| 552 | assert_eq!(k.lookup("-", false), Some(ActionId::VolumeDown)); |
| 553 | assert_eq!(k.lookup("+", false), None); |
| 554 | assert_eq!(k.lookup("escape", false), Some(ActionId::CancelExport)); |
| 555 | |
| 556 | assert_eq!(k.lookup("e", true), None); |
| 557 | assert_eq!(k.lookup("space", true), None); |
| 558 | assert_eq!(k.lookup("q", false), None); |
| 559 | |
| 560 | for (i, a) in k.entries().iter().enumerate() { |
| 561 | assert!( |
| 562 | !k.entries()[i + 1..].iter().any(|b| b.chord == a.chord), |
| 563 | "{:?} bound twice", |
| 564 | a.chord |
| 565 | ); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn a_file_survives_the_round_trip_exactly() { |
| 571 | let text = emit(&Keymap::defaults()); |
| 572 | assert!(text.starts_with("edith-keys 1\nplay space\n")); |
| 573 | assert!(text.contains("save ctrl+s\n")); |
| 574 | assert!(text.contains("cancel-export escape\n")); |
| 575 | |
| 576 | |
| 577 | assert_eq!(parse(&text), Ok(Keymap::defaults())); |
| 578 | assert_eq!(emit(&parse(&text).unwrap()), text); |
| 579 | let handwritten = "edith-keys 1\nundo ctrl+z\nplay space\n"; |
| 580 | assert_eq!(emit(&parse(handwritten).unwrap()), handwritten); |
| 581 | |
| 582 | |
| 583 | assert_eq!(parse("edith-keys 1\n").unwrap().entries(), &[]); |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn every_refusal_names_its_line() { |
| 588 | let err = |text: &str| parse(text).unwrap_err(); |
| 589 | assert_eq!(err(""), "line 1: not a keybindings file"); |
| 590 | assert_eq!(err("nonsense\n"), "line 1: not a keybindings file"); |
| 591 | assert_eq!(err("edith-keys 2\n"), "line 1: unsupported version 2"); |
| 592 | assert_eq!( |
| 593 | err("edith-keys 1\nfly space\n"), |
| 594 | "line 2: unknown action \"fly\"" |
| 595 | ); |
| 596 | assert_eq!( |
| 597 | err("edith-keys 1\nplay space\nplayspace\n"), |
| 598 | "line 3: \"playspace\" is not `<action> <chord>`" |
| 599 | ); |
| 600 | |
| 601 | assert_eq!( |
| 602 | err("edith-keys 1\n\n"), |
| 603 | "line 2: \"\" is not `<action> <chord>`" |
| 604 | ); |
| 605 | assert_eq!( |
| 606 | err("edith-keys 1\nplay ctrl+alt+x\n"), |
| 607 | "line 2: \"ctrl+alt+x\" is not a chord" |
| 608 | ); |
| 609 | assert_eq!( |
| 610 | err("edith-keys 1\nplay ctrl+\n"), |
| 611 | "line 2: \"ctrl+\" is not a chord" |
| 612 | ); |
| 613 | |
| 614 | |
| 615 | assert_eq!( |
| 616 | err("edith-keys 1\nplay X\n"), |
| 617 | "line 2: \"X\" is not a chord" |
| 618 | ); |
| 619 | |
| 620 | assert_eq!( |
| 621 | err("edith-keys 1\nplay space\ncut space\n"), |
| 622 | "line 3: space is already play's" |
| 623 | ); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn rebinding_refuses_to_steal_another_actions_stroke() { |
| 628 | let mut k = Keymap::defaults(); |
| 629 | assert_eq!( |
| 630 | k.rebind_action(ActionId::Export, chord("space", false)), |
| 631 | Err(ActionId::Play) |
| 632 | ); |
| 633 | |
| 634 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); |
| 635 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); |
| 636 | |
| 637 | assert_eq!(k.rebind_action(ActionId::Export, chord("w", true)), Ok(())); |
| 638 | assert_eq!(k.lookup("w", true), Some(ActionId::Export)); |
| 639 | assert_eq!(k.lookup("e", false), None); |
| 640 | assert_eq!(k.display(ActionId::Export), "ctrl+w"); |
| 641 | } |
| 642 | |
| 643 | #[test] |
| 644 | fn a_rebound_action_keeps_only_the_new_stroke() { |
| 645 | let mut k = Keymap::defaults(); |
| 646 | assert_eq!(k.display(ActionId::Delete), "x or delete"); |
| 647 | |
| 648 | assert_eq!(k.rebind_action(ActionId::Delete, chord("d", true)), Ok(())); |
| 649 | assert_eq!(k.display(ActionId::Delete), "ctrl+d"); |
| 650 | assert_eq!(k.lookup("x", false), None); |
| 651 | assert_eq!(k.lookup("delete", false), None); |
| 652 | assert_eq!(k.lookup("d", true), Some(ActionId::Delete)); |
| 653 | |
| 654 | |
| 655 | assert_eq!(k.rebind_action(ActionId::Undo, chord("z", true)), Ok(())); |
| 656 | assert_eq!(k.display(ActionId::Undo), "ctrl+z"); |
| 657 | assert_eq!(k.lookup("z", false), None); |
| 658 | |
| 659 | |
| 660 | let order: Vec<_> = k.entries().iter().map(|b| b.action).collect(); |
| 661 | assert_eq!(order, ActionId::ALL.to_vec()); |
| 662 | assert_eq!(emit(&k), emit(&parse(&emit(&k)).unwrap())); |
| 663 | |
| 664 | assert_eq!(k.entries().len(), ActionId::ALL.len()); |
| 665 | } |
| 666 | |
| 667 | #[test] |
| 668 | fn display_reads_as_a_person_would_say_it() { |
| 669 | let k = Keymap::defaults(); |
| 670 | assert_eq!(k.display(ActionId::Play), "space"); |
| 671 | assert_eq!(k.display(ActionId::Save), "ctrl+s"); |
| 672 | assert_eq!(k.display(ActionId::Delete), "x or delete"); |
| 673 | assert_eq!(k.display(ActionId::Undo), "z or ctrl+z"); |
| 674 | |
| 675 | assert_eq!(k.display(ActionId::CancelExport), "esc"); |
| 676 | assert_eq!( |
| 677 | parse("edith-keys 1\n").unwrap().display(ActionId::Cut), |
| 678 | "unbound" |
| 679 | ); |
| 680 | |
| 681 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); |
| 682 | assert_eq!(ActionId::ALL.len(), 14); |
| 683 | } |
| 684 | |
| 685 | #[test] |
| 686 | fn a_saved_keymap_comes_back_off_the_disk() { |
| 687 | |
| 688 | let dir = std::env::temp_dir().join(format!("edith-keymap-{}", std::process::id())); |
| 689 | let path = dir.join("edith").join("keybindings"); |
| 690 | let mut written = Keymap::defaults(); |
| 691 | written |
| 692 | .rebind_action(ActionId::Export, chord("w", true)) |
| 693 | .unwrap(); |
| 694 | |
| 695 | written.save_to(&path).unwrap(); |
| 696 | let (read, notice) = Keymap::load_from(&path); |
| 697 | assert_eq!(notice, None); |
| 698 | assert_eq!(read, written); |
| 699 | assert_eq!(read.lookup("w", true), Some(ActionId::Export)); |
| 700 | assert_eq!(read.lookup("e", false), None); |
| 701 | |
| 702 | assert!(!dir.join("edith").join("keybindings.part").exists()); |
| 703 | |
| 704 | let (fresh, notice) = Keymap::load_from(&dir.join("nothing-here")); |
| 705 | assert_eq!(fresh, Keymap::defaults()); |
| 706 | assert_eq!(notice, None); |
| 707 | |
| 708 | std::fs::write(&path, "edith-keys 1\nplay ctrl+alt+x\n").unwrap(); |
| 709 | let (fallback, notice) = Keymap::load_from(&path); |
| 710 | assert_eq!(fallback, Keymap::defaults()); |
| 711 | let notice = notice.expect("a refused file must say so"); |
| 712 | assert!(notice.contains("line 2:"), "{notice}"); |
| 713 | assert!(notice.contains("keybindings"), "{notice}"); |
| 714 | |
| 715 | Keymap::defaults().save_to(&path).unwrap(); |
| 716 | assert_eq!(Keymap::load_from(&path), (Keymap::defaults(), None)); |
| 717 | std::fs::remove_dir_all(&dir).ok(); |
| 718 | } |
| 719 | |
| 720 | #[test] |
| 721 | fn the_config_path_follows_xdg_then_home() { |
| 722 | let p = |xdg: Option<&str>, home: Option<&str>| { |
| 723 | config_path_in(xdg.map(Into::into), home.map(Into::into)) |
| 724 | }; |
| 725 | assert_eq!( |
| 726 | p(Some("/x/config"), Some("/home/u")), |
| 727 | std::path::Path::new("/x/config/edith/keybindings") |
| 728 | ); |
| 729 | assert_eq!( |
| 730 | p(None, Some("/home/u")), |
| 731 | std::path::Path::new("/home/u/.config/edith/keybindings") |
| 732 | ); |
| 733 | |
| 734 | assert_eq!( |
| 735 | p(Some(""), Some("/home/u")), |
| 736 | std::path::Path::new("/home/u/.config/edith/keybindings") |
| 737 | ); |
| 738 | |
| 739 | assert_eq!( |
| 740 | p(None, None), |
| 741 | std::path::Path::new(".config/edith/keybindings") |
| 742 | ); |
| 743 | } |
| 744 | } |