Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/rgitui_workspace/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,9 +1379,15 @@ mod tests {
command.id
);
}
// Fetch is bound `secondary-shift-r`, which is Command on macOS.
let primary = if cfg!(target_os = "macos") {
"Cmd+"
} else {
"Ctrl+"
};
assert_eq!(
summary.display(CommandId::Fetch).as_deref(),
Some("Ctrl+Shift+R")
Some(format!("{primary}Shift+R").as_str())
);
}

Expand Down
39 changes: 26 additions & 13 deletions crates/rgitui_workspace/src/keymap/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ fn render(modifiers: &Modifiers, key: &str, style: KeystrokeStyle) -> String {
out.push_str("Alt+");
}
if modifiers.platform {
out.push_str(if cfg!(target_os = "windows") {
out.push_str(if cfg!(target_os = "macos") {
"Cmd+"
} else if cfg!(target_os = "windows") {
"Win+"
} else {
"Super+"
Expand Down Expand Up @@ -282,6 +284,8 @@ fn named_key(key: &str, style: KeystrokeStyle) -> Option<&'static str> {
"platform" => {
if symbols {
"⌘"
} else if cfg!(target_os = "macos") {
"Cmd"
} else if cfg!(target_os = "windows") {
"Win"
} else {
Expand Down Expand Up @@ -312,11 +316,20 @@ fn title_case(value: &str) -> String {
mod tests {
use super::*;

/// Word-style spelling of what `secondary-` resolves to here: Command on
/// macOS, Control everywhere else. Tests that hard-coded `Ctrl+` passed on
/// Windows and Linux while asserting the wrong thing on macOS.
const PRIMARY_WORD: &str = if cfg!(target_os = "macos") {
"Cmd+"
} else {
"Ctrl+"
};

#[test]
fn the_primary_modifier_follows_the_platform() {
assert_eq!(
humanize_keystroke("secondary-shift-r", KeystrokeStyle::Words).as_deref(),
Some("Ctrl+Shift+R")
Some(format!("{PRIMARY_WORD}Shift+R").as_str())
);
assert_eq!(
humanize_keystroke("secondary-shift-r", KeystrokeStyle::Symbols).as_deref(),
Expand All @@ -340,20 +353,20 @@ mod tests {
#[test]
fn named_keys_get_conventional_names() {
for (source, words) in [
("secondary-enter", "Ctrl+Enter"),
("escape", "Esc"),
("f5", "F5"),
("shift-tab", "Shift+Tab"),
("secondary-up", "Ctrl+Up"),
("alt-5", "Alt+5"),
("space", "Space"),
("delete", "Delete"),
("secondary-,", "Ctrl+,"),
("secondary-[", "Ctrl+["),
("secondary-enter", format!("{PRIMARY_WORD}Enter")),
("escape", "Esc".to_owned()),
("f5", "F5".to_owned()),
("shift-tab", "Shift+Tab".to_owned()),
("secondary-up", format!("{PRIMARY_WORD}Up")),
("alt-5", "Alt+5".to_owned()),
("space", "Space".to_owned()),
("delete", "Delete".to_owned()),
("secondary-,", format!("{PRIMARY_WORD},")),
("secondary-[", format!("{PRIMARY_WORD}[")),
] {
assert_eq!(
humanize_keystroke(source, KeystrokeStyle::Words).as_deref(),
Some(words),
Some(words.as_str()),
"{source}"
);
}
Expand Down
25 changes: 19 additions & 6 deletions crates/rgitui_workspace/src/keymap/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,23 @@ mod tests {
BindingSpec::user_binding(keystrokes, Some(context), action)
}

/// Word-style spelling of what `secondary-` resolves to here: Command on
/// macOS, Control everywhere else. Only bindings written as `secondary-`
/// follow it — an explicit `ctrl-` binding stays Control on every platform.
const PRIMARY_WORD: &str = if cfg!(target_os = "macos") {
"Cmd+"
} else {
"Ctrl+"
};

/// The drift that motivated this work: the help used to advertise
/// `Ctrl+Shift+F` for Fetch while the registry bound `Ctrl+Shift+R`.
#[test]
fn a_default_label_comes_from_the_registry_keystroke() {
let summary = defaults();
assert_eq!(
summary.display(CommandId::Fetch).as_deref(),
Some("Ctrl+Shift+R")
Some(format!("{PRIMARY_WORD}Shift+R").as_str())
);
assert_eq!(
summary.display(CommandId::Fetch),
Expand Down Expand Up @@ -556,7 +565,7 @@ mod tests {
fn a_command_with_two_keystrokes_lists_both() {
assert_eq!(
defaults().display(CommandId::UnstageAll).as_deref(),
Some("Ctrl+Shift+S or Ctrl+U")
Some(format!("{PRIMARY_WORD}Shift+S or {PRIMARY_WORD}U").as_str())
);
}

Expand Down Expand Up @@ -605,12 +614,16 @@ mod tests {
assert!(commit
.bindings
.iter()
.any(|binding| binding.display == "Ctrl+S" && binding.is_user_defined()));
.any(|binding| binding.display == format!("{PRIMARY_WORD}S")
&& binding.is_user_defined()));

assert_eq!(summary.display(CommandId::StageAll), None);
let warnings = summary.warnings(CommandId::StageAll);
assert_eq!(warnings.len(), 1, "{warnings:?}");
assert!(warnings[0].contains("Ctrl+S"), "{warnings:?}");
assert!(
warnings[0].contains(&format!("{PRIMARY_WORD}S")),
"{warnings:?}"
);
assert!(warnings[0].contains("keymap.json"), "{warnings:?}");
}

Expand Down Expand Up @@ -643,7 +656,7 @@ mod tests {
// Fetch keeps its own default; only the extra binding was dropped.
assert_eq!(
summary.display(CommandId::Fetch).as_deref(),
Some("Ctrl+Shift+R")
Some(format!("{PRIMARY_WORD}Shift+R").as_str())
);
assert_eq!(
summary.display(CommandId::Pull).as_deref(),
Expand All @@ -665,7 +678,7 @@ mod tests {
// The default comes first: defaults are applied before user bindings.
assert_eq!(
summary.display(CommandId::OpenRepo).as_deref(),
Some("Ctrl+O or Ctrl+Alt+K Ctrl+Alt+O")
Some(format!("{PRIMARY_WORD}O or Ctrl+Alt+K Ctrl+Alt+O").as_str())
);
}

Expand Down
Loading