diff --git a/CHANGELOG.md b/CHANGELOG.md index 48dab470..bb951a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/yazi-binding/src/path.rs b/yazi-binding/src/path.rs index 137a1d84..609c18b0 100644 --- a/yazi-binding/src/path.rs +++ b/yazi-binding/src/path.rs @@ -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 { match child { Value::String(s) => { diff --git a/yazi-plugin/src/isolate/isolate.rs b/yazi-plugin/src/isolate/isolate.rs index afcd67ce..319e75fc 100644 --- a/yazi-plugin/src/isolate/isolate.rs +++ b/yazi-plugin/src/isolate/isolate.rs @@ -17,6 +17,7 @@ pub fn slim_lua(name: &str) -> mlua::Result { 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)?; diff --git a/yazi-plugin/src/lua.rs b/yazi-plugin/src/lua.rs index f0dea9f8..b1bed1a2 100644 --- a/yazi-plugin/src/lua.rs +++ b/yazi-plugin/src/lua.rs @@ -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()?; diff --git a/yazi-shared/src/path/buf.rs b/yazi-shared/src/path/buf.rs index 1ef141e5..b580ddb0 100644 --- a/yazi-shared/src/path/buf.rs +++ b/yazi-shared/src/path/buf.rs @@ -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 for PathBufDyn { fn from(value: std::path::PathBuf) -> Self { Self::Os(value) } } diff --git a/yazi-shared/src/path/path.rs b/yazi-shared/src/path/path.rs index 030b957d..d01c63a5 100644 --- a/yazi-shared/src/path/path.rs +++ b/yazi-shared/src/path/path.rs @@ -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()), }) } diff --git a/yazi-shared/src/strand/strand.rs b/yazi-shared/src/strand/strand.rs index e8501669..1a8f93ee 100644 --- a/yazi-shared/src/strand/strand.rs +++ b/yazi-shared/src/strand/strand.rs @@ -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 {