Skip to content

Commit

Permalink
chore(keybindings): Ensure we can always deserialize default keybindi…
Browse files Browse the repository at this point in the history
…ngs (#270)
  • Loading branch information
CosmicHorrorDev authored Mar 13, 2024
1 parent 16e2818 commit 632d843
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/keybindings/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{action::Action, KeyCombo};

/// A list of [`keybindings`](KeyCombo) each associated with an [`Action`].
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Keybindings(Vec<(Action, KeyCombo)>);
pub struct Keybindings(pub(crate) Vec<(Action, KeyCombo)>);

impl Keybindings {
/// Returns an iterator over the [`Action`]s and [`KeyCombo`]s
Expand Down
52 changes: 51 additions & 1 deletion src/keybindings/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::action::{Action, VertDirection};
use super::{KeyCombos, ModifiedKey};
use super::{KeyCombos, Keybindings, ModifiedKey};
use crate::opts::Config;
use crate::test_utils::init_test_log;

Expand Down Expand Up @@ -44,3 +44,53 @@ base = [
assert_eq!(key_combos.munch(key), maybe_action);
}
}

// TODO(cosmic): Move this to reading from the `inlyne.default.toml` file after a bit of cleanup to
// make things less verbose
// TODO(cosmic): Consider switching the casing away from PascalCase? Maybe keep it inline with the
// rest of the config file and use kebab-case instead?
// TODO(cosmic): Allow for passing a single string for `mod`
const DEFAULTS_TEMPLATE: &str = r#"
[keybindings]
base = [
# Regular
["Copy", { key = "c", mod = ["CTRL_OR_CMD"] }],
["ZoomIn", { key = "=", mod = ["CTRL_OR_CMD"] }],
["ZoomOut", { key = "-", mod = ["CTRL_OR_CMD"] }],
["HistoryNext", { key = "Right", mod = ["Alt"] }],
["HistoryPrevious", { key = "Left", mod = ["Alt"] }],
["ScrollUp", "Up"],
["ScrollDown", "Down"],
["PageUp", "PageUp"],
["PageDown", "PageDown"],
["ToTop", "Home"],
["ToBottom", "End"],
["Quit", "Escape"],
# Vim-like
["Copy", "y"],
["ScrollUp", "k"],
["ScrollDown", "j"],
["ToTop", ["g", "g"]],
["ToBottom", { key = "g", mod = ["Shift"] }],
["Quit", "q"],
["Quit", [{ key = "z", mod = ["Shift"] }, { key = "z", mod = ["Shift"] }]],
["Quit", [{ key = "z", mod = ["Shift"] }, { key = "q", mod = ["Shift"] }]],
["HistoryNext", ["b", "n"]],
["HistoryPrevious", ["b", "p"]],
]
"#;

#[test]
fn defaults() {
let ctrl_or_cmd = if cfg!(target_os = "macos") {
"Os"
} else {
"Ctrl"
};
let defaults = DEFAULTS_TEMPLATE.replace("CTRL_OR_CMD", ctrl_or_cmd);
let Config { keybindings, .. } = Config::load_from_str(&defaults).unwrap();
let config_defaults: Keybindings = keybindings.into();

let internal_defaults = Keybindings(super::defaults::defaults());
assert_eq!(config_defaults, internal_defaults);
}

0 comments on commit 632d843

Please sign in to comment.