Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update crossterm and crokey to new versions #101

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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
38 changes: 27 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ categories = ["command-line-utilities"]


[dependencies]
crossterm = "0.24.0"
crossterm = "0.28"
dirs = "5.0"
regex = "1.10"
serde_json = "1.0"
serde = { version = "1.0", features = ["rc"] }
textwrap = "0.16"
unicode-segmentation = "1.12"
crokey = "0.5"
crokey = "1.1"
strum_macros = "0.26"
strum = { version = "0.26", features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ impl TereAppState {
pub fn move_cursor(&mut self, amount: isize, wrap: bool) {
let old_cursor_pos = self.cursor_pos;
let n_visible_items = self.visible_items().len();
let max_cursor_pos = self.main_win_h - 1;
let max_cursor_pos = self.main_win_h.saturating_sub(1);
let old_scroll_pos = self.scroll_pos;

// pointer_pos: the global location of the cursor in ls_output_buf
Expand Down
61 changes: 32 additions & 29 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ impl TereSettings {
if let Some(false) = args.get_one::<bool>("clear-default-keymap") {
ret.keymap = DEFAULT_KEYMAP
.iter()
.map(|(k, c, a)| ((*k, c.clone()), a.clone()))
// (*k).into() converts crokey KeyCombinaton to crossterm KeyEvent
.map(|(k, c, a)| (((*k).into(), c.clone()), a.clone()))
.collect();
}

Expand Down Expand Up @@ -322,15 +323,17 @@ fn parse_keymap_arg(arg: &str) -> Result<Vec<(KeyEvent, ActionContext, Action)>,
))
};

ret.push((k, c, a));
ret.push((k.into(), c, a));
}

Ok(ret)
}

// NOTE: can't create a const hashmap (without an extra dependency like phf), so just using a slice
// of tuples.
pub const DEFAULT_KEYMAP: &[(KeyEvent, ActionContext, Action)] = &[
// NOTE: The key combinations are saved as crokey KeyCombinations, and converted to crossterm
// KeyEvents when this array is read during initialization
pub const DEFAULT_KEYMAP: &[(crokey::KeyCombination, ActionContext, Action)] = &[

(key!(enter), ActionContext::None, Action::ChangeDir),
(key!(right), ActionContext::None, Action::ChangeDir),
Expand Down Expand Up @@ -416,7 +419,7 @@ mod tests {

DEFAULT_KEYMAP
.iter()
.for_each(|(k, c, _)| *key_counts.entry((*k, c.clone())).or_default() += 1);
.for_each(|(k, c, _)| *key_counts.entry(((*k).into(), c.clone())).or_default() += 1);

for (k, v) in key_counts {
assert_eq!(v, 1, "found {} entries for key {:?} in context {:?}", v, k.0, k.1);
Expand All @@ -443,7 +446,7 @@ mod tests {
let m = parse_keymap_arg("ctrl-x:Exit").unwrap();
assert_eq!(m.len(), 1);
let (e, c, a) = &m[0];
assert_eq!(e, &key!(ctrl-x));
assert_eq!(e, &key!(ctrl-x).into());
assert_eq!(c, &ActionContext::None);
assert_eq!(a, &Action::Exit);
}
Expand All @@ -452,10 +455,10 @@ mod tests {
fn test_parse_keymap_arg2() {
let m = parse_keymap_arg("ctrl-x:Exit,ctrl-j:NotSearching:CursorUp").unwrap();
assert_eq!(m.len(), 2);
assert_eq!(m[0].0, key!(ctrl-x));
assert_eq!(m[0].0, key!(ctrl-x).into());
assert_eq!(m[0].1, ActionContext::None);
assert_eq!(m[0].2, Action::Exit);
assert_eq!(m[1].0, key!(ctrl-j));
assert_eq!(m[1].0, key!(ctrl-j).into());
assert_eq!(m[1].1, ActionContext::NotSearching);
assert_eq!(m[1].2, Action::CursorUp);
}
Expand All @@ -466,7 +469,7 @@ mod tests {
"foo",
"-m", "ctrl-x:Exit",
]);
assert_eq!(settings.keymap.get(&(key!(ctrl-x), ActionContext::None)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(ctrl-x).into(), ActionContext::None)), Some(&Action::Exit));
}

#[test]
Expand All @@ -475,8 +478,8 @@ mod tests {
"foo",
"-m", "ctrl-x:Exit,ctrl-y:ClearSearch",
]);
assert_eq!(settings.keymap.get(&(key!(ctrl-x), ActionContext::None)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(ctrl-y), ActionContext::None)), Some(&Action::ClearSearch));
assert_eq!(settings.keymap.get(&(key!(ctrl-x).into(), ActionContext::None)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(ctrl-y).into(), ActionContext::None)), Some(&Action::ClearSearch));
}

#[test]
Expand All @@ -485,7 +488,7 @@ mod tests {
"foo",
"-m", "ctrl-x:Exit,ctrl-x:ClearSearch", // repeated mapping
]);
assert_eq!(settings.keymap.get(&(key!(ctrl-x), ActionContext::None)), Some(&Action::ClearSearch));
assert_eq!(settings.keymap.get(&(key!(ctrl-x).into(), ActionContext::None)), Some(&Action::ClearSearch));
}

#[test]
Expand All @@ -496,7 +499,7 @@ mod tests {
"-m", "ctrl-x:Exit",
"-m", "ctrl-x:ClearSearch",
]);
assert_eq!(settings.keymap.get(&(key!(ctrl-x), ActionContext::None)), Some(&Action::ClearSearch));
assert_eq!(settings.keymap.get(&(key!(ctrl-x).into(), ActionContext::None)), Some(&Action::ClearSearch));
}

#[test]
Expand Down Expand Up @@ -564,43 +567,43 @@ mod tests {
let settings = parse_cli_no_warnings(vec![
"foo",
]);
assert_eq!(settings.keymap.get(&(key!(alt-h), ActionContext::None)), Some(&Action::ChangeDirParent));
assert_eq!(settings.keymap.get(&(key!(alt-j), ActionContext::None)), Some(&Action::CursorDown));
assert_eq!(settings.keymap.get(&(key!(alt-k), ActionContext::None)), Some(&Action::CursorUp));
assert_eq!(settings.keymap.get(&(key!(alt-l), ActionContext::None)), Some(&Action::ChangeDir));
assert_eq!(settings.keymap.get(&(key!(alt-h).into(), ActionContext::None)), Some(&Action::ChangeDirParent));
assert_eq!(settings.keymap.get(&(key!(alt-j).into(), ActionContext::None)), Some(&Action::CursorDown));
assert_eq!(settings.keymap.get(&(key!(alt-k).into(), ActionContext::None)), Some(&Action::CursorUp));
assert_eq!(settings.keymap.get(&(key!(alt-l).into(), ActionContext::None)), Some(&Action::ChangeDir));

let settings = parse_cli_no_warnings(vec![
"foo",
"-m", "alt-h:None,alt-j:None,alt-k:None,alt-l:None",
]);
assert_eq!(settings.keymap.get(&(key!(alt-h), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-j), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-k), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-l), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-h).into(), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-j).into(), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-k).into(), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(alt-l).into(), ActionContext::None)), None);
}

#[test]
fn test_unmap2() {
let settings = parse_cli_no_warnings(vec![
"foo",
]);
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::NotSearching)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::Searching)), Some(&Action::ClearSearch));
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::NotSearching)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::Searching)), Some(&Action::ClearSearch));
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::None)), None);

assert_eq!(settings.keymap.get(&(key!(backspace), ActionContext::Searching)), Some(&Action::EraseSearchChar));
assert_eq!(settings.keymap.get(&(key!(backspace), ActionContext::NotSearching)), Some(&Action::ChangeDirParent));
assert_eq!(settings.keymap.get(&(key!(backspace), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(backspace).into(), ActionContext::Searching)), Some(&Action::EraseSearchChar));
assert_eq!(settings.keymap.get(&(key!(backspace).into(), ActionContext::NotSearching)), Some(&Action::ChangeDirParent));
assert_eq!(settings.keymap.get(&(key!(backspace).into(), ActionContext::None)), None);

let settings = parse_cli_no_warnings(vec![
"foo",
"-m", "esc:Searching:None",
"-m", "backspace:None", // this shouldn't affect any of the mappings since they are context-dependent
"-m", "backspace:None:None", // this shouldn't affect any of the mappings since they are context-dependent
]);
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::NotSearching)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::Searching)), None);
assert_eq!(settings.keymap.get(&(key!(esc), ActionContext::None)), None);
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::NotSearching)), Some(&Action::Exit));
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::Searching)), None);
assert_eq!(settings.keymap.get(&(key!(esc).into(), ActionContext::None)), None);
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/ui/help_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn get_keyboard_shortcuts_table() -> &'static str {
fn get_justified_keyboard_shortcuts_table(
key_mapping: &HashMap<(KeyEvent, ActionContext), Action>,
) -> String {
let formatter = crokey::KeyEventFormat::default();
let formatter = crokey::KeyCombinationFormat::default();

let keyboard_shortcuts = get_keyboard_shortcuts_table();

Expand Down Expand Up @@ -156,7 +156,7 @@ fn invert_key_mapping_sorted(
// probably not the right place for this, but I'll move
// it out if I need it elsewhere.
fn cmp_key_events(k1: &KeyEvent, k2: &KeyEvent) -> std::cmp::Ordering {
let formatter = crokey::KeyEventFormat::default();
let formatter = crokey::KeyCombinationFormat::default();
match (k1.modifiers.is_empty(), k2.modifiers.is_empty()) {
(true, true) | (false, false) => {
// both or neither have modifiers, sort alphabetically
Expand Down Expand Up @@ -251,7 +251,7 @@ mod tests {
.collect();
for k in key_combos {
key_mappings
.entry(k)
.entry(k.into())
.and_modify(|a| a.push(action.clone()))
.or_insert_with(|| vec![action.clone()]);
}
Expand All @@ -277,8 +277,8 @@ mod tests {
continue;
}

let key_combo_str = crokey::KeyEventFormat::default().to_string(*key_combo);
let actions = key_mappings.get(key_combo).unwrap_or_else(|| {
let key_combo_str = crokey::KeyCombinationFormat::default().to_string(*key_combo);
let actions = key_mappings.get(&(*key_combo).into()).unwrap_or_else(|| {
panic!(
"Key mapping {}:{} not found in README",
key_combo_str, expected_action,
Expand Down
Loading