mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-21 23:01:05 +00:00
feat: new Path.os() API creates an OS-native Path (#3541)
This commit is contained in:
parent
b1dd185692
commit
16d20e2171
7 changed files with 34 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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)?;
|
||||
|
|
|
|||
|
|
@ -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()?;
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue