yazi_core/manager/commands/
tab_create.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
use yazi_boot::BOOT;
use yazi_macro::render;
use yazi_proxy::AppProxy;
use yazi_shared::{event::CmdCow, url::Url};

use crate::{manager::Tabs, tab::Tab};

const MAX_TABS: usize = 9;

struct Opt {
	url:     Url,
	current: bool,
}

impl From<CmdCow> for Opt {
	fn from(mut c: CmdCow) -> Self {
		if c.bool("current") {
			Self { url: Default::default(), current: true }
		} else {
			Self {
				url:     c.take_first_url().unwrap_or_else(|| Url::from(&BOOT.cwds[0])),
				current: false,
			}
		}
	}
}

impl Tabs {
	#[yazi_codegen::command]
	pub fn create(&mut self, opt: Opt) {
		if self.items.len() >= MAX_TABS {
			AppProxy::notify_warn("Too many tabs", "You can only open up to 9 tabs at the same time.");
			return;
		}

		let mut tab = Tab::default();
		if !opt.current {
			tab.cd(opt.url);
		} else if let Some(h) = self.active().hovered() {
			tab.pref = self.active().pref.clone();
			tab.apply_files_attrs();
			tab.reveal(h.url.to_regular());
		} else {
			tab.pref = self.active().pref.clone();
			tab.apply_files_attrs();
			tab.cd(self.active().cwd().to_regular());
		}

		self.items.insert(self.cursor + 1, tab);
		self.set_idx(self.cursor + 1);
		render!();
	}
}