yazi_core/tab/commands/
toggle.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
use yazi_macro::render_and;
use yazi_proxy::AppProxy;
use yazi_shared::{event::CmdCow, url::Url};

use crate::tab::Tab;

struct Opt {
	url:   Option<Url>,
	state: Option<bool>,
}

impl From<CmdCow> for Opt {
	fn from(mut c: CmdCow) -> Self {
		Self {
			url:   c.take_first_url(),
			state: match c.str("state") {
				Some("on") => Some(true),
				Some("off") => Some(false),
				_ => None,
			},
		}
	}
}

impl Tab {
	#[yazi_codegen::command]
	pub fn toggle(&mut self, opt: Opt) {
		let Some(url) = opt.url.as_ref().or(self.current.hovered().map(|h| &h.url)) else {
			return;
		};

		let b = match opt.state {
			Some(true) => render_and!(self.selected.add(url)),
			Some(false) => render_and!(self.selected.remove(url)) | true,
			None => render_and!(self.selected.remove(url) || self.selected.add(url)),
		};

		if !b {
			AppProxy::notify_warn(
				"Toggle",
				"This file cannot be selected, due to path nesting conflict.",
			);
		}
	}
}