feat: support getting file owner and group in lua

This commit is contained in:
urie 2024-01-02 13:37:13 +08:00
parent 5c62cf2c65
commit a66eaef0b9
8 changed files with 108 additions and 0 deletions

11
Cargo.lock generated
View file

@ -2191,6 +2191,16 @@ dependencies = [
"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]]
name = "utf8parse"
version = "0.2.1"
@ -2726,6 +2736,7 @@ dependencies = [
"tokio-util",
"tracing",
"unicode-width",
"users",
"yazi-adaptor",
"yazi-config",
"yazi-prebuild",

View file

@ -31,3 +31,4 @@ tokio-util = "^0"
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
unicode-width = "^0"
yazi-prebuild = "0.1.2"
users = "0.11"

View file

@ -140,11 +140,35 @@ function Status:progress(area, offset)
}
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)
self.area = area
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:owner(), self:permissions(), self:percentage(), self:position() })
local progress = self:progress(area, right:width())
return {
ui.Paragraph(area, { left }),

View file

@ -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_fifo", |_, me| Ok(me.is_fifo()));
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("created", |_, me| {

View file

@ -9,6 +9,8 @@ mod preview;
mod target;
mod text;
mod time;
#[cfg(unix)]
mod unix_user;
mod utils;
pub use preview::*;

View 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(())
}
}

View file

@ -14,6 +14,8 @@ pub fn install(lua: &Lua) -> mlua::Result<()> {
Utils::target(lua, &ya)?;
Utils::time(lua, &ya)?;
Utils::text(lua, &ya)?;
#[cfg(unix)]
Utils::unix_user(lua, &ya)?;
lua.globals().set("ya", ya)
}

View file

@ -27,6 +27,10 @@ pub struct Cha {
pub modified: Option<SystemTime>,
#[cfg(unix)]
pub permissions: u32,
#[cfg(unix)]
pub uid: u32,
#[cfg(unix)]
pub gid: u32,
}
impl From<Metadata> for Cha {
@ -68,6 +72,16 @@ impl From<Metadata> for Cha {
use std::os::unix::prelude::PermissionsExt;
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()
},
}
}
}