mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: support getting file owner and group in lua
This commit is contained in:
parent
5c62cf2c65
commit
a66eaef0b9
8 changed files with 108 additions and 0 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -2191,6 +2191,16 @@ dependencies = [
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "users"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
|
|
@ -2726,6 +2736,7 @@ dependencies = [
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"tracing",
|
"tracing",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
|
"users",
|
||||||
"yazi-adaptor",
|
"yazi-adaptor",
|
||||||
"yazi-config",
|
"yazi-config",
|
||||||
"yazi-prebuild",
|
"yazi-prebuild",
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,4 @@ tokio-util = "^0"
|
||||||
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
|
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
|
||||||
unicode-width = "^0"
|
unicode-width = "^0"
|
||||||
yazi-prebuild = "0.1.2"
|
yazi-prebuild = "0.1.2"
|
||||||
|
users = "0.11"
|
||||||
|
|
@ -140,11 +140,35 @@ function Status:progress(area, offset)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Status:owner(area)
|
||||||
|
local h = cx.active.current.hovered
|
||||||
|
if ya.target_family() ~= "unix" or h == nil then
|
||||||
|
return ui.Span("")
|
||||||
|
end
|
||||||
|
|
||||||
|
local spans = {}
|
||||||
|
|
||||||
|
local uid = h.cha.uid
|
||||||
|
if uid then
|
||||||
|
spans[#spans + 1] = ui.Span(ya.get_user_name_by_uid(uid) or tostring(uid))
|
||||||
|
spans[#spans + 1] = ui.Span(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
local gid = h.cha.gid
|
||||||
|
if gid then
|
||||||
|
spans[#spans + 1] = ui.Span(ya.get_group_name_by_gid(gid) or tostring(gid))
|
||||||
|
spans[#spans + 1] = ui.Span(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
return ui.Line(spans)
|
||||||
|
end
|
||||||
|
|
||||||
function Status:render(area)
|
function Status:render(area)
|
||||||
self.area = area
|
self.area = area
|
||||||
|
|
||||||
local left = ui.Line { self:mode(), self:size(), self:name() }
|
local left = ui.Line { self:mode(), self:size(), self:name() }
|
||||||
local right = ui.Line { self:permissions(), self:percentage(), self:position() }
|
local right = ui.Line { self:permissions(), self:percentage(), self:position() }
|
||||||
|
-- local right = ui.Line({ self:owner(), self:permissions(), self:percentage(), self:position() })
|
||||||
local progress = self:progress(area, right:width())
|
local progress = self:progress(area, right:width())
|
||||||
return {
|
return {
|
||||||
ui.Paragraph(area, { left }),
|
ui.Paragraph(area, { left }),
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ impl Cha {
|
||||||
reg.add_field_method_get("is_char_device", |_, me| Ok(me.is_char_device()));
|
reg.add_field_method_get("is_char_device", |_, me| Ok(me.is_char_device()));
|
||||||
reg.add_field_method_get("is_fifo", |_, me| Ok(me.is_fifo()));
|
reg.add_field_method_get("is_fifo", |_, me| Ok(me.is_fifo()));
|
||||||
reg.add_field_method_get("is_socket", |_, me| Ok(me.is_socket()));
|
reg.add_field_method_get("is_socket", |_, me| Ok(me.is_socket()));
|
||||||
|
reg.add_field_method_get("uid", |_, me| Ok(me.uid));
|
||||||
|
reg.add_field_method_get("gid", |_, me| Ok(me.gid));
|
||||||
}
|
}
|
||||||
reg.add_field_method_get("length", |_, me| Ok(me.len));
|
reg.add_field_method_get("length", |_, me| Ok(me.len));
|
||||||
reg.add_field_method_get("created", |_, me| {
|
reg.add_field_method_get("created", |_, me| {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ mod preview;
|
||||||
mod target;
|
mod target;
|
||||||
mod text;
|
mod text;
|
||||||
mod time;
|
mod time;
|
||||||
|
#[cfg(unix)]
|
||||||
|
mod unix_user;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub use preview::*;
|
pub use preview::*;
|
||||||
|
|
|
||||||
52
yazi-plugin/src/utils/unix_user.rs
Normal file
52
yazi-plugin/src/utils/unix_user.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use mlua::{Lua, Table};
|
||||||
|
use users::{Groups, Users, UsersCache};
|
||||||
|
|
||||||
|
use super::Utils;
|
||||||
|
|
||||||
|
impl Utils {
|
||||||
|
pub(super) fn unix_user(lua: &Lua, ya: &Table) -> mlua::Result<()> {
|
||||||
|
let arc_cache = Arc::new(UsersCache::new());
|
||||||
|
|
||||||
|
{
|
||||||
|
let cache = Arc::clone(&arc_cache);
|
||||||
|
ya.set("get_current_uid", lua.create_function(move |_, ()| Ok(cache.get_current_uid()))?)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let cache = Arc::clone(&arc_cache);
|
||||||
|
ya.set("get_current_gid", lua.create_function(move |_, ()| Ok(cache.get_current_gid()))?)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let cache = Arc::clone(&arc_cache);
|
||||||
|
ya.set(
|
||||||
|
"get_user_name_by_uid",
|
||||||
|
lua.create_function(move |_, uid: u32| {
|
||||||
|
Ok(
|
||||||
|
cache
|
||||||
|
.get_user_by_uid(uid)
|
||||||
|
.and_then(|u| u.name().to_str().and_then(|n| Some(n.to_owned()))),
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let cache = Arc::clone(&arc_cache);
|
||||||
|
ya.set(
|
||||||
|
"get_group_name_by_gid",
|
||||||
|
lua.create_function(move |_, gid: u32| {
|
||||||
|
Ok(
|
||||||
|
cache
|
||||||
|
.get_group_by_gid(gid)
|
||||||
|
.and_then(|g| g.name().to_str().and_then(|n| Some(n.to_owned()))),
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,8 @@ pub fn install(lua: &Lua) -> mlua::Result<()> {
|
||||||
Utils::target(lua, &ya)?;
|
Utils::target(lua, &ya)?;
|
||||||
Utils::time(lua, &ya)?;
|
Utils::time(lua, &ya)?;
|
||||||
Utils::text(lua, &ya)?;
|
Utils::text(lua, &ya)?;
|
||||||
|
#[cfg(unix)]
|
||||||
|
Utils::unix_user(lua, &ya)?;
|
||||||
|
|
||||||
lua.globals().set("ya", ya)
|
lua.globals().set("ya", ya)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ pub struct Cha {
|
||||||
pub modified: Option<SystemTime>,
|
pub modified: Option<SystemTime>,
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub permissions: u32,
|
pub permissions: u32,
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub uid: u32,
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub gid: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Metadata> for Cha {
|
impl From<Metadata> for Cha {
|
||||||
|
|
@ -68,6 +72,16 @@ impl From<Metadata> for Cha {
|
||||||
use std::os::unix::prelude::PermissionsExt;
|
use std::os::unix::prelude::PermissionsExt;
|
||||||
m.permissions().mode()
|
m.permissions().mode()
|
||||||
},
|
},
|
||||||
|
#[cfg(unix)]
|
||||||
|
uid: {
|
||||||
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
m.uid()
|
||||||
|
},
|
||||||
|
#[cfg(unix)]
|
||||||
|
gid: {
|
||||||
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
m.gid()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue