Skip to content

Commit 373d5c8

Browse files
committed
feat: add list_meta() method to archive plugin (#1638)
1 parent bc68d1e commit 373d5c8

File tree

9 files changed

+130
-51
lines changed

9 files changed

+130
-51
lines changed

yazi-config/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use yazi_shared::{RoCell, Xdg};
77
pub mod keymap;
88
mod layout;
99
mod log;
10+
mod macros;
1011
pub mod manager;
1112
pub mod open;
1213
mod pattern;

yazi-config/src/macros.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[macro_export]
2+
macro_rules! preset {
3+
($name:literal) => {{
4+
#[cfg(debug_assertions)]
5+
{
6+
std::borrow::Cow::Owned(
7+
std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/preset/", $name, ".toml"))
8+
.expect(concat!("Failed to read 'yazi-config/preset/", $name, ".toml'")),
9+
)
10+
}
11+
#[cfg(not(debug_assertions))]
12+
{
13+
std::borrow::Cow::Borrowed(include_str!(concat!("../preset/", $name, ".toml")))
14+
}
15+
}};
16+
}

yazi-config/src/preset.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ use std::{borrow::Cow, path::{Path, PathBuf}};
33
use anyhow::{anyhow, Context, Result};
44
use toml::{Table, Value};
55

6-
use crate::theme::Flavor;
6+
use crate::{preset, theme::Flavor};
77

88
pub(crate) struct Preset;
99

1010
impl Preset {
1111
pub(crate) fn yazi(p: &Path) -> Result<Cow<str>> {
12-
Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml"))
12+
Self::merge_path(p.join("yazi.toml"), preset!("yazi"))
1313
}
1414

1515
pub(crate) fn keymap(p: &Path) -> Result<Cow<str>> {
16-
Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml"))
16+
Self::merge_path(p.join("keymap.toml"), preset!("keymap"))
1717
}
1818

1919
pub(crate) fn theme(p: &Path) -> Result<Cow<str>> {
2020
let Ok(user) = std::fs::read_to_string(p.join("theme.toml")) else {
21-
return Ok(include_str!("../preset/theme.toml").into());
21+
return Ok(preset!("theme"));
2222
};
2323
let Some(use_) = Flavor::parse_use(&user) else {
24-
return Self::merge_str(&user, include_str!("../preset/theme.toml"));
24+
return Self::merge_str(&user, &preset!("theme"));
2525
};
2626

2727
let p = p.join(format!("flavors/{use_}.yazi/flavor.toml"));
2828
let flavor =
2929
std::fs::read_to_string(&p).with_context(|| anyhow!("Failed to load flavor {p:?}"))?;
3030

31-
Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml"))?)
31+
Self::merge_str(&user, &Self::merge_str(&flavor, &preset!("theme"))?)
3232
}
3333

3434
#[inline]
@@ -48,13 +48,13 @@ impl Preset {
4848
}
4949

5050
#[inline]
51-
fn merge_path(user: PathBuf, base: &str) -> Result<Cow<str>> {
51+
fn merge_path(user: PathBuf, base: Cow<str>) -> Result<Cow<str>> {
5252
let s = std::fs::read_to_string(&user).unwrap_or_default();
5353
if s.is_empty() {
54-
return Ok(base.into());
54+
return Ok(base);
5555
}
5656

57-
Self::merge_str(&s, base).with_context(|| anyhow!("Loading {user:?}"))
57+
Self::merge_str(&s, &base).with_context(|| anyhow!("Loading {user:?}"))
5858
}
5959

6060
fn merge(a: &mut Table, b: Table, max: u8) {

yazi-plugin/preset/plugins/archive.lua

+44-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ end
8181
---@param args table
8282
---@param skip integer
8383
---@param limit integer
84-
---@return table
85-
---@return integer
86-
---@return integer
84+
---@return table files
85+
---@return integer bound
86+
---@return integer code
8787
--- 0: success
8888
--- 1: failed to spawn
8989
--- 2: wrong password
@@ -137,6 +137,47 @@ function M:list_files(args, skip, limit)
137137
return files, i, code
138138
end
139139

140+
---List metadata of an archive
141+
---@param args table
142+
---@return string|nil type
143+
---@return integer code
144+
--- 0: success
145+
--- 1: failed to spawn
146+
--- 2: wrong password
147+
--- 3: partial success
148+
function M:list_meta(args)
149+
local child = self:spawn_7z { "l", "-slt", table.unpack(args) }
150+
if not child then
151+
return nil, 1
152+
end
153+
154+
local i, head = 0, ""
155+
local typ, code = nil, 0
156+
while i < 500 do
157+
i = i + 1
158+
159+
local next, event = child:read_line()
160+
if event == 1 and self:is_encrypted(next) then
161+
code = 2
162+
break
163+
elseif event == 1 then
164+
code = 3
165+
elseif event == 0 then
166+
head = head .. next
167+
else
168+
break
169+
end
170+
171+
typ = head:gmatch("--[\r\n]+Path = .-[\r\n]+Type = (.-)[\r\n]+")()
172+
if typ then
173+
break
174+
end
175+
end
176+
177+
child:start_kill()
178+
return typ ~= "" and typ or nil, code
179+
end
180+
140181
function M:is_encrypted(s) return s:find(" Wrong password", 1, true) end
141182

142183
return M

yazi-plugin/preset/plugins/extract.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function M:try_with(url, pwd)
5656
local output, err = child:wait_with_output()
5757
if output and output.status.code == 2 and archive:is_encrypted(output.stderr) then
5858
fs.remove("dir_clean", tmp)
59-
return true -- Needs retry
59+
return true -- Need to retry
6060
end
6161

6262
self:tidy(url, tmp)

yazi-plugin/src/isolate/isolate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use mlua::Lua;
22

3-
use crate::{elements, runtime::Runtime};
3+
use crate::{elements, preset, runtime::Runtime};
44

55
pub fn slim_lua(name: &str) -> mlua::Result<Lua> {
66
let lua = Lua::new();
@@ -17,7 +17,7 @@ pub fn slim_lua(name: &str) -> mlua::Result<Lua> {
1717
crate::process::install(&lua)?;
1818
crate::utils::install_isolate(&lua)?;
1919
crate::Config::new(&lua).install_preview()?;
20-
lua.load(include_str!("../../preset/ya.lua")).set_name("ya.lua").exec()?;
20+
lua.load(preset!("ya")).set_name("ya.lua").exec()?;
2121

2222
// Elements
2323
let ui = lua.create_table()?;

yazi-plugin/src/loader/loader.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use tokio::fs;
77
use yazi_boot::BOOT;
88
use yazi_shared::RoCell;
99

10+
use crate::preset;
11+
1012
pub static LOADER: RoCell<Loader> = RoCell::new();
1113

1214
#[derive(Default)]
@@ -21,32 +23,32 @@ impl Loader {
2123
}
2224

2325
let preset = match name {
24-
"archive" => &include_bytes!("../../preset/plugins/archive.lua")[..],
25-
"code" => include_bytes!("../../preset/plugins/code.lua"),
26-
"dds" => include_bytes!("../../preset/plugins/dds.lua"),
27-
"empty" => include_bytes!("../../preset/plugins/empty.lua"),
28-
"extract" => include_bytes!("../../preset/plugins/extract.lua"),
29-
"file" => include_bytes!("../../preset/plugins/file.lua"),
30-
"folder" => include_bytes!("../../preset/plugins/folder.lua"),
31-
"font" => include_bytes!("../../preset/plugins/font.lua"),
32-
"fzf" => include_bytes!("../../preset/plugins/fzf.lua"),
33-
"image" => include_bytes!("../../preset/plugins/image.lua"),
34-
"json" => include_bytes!("../../preset/plugins/json.lua"),
35-
"magick" => include_bytes!("../../preset/plugins/magick.lua"),
36-
"mime" => include_bytes!("../../preset/plugins/mime.lua"),
37-
"noop" => include_bytes!("../../preset/plugins/noop.lua"),
38-
"pdf" => include_bytes!("../../preset/plugins/pdf.lua"),
39-
"session" => include_bytes!("../../preset/plugins/session.lua"),
40-
"video" => include_bytes!("../../preset/plugins/video.lua"),
41-
"zoxide" => include_bytes!("../../preset/plugins/zoxide.lua"),
42-
_ => b"",
26+
"archive" => preset!("plugins/archive"),
27+
"code" => preset!("plugins/code"),
28+
"dds" => preset!("plugins/dds"),
29+
"empty" => preset!("plugins/empty"),
30+
"extract" => preset!("plugins/extract"),
31+
"file" => preset!("plugins/file"),
32+
"folder" => preset!("plugins/folder"),
33+
"font" => preset!("plugins/font"),
34+
"fzf" => preset!("plugins/fzf"),
35+
"image" => preset!("plugins/image"),
36+
"json" => preset!("plugins/json"),
37+
"magick" => preset!("plugins/magick"),
38+
"mime" => preset!("plugins/mime"),
39+
"noop" => preset!("plugins/noop"),
40+
"pdf" => preset!("plugins/pdf"),
41+
"session" => preset!("plugins/session"),
42+
"video" => preset!("plugins/video"),
43+
"zoxide" => preset!("plugins/zoxide"),
44+
_ => Default::default(),
4345
};
4446

4547
let b = if preset.is_empty() {
4648
let p = BOOT.plugin_dir.join(format!("{name}.yazi/init.lua"));
4749
Cow::Owned(fs::read(&p).await.with_context(|| format!("failed to load plugin from {p:?}"))?)
4850
} else {
49-
Cow::Borrowed(preset)
51+
preset.into()
5052
};
5153

5254
self.cache.write().insert(name.to_owned(), b);

yazi-plugin/src/lua.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use mlua::Lua;
33
use yazi_boot::BOOT;
44
use yazi_shared::RoCell;
55

6-
use crate::runtime::Runtime;
6+
use crate::{preset, runtime::Runtime};
77

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

@@ -21,7 +21,7 @@ fn stage_1(lua: &'static Lua) -> Result<()> {
2121

2222
// Base
2323
lua.set_named_registry_value("rt", Runtime::default())?;
24-
lua.load(include_str!("../preset/ya.lua")).set_name("ya.lua").exec()?;
24+
lua.load(preset!("ya")).set_name("ya.lua").exec()?;
2525
crate::bindings::Icon::register(lua)?;
2626
crate::bindings::MouseEvent::register(lua)?;
2727
crate::elements::pour(lua)?;
@@ -32,25 +32,26 @@ fn stage_1(lua: &'static Lua) -> Result<()> {
3232
crate::url::pour(lua)?;
3333

3434
// Components
35-
lua.load(include_str!("../preset/components/current.lua")).set_name("current.lua").exec()?;
36-
lua.load(include_str!("../preset/components/entity.lua")).set_name("entity.lua").exec()?;
37-
lua.load(include_str!("../preset/components/header.lua")).set_name("header.lua").exec()?;
38-
lua.load(include_str!("../preset/components/linemode.lua")).set_name("linemode.lua").exec()?;
39-
lua.load(include_str!("../preset/components/marker.lua")).set_name("marker.lua").exec()?;
40-
lua.load(include_str!("../preset/components/parent.lua")).set_name("parent.lua").exec()?;
41-
lua.load(include_str!("../preset/components/preview.lua")).set_name("preview.lua").exec()?;
42-
lua.load(include_str!("../preset/components/progress.lua")).set_name("progress.lua").exec()?;
43-
lua.load(include_str!("../preset/components/rail.lua")).set_name("rail.lua").exec()?;
44-
lua.load(include_str!("../preset/components/root.lua")).set_name("root.lua").exec()?;
45-
lua.load(include_str!("../preset/components/status.lua")).set_name("status.lua").exec()?;
46-
lua.load(include_str!("../preset/components/tab.lua")).set_name("tab.lua").exec()?;
35+
lua.load(preset!("components/current")).set_name("current.lua").exec()?;
36+
lua.load(preset!("components/entity")).set_name("entity.lua").exec()?;
37+
lua.load(preset!("components/header")).set_name("header.lua").exec()?;
38+
lua.load(preset!("components/linemode")).set_name("linemode.lua").exec()?;
39+
40+
lua.load(preset!("components/marker")).set_name("marker.lua").exec()?;
41+
lua.load(preset!("components/parent")).set_name("parent.lua").exec()?;
42+
lua.load(preset!("components/preview")).set_name("preview.lua").exec()?;
43+
lua.load(preset!("components/progress")).set_name("progress.lua").exec()?;
44+
lua.load(preset!("components/rail")).set_name("rail.lua").exec()?;
45+
lua.load(preset!("components/root")).set_name("root.lua").exec()?;
46+
lua.load(preset!("components/status")).set_name("status.lua").exec()?;
47+
lua.load(preset!("components/tab")).set_name("tab.lua").exec()?;
4748

4849
Ok(())
4950
}
5051

5152
fn stage_2(lua: &'static Lua) -> mlua::Result<()> {
52-
lua.load(include_str!("../preset/setup.lua")).set_name("setup.lua").exec()?;
53-
lua.load(include_str!("../preset/compat.lua")).set_name("compat.lua").exec()?;
53+
lua.load(preset!("setup")).set_name("setup.lua").exec()?;
54+
lua.load(preset!("compat")).set_name("compat.lua").exec()?;
5455

5556
if let Ok(b) = std::fs::read(BOOT.config_dir.join("init.lua")) {
5657
lua.load(b).set_name("init.lua").exec()?;

yazi-plugin/src/macros.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
#[macro_export]
2+
macro_rules! preset {
3+
($name:literal) => {{
4+
#[cfg(debug_assertions)]
5+
{
6+
std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/preset/", $name, ".lua")).expect(concat!(
7+
"Failed to read 'yazi-plugin/preset/",
8+
$name,
9+
".lua'"
10+
))
11+
}
12+
#[cfg(not(debug_assertions))]
13+
{
14+
&include_bytes!(concat!("../preset/", $name, ".lua"))[..]
15+
}
16+
}};
17+
}
18+
119
#[macro_export]
220
macro_rules! impl_style_method {
321
($methods:ident, $($field:tt).+) => {

0 commit comments

Comments
 (0)