From cf769bf533259b2f157d813852327985bc0f55b1 Mon Sep 17 00:00:00 2001 From: Urie <43716456+Urie96@users.noreply.github.com> Date: Wed, 3 Jan 2024 01:32:29 +0800 Subject: [PATCH] feat: add user and group-related plugin APIs (#469) --- Cargo.lock | 83 ++++++++++++++---------- yazi-core/Cargo.toml | 4 +- yazi-plugin/Cargo.toml | 5 +- yazi-plugin/preset/components/status.lua | 6 +- yazi-plugin/src/bindings/cha.rs | 2 + yazi-plugin/src/plugin.rs | 1 + yazi-plugin/src/utils/mod.rs | 1 + yazi-plugin/src/utils/user.rs | 41 ++++++++++++ yazi-plugin/src/utils/utils.rs | 11 +++- yazi-shared/Cargo.toml | 4 +- yazi-shared/src/fs/cha.rs | 14 ++++ 11 files changed, 129 insertions(+), 43 deletions(-) create mode 100644 yazi-plugin/src/utils/user.rs diff --git a/Cargo.lock b/Cargo.lock index 0243451a..a48c9349 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,9 +120,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.77" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9" +checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" [[package]] name = "arc-swap" @@ -339,7 +339,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -483,9 +483,9 @@ dependencies = [ [[package]] name = "deranged" -version = "0.3.10" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eb30d70a07a3b04884d2677f06bec33509dc67ca60d92949e5535352d3191dc" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", ] @@ -720,7 +720,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -838,9 +838,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.58" +version = "0.1.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -1192,7 +1192,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -1461,9 +1461,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.71" +version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +checksum = "2de98502f212cfcea8d0bb305bd0f49d7ebdd75b64ba0a68f937d888f4e0d6db" dependencies = [ "unicode-ident", ] @@ -1488,9 +1488,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1649,9 +1649,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773" dependencies = [ "serde_derive", ] @@ -1668,20 +1668,20 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "6fbd975230bada99c8bb618e0c365c2eefa219158d5c6c29610fd09ff1833257" dependencies = [ "itoa", "ryu", @@ -1835,7 +1835,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -1851,9 +1851,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.43" +version = "2.0.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "89456b690ff72fddcecf231caedbe615c59480c93358a93dfae7fc29e3ebbf0e" dependencies = [ "proc-macro2", "quote", @@ -1882,22 +1882,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -1994,7 +1994,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -2087,7 +2087,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] @@ -2197,6 +2197,16 @@ 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" @@ -2299,7 +2309,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", "wasm-bindgen-shared", ] @@ -2321,7 +2331,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2393,11 +2403,11 @@ dependencies = [ [[package]] name = "windows-core" -version = "0.51.1" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] @@ -2726,6 +2736,7 @@ dependencies = [ "tokio-util", "tracing", "unicode-width", + "uzers", "yazi-adaptor", "yazi-config", "yazi-prebuild", @@ -2793,7 +2804,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.46", ] [[package]] diff --git a/yazi-core/Cargo.toml b/yazi-core/Cargo.toml index 7e60a52e..579246b8 100644 --- a/yazi-core/Cargo.toml +++ b/yazi-core/Cargo.toml @@ -22,7 +22,6 @@ bitflags = "^2" crossterm = "^0" futures = "^0" indexmap = "^2" -libc = "^0" notify = { version = "^6", default-features = false, features = [ "macos_fsevent" ] } parking_lot = "^0" ratatui = "^0" @@ -37,5 +36,8 @@ unicode-width = "^0" # Logging tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } +[target."cfg(unix)".dependencies] +libc = "^0" + [target."cfg(windows)".dependencies] clipboard-win = "^4" diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index e777ba3e..666787e7 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -18,7 +18,6 @@ ansi-to-tui = "^3" anyhow = "^1" crossterm = "^0" futures = "^0" -libc = "^0" md-5 = "^0" mlua = { version = "^0", features = [ "lua54", "vendored", "serialize", "macros", "async" ] } parking_lot = "^0" @@ -31,3 +30,7 @@ tokio-util = "^0" tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } unicode-width = "^0" yazi-prebuild = "0.1.2" + +[target."cfg(unix)".dependencies] +libc = "^0" +uzers = "^0" diff --git a/yazi-plugin/preset/components/status.lua b/yazi-plugin/preset/components/status.lua index 96bcc22e..e25c3d20 100644 --- a/yazi-plugin/preset/components/status.lua +++ b/yazi-plugin/preset/components/status.lua @@ -28,7 +28,7 @@ end function Status:size() local h = cx.active.current.hovered if h == nil then - return ui.Span("") + return ui.Line {} end local style = self.style() @@ -50,12 +50,12 @@ end function Status:permissions() local h = cx.active.current.hovered if h == nil then - return ui.Span("") + return ui.Line {} end local perm = h.cha:permissions() if perm == nil then - return ui.Span("") + return ui.Line {} end local spans = {} diff --git a/yazi-plugin/src/bindings/cha.rs b/yazi-plugin/src/bindings/cha.rs index 6589466d..630fe2a2 100644 --- a/yazi-plugin/src/bindings/cha.rs +++ b/yazi-plugin/src/bindings/cha.rs @@ -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| { diff --git a/yazi-plugin/src/plugin.rs b/yazi-plugin/src/plugin.rs index 5d1676c5..f32799ff 100644 --- a/yazi-plugin/src/plugin.rs +++ b/yazi-plugin/src/plugin.rs @@ -9,6 +9,7 @@ pub fn init() { fn stage_1(lua: &Lua) -> Result<()> { crate::Loader::init(); crate::Config::new(lua).install_boot()?.install_manager()?.install_theme()?; + crate::utils::init(); crate::utils::install(lua)?; // Base diff --git a/yazi-plugin/src/utils/mod.rs b/yazi-plugin/src/utils/mod.rs index 9f5baa10..e4b94e6a 100644 --- a/yazi-plugin/src/utils/mod.rs +++ b/yazi-plugin/src/utils/mod.rs @@ -9,6 +9,7 @@ mod preview; mod target; mod text; mod time; +mod user; mod utils; pub use preview::*; diff --git a/yazi-plugin/src/utils/user.rs b/yazi-plugin/src/utils/user.rs new file mode 100644 index 00000000..216b09ac --- /dev/null +++ b/yazi-plugin/src/utils/user.rs @@ -0,0 +1,41 @@ +use mlua::{Lua, Table}; + +use super::Utils; + +impl Utils { + #[cfg(unix)] + pub(super) fn user(lua: &Lua, ya: &Table) -> mlua::Result<()> { + use uzers::{Groups, Users}; + + use crate::utils::USERS_CACHE; + + ya.set("uid", lua.create_function(|_, ()| Ok(USERS_CACHE.get_current_uid()))?)?; + + ya.set("gid", lua.create_function(|_, ()| Ok(USERS_CACHE.get_current_gid()))?)?; + + ya.set( + "user_name", + lua.create_function(|lua, uid: Option| { + USERS_CACHE + .get_user_by_uid(uid.unwrap_or_else(|| USERS_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| { + USERS_CACHE + .get_group_by_gid(gid.unwrap_or_else(|| USERS_CACHE.get_current_gid())) + .map(|s| lua.create_string(s.name().as_encoded_bytes())) + .transpose() + })?, + )?; + + Ok(()) + } + + #[cfg(windows)] + pub(super) fn user(_lua: &Lua, _ya: &Table) -> mlua::Result<()> { Ok(()) } +} diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index 44bda2c3..ffe1be50 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -1,7 +1,15 @@ use mlua::{Lua, Table}; +#[cfg(unix)] +pub(super) static USERS_CACHE: yazi_shared::RoCell = yazi_shared::RoCell::new(); + pub(super) struct Utils; +pub fn init() { + #[cfg(unix)] + USERS_CACHE.with(Default::default); +} + pub fn install(lua: &Lua) -> mlua::Result<()> { let ya: Table = lua.create_table()?; @@ -12,8 +20,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)?; + Utils::time(lua, &ya)?; + Utils::user(lua, &ya)?; lua.globals().set("ya", ya) } diff --git a/yazi-shared/Cargo.toml b/yazi-shared/Cargo.toml index e4c4e59e..4ef12e80 100644 --- a/yazi-shared/Cargo.toml +++ b/yazi-shared/Cargo.toml @@ -13,10 +13,12 @@ anyhow = "^1" bitflags = "^2" crossterm = "^0" futures = "^0" -libc = "^0" parking_lot = "^0" percent-encoding = "^2" ratatui = "^0" regex = "^1" serde = "^1" tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs" ] } + +[target."cfg(unix)".dependencies] +libc = "^0" diff --git a/yazi-shared/src/fs/cha.rs b/yazi-shared/src/fs/cha.rs index 87f4d7c5..a28f2425 100644 --- a/yazi-shared/src/fs/cha.rs +++ b/yazi-shared/src/fs/cha.rs @@ -27,6 +27,10 @@ pub struct Cha { pub modified: Option, #[cfg(unix)] pub permissions: u32, + #[cfg(unix)] + pub uid: u32, + #[cfg(unix)] + pub gid: u32, } impl From for Cha { @@ -68,6 +72,16 @@ impl From 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() + }, } } }