feat: new fs.unique() creates a unique file or directory

This commit is contained in:
sxyazi 2026-02-11 12:36:56 +08:00
parent a8a4b684a1
commit bf056a8bb4
No known key found for this signature in database
5 changed files with 44 additions and 27 deletions

36
Cargo.lock generated
View file

@ -276,9 +276,9 @@ dependencies = [
[[package]]
name = "avif-serialize"
version = "0.8.6"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47c8fbc0f831f4519fe8b810b6a7a91410ec83031b8233f730a0480029f6a23f"
checksum = "375082f007bd67184fb9c0374614b29f9aaa604ec301635f72338bb65386a53d"
dependencies = [
"arrayvec",
]
@ -2193,9 +2193,9 @@ checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
[[package]]
name = "libc"
version = "0.2.180"
version = "0.2.181"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
checksum = "459427e2af2b9c839b132acb702a1c654d95e10f8c326bfc2ad11310e458b1c5"
[[package]]
name = "libcrux-intrinsics"
@ -2265,9 +2265,9 @@ dependencies = [
[[package]]
name = "libfuzzer-sys"
version = "0.4.10"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5037190e1f70cbeef565bd267599242926f724d3b8a9f510fd7e0b540cfa4404"
checksum = "f12a681b7dd8ce12bff52488013ba614b869148d54dd79836ab85aafdd53f08d"
dependencies = [
"arbitrary",
"cc",
@ -3843,9 +3843,9 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "ryu"
version = "1.0.22"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984"
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
[[package]]
name = "safe_arch"
@ -4633,9 +4633,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.9.11+spec-1.1.0"
version = "0.9.12+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3afc9a848309fe1aaffaed6e1546a7a14de1f935dc9d89d32afd9a44bab7c46"
checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863"
dependencies = [
"indexmap 2.13.0",
"serde_core",
@ -4657,9 +4657,9 @@ dependencies = [
[[package]]
name = "toml_parser"
version = "1.0.6+spec-1.1.0"
version = "1.0.7+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44"
checksum = "247eaa3197818b831697600aadf81514e577e0cba5eab10f7e064e78ae154df1"
dependencies = [
"winnow",
]
@ -4772,9 +4772,9 @@ dependencies = [
[[package]]
name = "typed-path"
version = "0.12.2"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3015e6ce46d5ad8751e4a772543a30c7511468070e98e64e20165f8f81155b64"
checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e"
[[package]]
name = "typeid"
@ -4796,9 +4796,9 @@ checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971"
[[package]]
name = "unicode-ident"
version = "1.0.22"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e"
[[package]]
name = "unicode-segmentation"
@ -6160,9 +6160,9 @@ dependencies = [
[[package]]
name = "zmij"
version = "1.0.19"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ff05f8caa9038894637571ae6b9e29466c1f4f829d26c9b28f869a29cbe3445"
checksum = "4de98dfa5d5b7fef4ee834d0073d560c9ca7b6c46a71d058c48db7960f8cfaf7"
[[package]]
name = "zune-core"

View file

@ -41,7 +41,7 @@ futures = "0.3.31"
globset = "0.4.18"
hashbrown = { version = "0.16.1", features = [ "serde" ] }
indexmap = { version = "2.13.0", features = [ "serde" ] }
libc = "0.2.180"
libc = "0.2.181"
lru = "0.16.3"
mlua = { version = "0.11.6", features = [ "anyhow", "async", "error-send", "lua55", "macros", "serde" ] }
objc2 = "0.6.3"
@ -62,10 +62,10 @@ thiserror = "2.0.18"
tokio = { version = "1.49.0", features = [ "full" ] }
tokio-stream = "0.1.18"
tokio-util = "0.7.18"
toml = { version = "0.9.11" }
toml = { version = "0.9.12" }
tracing = { version = "0.1.44", features = [ "max_level_debug", "release_max_level_debug" ] }
twox-hash = { version = "2.1.2", default-features = false, features = [ "std", "random", "xxhash3_128" ] }
typed-path = "0.12.2"
typed-path = "0.12.3"
unicode-width = { version = "0.2.2", default-features = false }
uzers = "0.12.2"

View file

@ -32,9 +32,9 @@ macro_rules! deprecate {
($lua:ident, $tt:tt) => {{
static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
if !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
let id = match $crate::runtime!($lua)?.current() {
Some(id) => &format!("`{id}.yazi` plugin"),
None => "`init.lua` config",
let id = match $crate::runtime!($lua)?.current()? {
"init" => "`init.lua` config file",
s => &format!("`{s}.yazi` plugin"),
};
yazi_macro::emit!(Call(
yazi_macro::relay!(app:deprecate).with("content", format!($tt, id))

View file

@ -45,7 +45,7 @@ function M:try_with(from, pwd, to)
fail("Invalid URL '%s'", from)
end
local tmp = fs.unique_name(to:join(self.tmp_name(from)))
local tmp = fs.unique("dir", to:join(self.tmp_name(from)))
if not tmp then
fail("Failed to determine a temporary directory for %s", from)
end
@ -94,7 +94,7 @@ function M:tidy(from, to, tmp)
target = to:join(self.trim_ext(from.name))
end
target = fs.unique_name(target)
target = fs.unique(only and "file" or "dir", target)
if not target then
fail("Failed to determine a target for '%s'", from)
end

View file

@ -1,7 +1,7 @@
use std::str::FromStr;
use mlua::{ExternalError, Function, IntoLua, IntoLuaMulti, Lua, Table, Value};
use yazi_binding::{Cha, Composer, ComposerGet, ComposerSet, Error, File, SizeCalculator, Url, UrlRef};
use yazi_binding::{Cha, Composer, ComposerGet, ComposerSet, Error, File, SizeCalculator, Url, UrlRef, deprecate};
use yazi_config::Pattern;
use yazi_fs::{mounts::PARTITIONS, provider::{Attrs, DirReader, FileHolder}};
use yazi_shared::url::{UrlCow, UrlLike};
@ -22,6 +22,7 @@ pub fn compose() -> Composer<ComposerGet, ComposerSet> {
b"read_dir" => read_dir(lua)?,
b"remove" => remove(lua)?,
b"rename" => rename(lua)?,
b"unique" => unique(lua)?,
b"unique_name" => unique_name(lua)?,
b"write" => write(lua)?,
_ => return Ok(Value::Nil),
@ -215,8 +216,24 @@ fn rename(lua: &Lua) -> mlua::Result<Function> {
})
}
fn unique(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, (r#type, url): (mlua::String, UrlRef)| async move {
let result = match &*r#type.as_bytes() {
b"dir" => yazi_vfs::unique_file(url.clone(), true).await,
b"file" => yazi_vfs::unique_file(url.clone(), false).await,
_ => Err("Type must be 'dir' or 'file'".into_lua_err())?,
};
match result {
Ok(u) => Url::new(u).into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),
}
})
}
fn unique_name(lua: &Lua) -> mlua::Result<Function> {
lua.create_async_function(|lua, url: UrlRef| async move {
deprecate!(lua, "`fs.unique_name()` is deprecated, use `fs.unique()` instead, in your {}");
match yazi_vfs::unique_name(url.clone(), async { false }).await {
Ok(u) => Url::new(u).into_lua_multi(&lua),
Err(e) => (Value::Nil, Error::Io(e)).into_lua_multi(&lua),