Switch to uzers crate from users

This commit is contained in:
sxyazi 2024-01-03 01:11:07 +08:00
parent a66eaef0b9
commit d750909a9d
No known key found for this signature in database
7 changed files with 54 additions and 69 deletions

22
Cargo.lock generated
View file

@ -2191,22 +2191,22 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uzers"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76d283dc7e8c901e79e32d077866eaf599156cbf427fffa8289aecc52c5c3f63"
dependencies = [
"libc",
"log",
]
[[package]]
name = "validator"
version = "0.16.1"
@ -2736,7 +2736,7 @@ dependencies = [
"tokio-util",
"tracing",
"unicode-width",
"users",
"uzers",
"yazi-adaptor",
"yazi-config",
"yazi-prebuild",

View file

@ -30,5 +30,5 @@ tokio = { version = "^1", features = [ "parking_lot", "rt-multi-thread"
tokio-util = "^0"
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
unicode-width = "^0"
uzers = "^0"
yazi-prebuild = "0.1.2"
users = "0.11"

0
yazi-plugin/src/os/os.rs Normal file
View file

View file

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

View file

@ -1,52 +0,0 @@
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

@ -0,0 +1,39 @@
use mlua::{Lua, Table};
use uzers::{Groups, Users, UsersCache};
use yazi_shared::RoCell;
use super::Utils;
static CACHE: RoCell<UsersCache> = RoCell::new();
impl Utils {
pub(super) fn user(lua: &Lua, ya: &Table) -> mlua::Result<()> {
CACHE.with(Default::default);
ya.set("uid", lua.create_function(|_, ()| Ok(CACHE.get_current_uid()))?)?;
ya.set("gid", lua.create_function(|_, ()| Ok(CACHE.get_current_gid()))?)?;
ya.set(
"user_name",
lua.create_function(|lua, uid: Option<u32>| {
CACHE
.get_user_by_uid(uid.unwrap_or_else(|| CACHE.get_current_uid()))
.map(|s| lua.create_string(s.name().as_encoded_bytes()))
.transpose()
})?,
)?;
ya.set(
"group_name",
lua.create_function(|lua, gid: Option<u32>| {
CACHE
.get_group_by_gid(gid.unwrap_or_else(|| CACHE.get_current_gid()))
.map(|s| lua.create_string(s.name().as_encoded_bytes()))
.transpose()
})?,
)?;
Ok(())
}
}

View file

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