feat: new Path.os() API creates an OS-native Path (#3541)

This commit is contained in:
三咲雅 misaki masa 2026-01-09 19:51:37 +08:00 committed by GitHub
parent b1dd185692
commit 16d20e2171
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 34 additions and 4 deletions

View file

@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
### Added
- Support compressed tarballs (`.tar.gz`, `.tar.bz2`, etc.) in the preset archive previewer ([#3518])
- New `Path.os()` API creates an OS-native `Path` ([#3541])
### Fixed
@ -1597,3 +1598,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
[#3518]: https://github.com/sxyazi/yazi/pull/3518
[#3532]: https://github.com/sxyazi/yazi/pull/3532
[#3540]: https://github.com/sxyazi/yazi/pull/3540
[#3541]: https://github.com/sxyazi/yazi/pull/3541

View file

@ -46,6 +46,18 @@ impl Path {
}
}
pub fn install(lua: &Lua) -> mlua::Result<()> {
lua.globals().raw_set(
"Path",
lua.create_table_from([(
"os",
lua.create_function(|_, s: mlua::String| {
Ok(Self::new(s.as_bytes().as_strand().as_os_path().into_lua_err()?))
})?,
)])?,
)
}
fn ends_with(&self, child: Value) -> mlua::Result<bool> {
match child {
Value::String(s) => {

View file

@ -17,6 +17,7 @@ pub fn slim_lua(name: &str) -> mlua::Result<Lua> {
yazi_binding::Cha::install(&lua)?;
yazi_binding::File::install(&lua)?;
yazi_binding::Url::install(&lua)?;
yazi_binding::Path::install(&lua)?;
yazi_binding::Error::install(&lua)?;
crate::loader::install(&lua)?;

View file

@ -34,6 +34,7 @@ fn stage_1(lua: &'static Lua) -> Result<()> {
crate::process::install(lua)?;
yazi_binding::File::install(lua)?;
yazi_binding::Url::install(lua)?;
yazi_binding::Path::install(lua)?;
// Addons
lua.load(preset!("ya")).set_name("ya.lua").exec()?;

View file

@ -11,6 +11,10 @@ pub enum PathBufDyn {
Unix(typed_path::UnixPathBuf),
}
impl From<&std::path::Path> for PathBufDyn {
fn from(value: &std::path::Path) -> Self { Self::Os(value.into()) }
}
impl From<std::path::PathBuf> for PathBufDyn {
fn from(value: std::path::PathBuf) -> Self { Self::Os(value) }
}

View file

@ -259,8 +259,8 @@ impl<'p> PathDyn<'p> {
let s = suffix.as_strand();
let mut me_comps = self.components();
let mut suf_comps = match self.kind() {
PathKind::Os => Components::Os(std::path::Path::new(s.as_os()?).components()),
PathKind::Unix => Components::Unix(typed_path::UnixPath::new(s.encoded_bytes()).components()),
PathKind::Os => Components::Os(s.as_os_path()?.components()),
PathKind::Unix => Components::Unix(s.as_unix_path().components()),
};
while let Some(next) = suf_comps.next_back() {
@ -279,8 +279,8 @@ impl<'p> PathDyn<'p> {
{
let s = strand.as_strand();
Ok(match kind.into() {
PathKind::Os => Self::Os(std::path::Path::new(s.as_os()?)),
PathKind::Unix => Self::Unix(typed_path::UnixPath::new(s.encoded_bytes())),
PathKind::Os => Self::Os(s.as_os_path()?),
PathKind::Unix => Self::Unix(s.as_unix_path()),
})
}

View file

@ -88,6 +88,16 @@ impl<'a> Strand<'a> {
}
}
#[inline]
pub fn as_os_path(self) -> Result<&'a std::path::Path, StrandError> {
self.as_os().map(std::path::Path::new)
}
#[inline]
pub fn as_unix_path(self) -> &'a typed_path::UnixPath {
typed_path::UnixPath::new(self.encoded_bytes())
}
#[inline]
pub fn as_utf8(self) -> Result<&'a str, StrandError> {
match self {