yazi_core/which/commands/
show.rs

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::str::FromStr;

use yazi_config::{KEYMAP, keymap::{Chord, Key}};
use yazi_macro::render;
use yazi_shared::{Layer, event::CmdCow};

use crate::which::{Which, WhichSorter};

pub struct Opt {
	cands:  Vec<Chord>,
	layer:  Layer,
	silent: bool,
}

impl TryFrom<CmdCow> for Opt {
	type Error = anyhow::Error;

	fn try_from(mut c: CmdCow) -> Result<Self, Self::Error> {
		Ok(Self {
			cands:  c.take_any("candidates").unwrap_or_default(),
			layer:  Layer::from_str(&c.take_str("layer").unwrap_or_default())?,
			silent: c.bool("silent"),
		})
	}
}

impl Which {
	pub fn show(&mut self, opt: impl TryInto<Opt>) {
		let Ok(opt) = opt.try_into() else {
			return;
		};

		if opt.cands.is_empty() {
			return;
		}

		self.layer = opt.layer;
		self.times = 0;
		self.cands = opt.cands.into_iter().map(|c| c.into()).collect();

		self.visible = true;
		self.silent = opt.silent;
		render!();
	}

	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();

		WhichSorter::default().sort(&mut self.cands);
		self.visible = true;
		self.silent = false;
		render!();
	}
}