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
use anyhow::Result;
use mlua::Lua;
use yazi_config::BOOT;
use yazi_shared::RoCell;

pub static LUA: RoCell<Lua> = RoCell::new();

pub fn init() {
	fn stage_1(lua: &Lua) -> Result<()> {
		crate::Loader::init();
		crate::Config::new(lua).install_boot()?.install_manager()?.install_theme()?;
		crate::utils::init();
		crate::utils::install(lua)?;

		// Base
		lua.load(include_str!("../preset/inspect/inspect.lua")).exec()?;
		lua.load(include_str!("../preset/ui.lua")).exec()?;
		lua.load(include_str!("../preset/ya.lua")).exec()?;
		crate::elements::init(lua)?;

		// Components
		lua.load(include_str!("../preset/components/current.lua")).exec()?;
		lua.load(include_str!("../preset/components/folder.lua")).exec()?;
		lua.load(include_str!("../preset/components/header.lua")).exec()?;
		lua.load(include_str!("../preset/components/manager.lua")).exec()?;
		lua.load(include_str!("../preset/components/parent.lua")).exec()?;
		lua.load(include_str!("../preset/components/preview.lua")).exec()?;
		lua.load(include_str!("../preset/components/progress.lua")).exec()?;
		lua.load(include_str!("../preset/components/status.lua")).exec()?;

		Ok(())
	}

	fn stage_2(lua: &Lua) {
		let setup = br#"
ya.SYNC_ON = true
package.path = BOOT.plugin_dir .. "/?.yazi/init.lua;" .. package.path
"#;
		lua.load(setup as &[u8]).exec().unwrap();

		if let Ok(b) = std::fs::read(BOOT.config_dir.join("init.lua")) {
			lua.load(b).exec().unwrap();
		}
	}

	let lua = Lua::new();
	stage_1(&lua).expect("failed to initialize Lua");
	stage_2(&lua);
	LUA.init(lua);
}