From 3e6f153bbe00529d6e4bf298b349d526df40c5ca Mon Sep 17 00:00:00 2001 From: Abdenasser Date: Wed, 30 Oct 2024 18:30:32 +0100 Subject: [PATCH] show username and id --- src-tauri/Cargo.lock | 11 ++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 48 ++++++++++++------------- src/lib/components/ToolBar.svelte | 60 +++---------------------------- src/lib/utils/index.ts | 23 +++--------- 5 files changed, 42 insertions(+), 101 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 4d34116..2e4083a 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1712,6 +1712,7 @@ dependencies = [ "tauri", "tauri-build", "tauri-plugin-shell", + "users", "windows 0.48.0", ] @@ -3796,6 +3797,16 @@ dependencies = [ "url", ] +[[package]] +name = "users" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" +dependencies = [ + "libc", + "log", +] + [[package]] name = "utf-8" version = "0.7.6" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index f080241..ee6f8a3 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -24,6 +24,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" sysinfo = "0.29.0" base64 = "0.21" +users = "0.11" [target.'cfg(target_os = "macos")'.dependencies] cocoa = "0.25" diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1c8a85d..3cc6bd6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,6 +3,7 @@ use sysinfo::{CpuExt, SystemExt, ProcessExt, System, PidExt, ProcessStatus}; use tauri::State; use std::sync::Mutex; +use users::{Users, UsersCache}; struct AppState { sys: Mutex, @@ -35,33 +36,30 @@ async fn get_processes(state: State<'_, AppState>) -> Result, S let mut sys = state.sys.lock().map_err(|_| "Failed to lock system state")?; sys.refresh_all(); + let users_cache = UsersCache::new(); + Ok(sys.processes() .iter() .map(|(pid, process)| { - let threads = if cfg!(target_os = "macos") { - use std::process::Command; - Command::new("ps") - .args(["-o", "thcount=", "-p", &pid.as_u32().to_string()]) - .output() - .ok() - .and_then(|output| { - String::from_utf8(output.stdout) - .ok() - .and_then(|s| s.trim().parse().ok()) - }) - } else { - None + let status = match process.status() { + ProcessStatus::Run => "Running", + ProcessStatus::Sleep => { + if process.cpu_usage() < 0.1 { + "Idle" + } else { + "Sleeping" + } + }, + _ => "Unknown" }; - let status = match process.status() { - ProcessStatus::Run => "R", // Running - ProcessStatus::Sleep => "S", // Sleeping - ProcessStatus::Idle => "I", // Idle - ProcessStatus::Zombie => "Z", // Zombie - ProcessStatus::Stop => "T", // Stopped - ProcessStatus::Dead => "X", // Dead - _ => "Unknown", - }; + let user = process.user_id() + .and_then(|uid| { + let uid_num = uid.to_string().parse::().unwrap_or(0); + users_cache.get_user_by_uid(uid_num) + .map(|user| format!("{} ({})", user.name().to_string_lossy(), uid_num)) + }) + .unwrap_or_else(|| "-".to_string()); ProcessInfo { pid: pid.as_u32(), @@ -70,11 +68,9 @@ async fn get_processes(state: State<'_, AppState>) -> Result, S cpu_usage: process.cpu_usage(), memory_usage: process.memory(), status: status.to_string(), - user: process.user_id() - .map(|uid| uid.to_string()) - .unwrap_or_else(|| "-".to_string()), + user, command: process.cmd().join(" "), - threads, + threads: None, } }) .collect()) diff --git a/src/lib/components/ToolBar.svelte b/src/lib/components/ToolBar.svelte index 2ccb2df..1895e9c 100644 --- a/src/lib/components/ToolBar.svelte +++ b/src/lib/components/ToolBar.svelte @@ -22,13 +22,10 @@ const statusOptions = [ { value: "all", label: "All Statuses" }, - { value: "R", label: "🏃 Running" }, - { value: "S", label: "😴 Sleeping" }, - { value: "I", label: "⌛ Idle" }, - { value: "Z", label: "🧟 Zombie" }, - { value: "T", label: "⛔ Stopped" }, - { value: "X", label: "💀 Dead" }, - { value: "Unknown", label: "🤔 Unknown" }, + { value: "Running", label: "🏃 Running" }, + { value: "Sleeping", label: "😴 Sleeping" }, + { value: "Idle", label: "⌛ Idle" }, + { value: "Unknown", label: "🫥 Unknown" }, ]; function changePage(page: number) { @@ -60,7 +57,6 @@
- Status: