From 54a760d8f4d97caa2e9fde399b399e799aa30764 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 28 May 2026 08:56:44 +0800 Subject: [PATCH] fix: encode the uri list --- Cargo.lock | 1 + yazi-binding/src/tty.rs | 2 +- yazi-plugin/preset/plugins/dnd.lua | 4 ++-- yazi-plugin/src/utils/text.rs | 11 +++++++++++ yazi-plugin/src/utils/utils.rs | 1 + yazi-shim/Cargo.toml | 21 +++++++++++---------- yazi-shim/src/lib.rs | 4 ++-- yazi-shim/src/percent_encoding.rs | 5 +++++ 8 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 yazi-shim/src/percent_encoding.rs diff --git a/Cargo.lock b/Cargo.lock index 987bd688..139d48c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6140,6 +6140,7 @@ dependencies = [ "base64", "hashbrown 0.17.1", "mlua", + "percent-encoding", "ratatui", "serde", "thiserror 2.0.18", diff --git a/yazi-binding/src/tty.rs b/yazi-binding/src/tty.rs index 728e636f..39f52e74 100644 --- a/yazi-binding/src/tty.rs +++ b/yazi-binding/src/tty.rs @@ -47,7 +47,7 @@ impl Tty { }) } b"FinishDrop" => match &*t.raw_get::("type")? { - b"copy" => write!(w, "{}", FinishDrop::Move), + b"copy" => write!(w, "{}", FinishDrop::Copy), b"move" => write!(w, "{}", FinishDrop::Move), _ => return Err("invalid FinishDrop type".into_lua_err()), }, diff --git a/yazi-plugin/preset/plugins/dnd.lua b/yazi-plugin/preset/plugins/dnd.lua index 37160c14..039cf90b 100644 --- a/yazi-plugin/preset/plugins/dnd.lua +++ b/yazi-plugin/preset/plugins/dnd.lua @@ -3,10 +3,10 @@ local M = {} function M.selected_uri_list() local paths = {} for _, u in pairs(cx.active.selected) do - paths[#paths + 1] = string.format("file://%s", u.path) + paths[#paths + 1] = "file://" .. ya.percent_encode(tostring(u.path)) end if #paths == 0 and cx.active.current.hovered then - paths[1] = string.format("file://%s", cx.active.current.hovered.path) + paths[1] = "file://" .. ya.percent_encode(tostring(cx.active.current.hovered.path)) end return paths end diff --git a/yazi-plugin/src/utils/text.rs b/yazi-plugin/src/utils/text.rs index 30206dcb..f5522195 100644 --- a/yazi-plugin/src/utils/text.rs +++ b/yazi-plugin/src/utils/text.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use mlua::{Function, Lua}; use twox_hash::XxHash3_128; +use yazi_shim::RFC_3986; use yazi_widgets::CLIPBOARD; use super::Utils; @@ -45,4 +46,14 @@ impl Utils { } }) } + + pub(super) fn percent_encode(lua: &Lua) -> mlua::Result { + lua.create_function(|lua, s: mlua::String| { + let b = s.as_bytes(); + match percent_encoding::percent_encode(&b, RFC_3986).into() { + Cow::Borrowed(_) => Ok(s), + Cow::Owned(s) => lua.create_external_string(s), + } + }) + } } diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index e5139720..3bcbb23a 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -66,6 +66,7 @@ pub fn compose( b"hash" => Utils::hash(lua)?, b"quote" => Utils::quote(lua)?, b"clipboard" => Utils::clipboard(lua)?, + b"percent_encode" => Utils::percent_encode(lua)?, b"percent_decode" => Utils::percent_decode(lua)?, // Time diff --git a/yazi-shim/Cargo.toml b/yazi-shim/Cargo.toml index acab142a..ad580071 100644 --- a/yazi-shim/Cargo.toml +++ b/yazi-shim/Cargo.toml @@ -20,16 +20,17 @@ vendored-lua = [ "mlua/vendored" ] yazi-macro = { path = "../yazi-macro", version = "26.5.6" } # External dependencies -arc-swap = { workspace = true } -base64 = { workspace = true } -hashbrown = { workspace = true } -mlua = { workspace = true } -ratatui = { workspace = true } -serde = { workspace = true } -thiserror = { workspace = true } -toml = { workspace = true } -twox-hash = { workspace = true } -unicode-width = { workspace = true } +arc-swap = { workspace = true } +base64 = { workspace = true } +hashbrown = { workspace = true } +mlua = { workspace = true } +percent-encoding = { workspace = true } +ratatui = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } +toml = { workspace = true } +twox-hash = { workspace = true } +unicode-width = { workspace = true } [dependencies.unicode-segmentation] version = "1.13.2" diff --git a/yazi-shim/src/lib.rs b/yazi-shim/src/lib.rs index f6ebfea2..4264d4e1 100644 --- a/yazi-shim/src/lib.rs +++ b/yazi-shim/src/lib.rs @@ -1,6 +1,6 @@ -yazi_macro::mod_pub!(arc_swap base64 cell mlua path ratatui serde strum toml vec); +yazi_macro::mod_pub!(arc_swap cell mlua path ratatui serde strum toml vec); -yazi_macro::mod_flat!(twox utf8); +yazi_macro::mod_flat!(base64 percent_encoding twox utf8); #[cfg(windows)] yazi_macro::mod_flat!(win32); diff --git a/yazi-shim/src/percent_encoding.rs b/yazi-shim/src/percent_encoding.rs new file mode 100644 index 00000000..d2b28111 --- /dev/null +++ b/yazi-shim/src/percent_encoding.rs @@ -0,0 +1,5 @@ +use percent_encoding::{AsciiSet, NON_ALPHANUMERIC}; + +// RFC 3986 path component: encode everything that is not a safe path character. +pub const RFC_3986: &AsciiSet = + &NON_ALPHANUMERIC.remove(b'-').remove(b'.').remove(b'_').remove(b'~');