fix: displaying duplicate command suggestions

Fixes https://github.com/sxyazi/yazi/issues/955
This commit is contained in:
Mika Vilpas 2024-04-29 16:42:46 +03:00
parent 3a09155328
commit 0f02b9e348

View file

@ -1,6 +1,6 @@
use std::str::FromStr;
use std::{collections::HashSet, str::FromStr};
use yazi_config::{keymap::{Control, Key}, KEYMAP};
use yazi_config::{keymap::{Control, ControlCow, Key}, KEYMAP};
use yazi_shared::{event::Cmd, render, Layer};
use crate::which::{Which, WhichSorter};
@ -45,12 +45,7 @@ impl Which {
pub fn show_with(&mut self, key: &Key, layer: Layer) {
self.layer = layer;
self.times = 1;
self.cands = KEYMAP
.get(layer)
.iter()
.filter(|c| c.on.len() > 1 && &c.on[0] == key)
.map(|c| c.into())
.collect();
self.cands = keymap_candidates(layer, key);
WhichSorter::default().sort(&mut self.cands);
self.visible = true;
@ -58,3 +53,13 @@ impl Which {
render!();
}
}
fn keymap_candidates(layer: Layer, key: &Key) -> Vec<ControlCow> {
let mut seen = HashSet::new();
let mut matches =
KEYMAP.get(layer).iter().filter(|c| c.on.len() > 1 && &c.on[0] == key).collect::<Vec<_>>();
matches.retain(|c| seen.insert(c.on.clone()));
matches.into_iter().map(|c| c.into()).collect()
}