mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: expose directory creation as part of the fs module
This commit is contained in:
parent
8800bae0aa
commit
58dd8ffc0a
1 changed files with 22 additions and 2 deletions
|
|
@ -1,9 +1,16 @@
|
||||||
use globset::GlobBuilder;
|
use globset::GlobBuilder;
|
||||||
use mlua::{ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, MetaMethod, Table, Value};
|
use mlua::{
|
||||||
|
ExternalError, ExternalResult, Function, IntoLua, IntoLuaMulti, Lua, MetaMethod, Table, Value,
|
||||||
|
};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use yazi_fs::remove_dir_clean;
|
use yazi_fs::remove_dir_clean;
|
||||||
|
|
||||||
use crate::{Error, bindings::{Cast, Cha}, file::File, url::{Url, UrlRef}};
|
use crate::{
|
||||||
|
Error,
|
||||||
|
bindings::{Cast, Cha},
|
||||||
|
file::File,
|
||||||
|
url::{Url, UrlRef},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
|
pub fn compose(lua: &Lua) -> mlua::Result<Table> {
|
||||||
let index = lua.create_function(|lua, (ts, key): (Table, mlua::String)| {
|
let index = lua.create_function(|lua, (ts, key): (Table, mlua::String)| {
|
||||||
|
|
@ -12,6 +19,7 @@ pub fn compose(lua: &Lua) -> mlua::Result<Table> {
|
||||||
b"cha" => cha(lua)?,
|
b"cha" => cha(lua)?,
|
||||||
b"write" => write(lua)?,
|
b"write" => write(lua)?,
|
||||||
b"remove" => remove(lua)?,
|
b"remove" => remove(lua)?,
|
||||||
|
b"create_dir" => create_dir(lua)?,
|
||||||
b"read_dir" => read_dir(lua)?,
|
b"read_dir" => read_dir(lua)?,
|
||||||
b"unique_name" => unique_name(lua)?,
|
b"unique_name" => unique_name(lua)?,
|
||||||
_ => return Ok(Value::Nil),
|
_ => return Ok(Value::Nil),
|
||||||
|
|
@ -76,6 +84,18 @@ fn remove(lua: &Lua) -> mlua::Result<Function> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_dir(lua: &Lua) -> mlua::Result<Function> {
|
||||||
|
lua.create_async_function(|lua, (dir, recursive): (UrlRef, Option<bool>)| async move {
|
||||||
|
let is_recursive = recursive.unwrap_or(false);
|
||||||
|
let result =
|
||||||
|
if is_recursive { fs::create_dir_all(&*dir).await } else { fs::create_dir(&*dir).await };
|
||||||
|
match result {
|
||||||
|
Ok(_) => (true, Value::Nil).into_lua_multi(&lua),
|
||||||
|
Err(e) => (false, Error::Io(e)).into_lua_multi(&lua),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn read_dir(lua: &Lua) -> mlua::Result<Function> {
|
fn read_dir(lua: &Lua) -> mlua::Result<Function> {
|
||||||
lua.create_async_function(|lua, (dir, options): (UrlRef, Table)| async move {
|
lua.create_async_function(|lua, (dir, options): (UrlRef, Table)| async move {
|
||||||
let glob = if let Ok(s) = options.raw_get::<mlua::String>("glob") {
|
let glob = if let Ok(s) = options.raw_get::<mlua::String>("glob") {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue