refactor: deprecate the Command:args() method (#2752)

This commit is contained in:
三咲雅 · Misaki Masa 2025-05-14 01:29:03 +08:00 committed by GitHub
parent ea90b0477e
commit 55f69fbfee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 53 additions and 106 deletions

62
Cargo.lock generated
View file

@ -274,15 +274,6 @@ dependencies = [
"wyz",
]
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "block2"
version = "0.5.1"
@ -627,16 +618,6 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "darling"
version = "0.20.11"
@ -733,16 +714,6 @@ dependencies = [
"syn",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "dirs"
version = "6.0.0"
@ -1023,16 +994,6 @@ dependencies = [
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.16"
@ -1456,16 +1417,6 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest",
]
[[package]]
name = "memchr"
version = "2.7.4"
@ -2845,12 +2796,6 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"
@ -2976,12 +2921,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "walkdir"
version = "2.5.0"
@ -3606,7 +3545,6 @@ dependencies = [
"futures",
"globset",
"libc",
"md-5",
"mlua",
"parking_lot",
"paste",

View file

@ -34,7 +34,6 @@ globset = "0.4.16"
indexmap = { version = "2.9.0", features = [ "serde" ] }
libc = "0.2.172"
lru = "0.14.0"
md-5 = "0.10.6"
mlua = { version = "0.10.3", features = [ "anyhow", "async", "error-send", "lua54", "macros", "serialize" ] }
objc = "0.2.7"
parking_lot = "0.12.3"

View file

@ -66,11 +66,11 @@ impl UserData for Error {
match (lhs, rhs) {
(Value::String(l), Value::UserData(r)) => {
let r = r.borrow::<Self>()?;
lua.create_string([l.as_bytes().as_ref(), r.to_string().as_bytes()].concat())
lua.create_string([&l.as_bytes(), r.to_string().as_bytes()].concat())
}
(Value::UserData(l), Value::String(r)) => {
let l = l.borrow::<Self>()?;
lua.create_string([l.to_string().as_bytes(), r.as_bytes().as_ref()].concat())
lua.create_string([l.to_string().as_bytes(), &r.as_bytes()].concat())
}
_ => Err("only string can be concatenated with Error".into_lua_err()),
}

View file

@ -138,7 +138,7 @@ impl UserData for Url {
lua.create_string(me.as_os_str().as_encoded_bytes())
});
methods.add_meta_method(MetaMethod::Concat, |lua, lhs, rhs: mlua::String| {
lua.create_string([lhs.as_os_str().as_encoded_bytes(), rhs.as_bytes().as_ref()].concat())
lua.create_string([lhs.as_os_str().as_encoded_bytes(), &rhs.as_bytes()].concat())
});
}
}

View file

@ -1,4 +1,4 @@
#![allow(clippy::module_inception, clippy::unit_arg)]
#![allow(clippy::if_same_then_else, clippy::module_inception, clippy::unit_arg)]
#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
#[global_allocator]

View file

@ -31,7 +31,6 @@ base64 = { workspace = true }
crossterm = { workspace = true }
futures = { workspace = true }
globset = { workspace = true }
md-5 = { workspace = true }
mlua = { workspace = true }
parking_lot = { workspace = true }
paste = { workspace = true }

View file

@ -48,7 +48,7 @@ function M.spawn_7z(args)
local last_err = nil
local try = function(name)
local stdout = args[1] == "l" and Command.PIPED or Command.NULL
local child, err = Command(name):args(args):stdout(stdout):stderr(Command.PIPED):spawn()
local child, err = Command(name):arg(args):stdout(stdout):stderr(Command.PIPED):spawn()
if not child then
last_err = err
end

View file

@ -2,7 +2,7 @@ local M = {}
function M:peek(job)
local cmd = os.getenv("YAZI_FILE_ONE") or "file"
local output, err = Command(cmd):args({ "-bL", "--", tostring(job.file.url) }):stdout(Command.PIPED):output()
local output, err = Command(cmd):arg({ "-bL", "--", tostring(job.file.url) }):stdout(Command.PIPED):output()
local text
if output then

View file

@ -27,7 +27,7 @@ function M:preload(job)
return true
end
local status, err = Command("magick"):args({
local status, err = Command("magick"):arg({
"-size",
"800x560",
"-gravity",

View file

@ -2,7 +2,7 @@ local M = {}
function M:peek(job)
local child = Command("jq")
:args({ "-b", "-C", "--tab", ".", tostring(job.file.url) })
:arg({ "-b", "-C", "--tab", ".", tostring(job.file.url) })
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:spawn()

View file

@ -50,13 +50,13 @@ function M:spot(job) require("file"):spot(job) end
function M.with_limit()
local cmd = Command("magick"):args { "-limit", "thread", 1 }
if rt.tasks.image_alloc > 0 then
cmd = cmd:args({ "-limit", "memory", rt.tasks.image_alloc }):args { "-limit", "disk", "1MiB" }
cmd:arg { "-limit", "memory", rt.tasks.image_alloc, "-limit", "disk", "1MiB" }
end
if rt.tasks.image_bound[1] > 0 then
cmd = cmd:args { "-limit", "width", rt.tasks.image_bound[1] }
cmd:arg { "-limit", "width", rt.tasks.image_bound[1] }
end
if rt.tasks.image_bound[2] > 0 then
cmd = cmd:args { "-limit", "height", rt.tasks.image_bound[2] }
cmd:arg { "-limit", "height", rt.tasks.image_bound[2] }
end
return cmd
end

View file

@ -16,7 +16,7 @@ function M:fetch(job)
end
local cmd = os.getenv("YAZI_FILE_ONE") or "file"
local child, err = Command(cmd):args({ "-bL", "--mime-type", "--" }):args(urls):stdout(Command.PIPED):spawn()
local child, err = Command(cmd):arg({ "-bL", "--mime-type", "--" }):arg(urls):stdout(Command.PIPED):spawn()
if not child then
return true, Err("Failed to start `%s`, error: %s", cmd, err)
end

View file

@ -33,7 +33,7 @@ function M:preload(job)
-- stylua: ignore
local output, err = Command("pdftoppm")
:args({
:arg({
"-f", job.skip + 1,
"-l", job.skip + 1,
"-singlefile",

View file

@ -53,7 +53,7 @@ function M:preload(job)
end
-- stylua: ignore
local cmd = Command("ffmpeg"):args({
local cmd = Command("ffmpeg"):arg({
"-v", "quiet", "-threads", 1, "-hwaccel", "auto",
"-skip_frame", "nokey",
"-an", "-sn", "-dn",
@ -68,7 +68,7 @@ function M:preload(job)
end
-- stylua: ignore
local status, err = cmd:args({
local status, err = cmd:arg({
"-vframes", 1,
"-q:v", 31 - math.floor(rt.preview.image_quality * 0.3),
"-vf", string.format("scale='min(%d,iw)':'min(%d,ih)':force_original_aspect_ratio=decrease:flags=fast_bilinear", rt.preview.max_width, rt.preview.max_height),
@ -131,7 +131,7 @@ function M.list_meta(url, entries)
cmd = cmd:args { "-select_streams", "v" }
end
local output, err = cmd:args({ "-show_entries", entries, "-of", "json=c=1", tostring(url) }):output()
local output, err = cmd:arg({ "-show_entries", entries, "-of", "json=c=1", tostring(url) }):output()
if not output then
return nil, Err("Failed to start `ffprobe`, error: %s", err)
end

View file

@ -49,7 +49,7 @@ local function options()
end
local function empty(cwd)
local child = Command("zoxide"):args({ "query", "-l", "--exclude" }):arg(cwd):stdout(Command.PIPED):spawn()
local child = Command("zoxide"):arg({ "query", "-l", "--exclude", cwd }):stdout(Command.PIPED):spawn()
if not child then
return true
end
@ -89,8 +89,7 @@ local function entry()
local _permit = ya.hide()
local child, err1 = Command("zoxide")
:args({ "query", "-i", "--exclude" })
:arg(st.cwd)
:arg({ "query", "-i", "--exclude", st.cwd })
:env("SHELL", "sh")
:env("CLICOLOR", 1)
:env("CLICOLOR_FORCE", 1)

View file

@ -8,7 +8,7 @@ impl Composer {
F: Fn(&Lua, &[u8]) -> mlua::Result<Value> + 'static,
{
let index = lua.create_function(move |lua, (ts, key): (Table, mlua::String)| {
let v = f(lua, key.as_bytes().as_ref())?;
let v = f(lua, &key.as_bytes())?;
ts.raw_set(key, v.clone())?;
Ok(v)
})?;

View file

@ -1,4 +1,4 @@
#![allow(clippy::unit_arg)]
#![allow(clippy::if_same_then_else, clippy::unit_arg)]
mod macros;

View file

@ -135,11 +135,30 @@ impl UserData for Command {
)
}
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, mlua::String)| {
ud.borrow_mut::<Self>()?.inner.arg(arg.to_string_lossy());
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, Value)| {
{
let mut me = ud.borrow_mut::<Self>()?;
match arg {
Value::String(s) => {
me.inner.arg(String::from_utf8_lossy(&s.as_bytes()).as_ref());
}
Value::Table(t) => {
for s in t.sequence_values::<mlua::String>() {
me.inner.arg(String::from_utf8_lossy(&s?.as_bytes()).as_ref());
}
}
_ => return Err("arg must be a string or table of strings".into_lua_err()),
}
}
Ok(ud)
});
methods.add_function_mut("args", |_, (ud, args): (AnyUserData, Vec<mlua::String>)| {
// TODO: remove this
methods.add_function_mut("args", |lua, (ud, args): (AnyUserData, Vec<mlua::String>)| {
crate::deprecate!(
lua,
"The `args()` method on `Command` is deprecated, use `arg()` instead in your {}\n\nSee #2752 for more details: https://github.com/sxyazi/yazi/pull/2752"
);
{
let mut me = ud.borrow_mut::<Self>()?;
for arg in args {
@ -155,7 +174,10 @@ impl UserData for Command {
methods.add_function_mut(
"env",
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
ud.borrow_mut::<Self>()?.inner.env(key.to_string_lossy(), value.to_string_lossy());
ud.borrow_mut::<Self>()?.inner.env(
String::from_utf8_lossy(&key.as_bytes()).as_ref(),
String::from_utf8_lossy(&value.as_bytes()).as_ref(),
);
Ok(ud)
},
);

View file

@ -1,6 +1,4 @@
use std::ops::ControlFlow;
use md5::{Digest, Md5};
use mlua::{Function, Lua, Table};
use twox_hash::XxHash3_128;
use unicode_width::UnicodeWidthChar;
@ -9,17 +7,9 @@ use super::Utils;
use crate::CLIPBOARD;
impl Utils {
pub(super) fn hash(lua: &Lua, deprecated: bool) -> mlua::Result<Function> {
lua.create_async_function(move |lua, s: mlua::String| async move {
if deprecated {
crate::deprecate!(
lua,
"The `ya.md5()` function is deprecated, please use `ya.hash()` instead, in your {}"
);
Ok(format!("{:x}", Md5::new_with_prefix(s.as_bytes()).finalize()))
} else {
Ok(format!("{:x}", XxHash3_128::oneshot(s.as_bytes().as_ref())))
}
pub(super) fn hash(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(move |_, s: mlua::String| async move {
Ok(format!("{:x}", XxHash3_128::oneshot(&s.as_bytes())))
})
}
@ -39,20 +29,20 @@ impl Utils {
fn truncate_impl(mut chars: impl Iterator<Item = char>, max: usize) -> Vec<char> {
let mut width = 0;
let flow = chars.try_fold(Vec::with_capacity(max), |mut v, c| {
width += c.width().unwrap_or(0);
width += c.width().unwrap_or(0);
if width < max {
v.push(c);
ControlFlow::Continue(v)
} else {
ControlFlow::Break(v)
}
}
});
match flow {
ControlFlow::Break(v) => v,
ControlFlow::Continue(v) => v,
}
}
}
lua.create_function(|_, (text, t): (mlua::String, Table)| {
let (max, text) = (t.raw_get("max")?, text.to_string_lossy());

View file

@ -65,7 +65,7 @@ pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result<Value> {
b"target_family" => Utils::target_family(lua)?,
// Text
b"hash" => Utils::hash(lua, false)?,
b"hash" => Utils::hash(lua)?,
b"quote" => Utils::quote(lua)?,
b"truncate" => Utils::truncate(lua)?,
b"clipboard" => Utils::clipboard(lua)?,