Skip to content

Commit

Permalink
feat: allow opening process executable directory
Browse files Browse the repository at this point in the history
fix build
  • Loading branch information
ArnoChenFx committed Jan 27, 2025
1 parent 511e0da commit c7d8726
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- 🔍 Advanced process search and filtering
- 📌 Pin important processes
- 🛠 Process management (kill processes)
- 📂 Open the directory of the process
- 🎯 Sort by any column
- 🔄 Auto-refresh system stats

Expand Down
21 changes: 21 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ pub async fn kill_process(pid: u32, state: State<'_, AppState>) -> Result<bool,
let sys = state.sys.lock().map_err(|e| e.to_string())?;
Ok(ProcessMonitor::kill_process(&sys, pid))
}

/// Opens the directory of the process executable in the file explorer
///
/// # Arguments
///
/// * `pid` - Process ID
/// * `state` - The application state
///
/// # Returns
///
/// * `true` if the directory was successfully opened
/// * `false` otherwise
///
/// # Errors
///
/// Returns an error string if failed to acquire lock on system state
#[tauri::command]
pub async fn open_process_directory(pid: u32, state: State<'_, AppState>) -> Result<bool, String> {
let sys = state.sys.lock().map_err(|e| e.to_string())?;
Ok(ProcessMonitor::open_process_directory(&sys, pid))
}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() {
.invoke_handler(tauri::generate_handler![
commands::get_processes,
commands::kill_process,
commands::open_process_directory,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
37 changes: 37 additions & 0 deletions src-tauri/src/monitoring/process_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,43 @@ impl ProcessMonitor {
.unwrap_or(false)
}

/// Opens the directory of the process executable
///
/// # Arguments
///
/// * `sys` - System information provider
/// * `pid` - Process ID
///
/// # Returns
///
/// Boolean indicating whether the directory was successfully opened
pub fn open_process_directory(sys: &sysinfo::System, pid: u32) -> bool {
sys.process(sysinfo::Pid::from(pid as usize))
.map(|process| {
let process_path = process.exe();
if std::fs::metadata(&process_path).is_ok() {
let dir = process_path
.parent()
.unwrap_or_else(|| std::path::Path::new("."));

#[cfg(target_os = "windows")]
let cmd = "explorer";

#[cfg(target_os = "macos")]
let cmd = "open";

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
let cmd = "xdg-open";

if std::process::Command::new(cmd).arg(dir).spawn().is_ok() {
return true;
}
}
false
})
.unwrap_or(false)
}

/// Gets the current system time in seconds since UNIX epoch
fn get_current_time() -> Result<u64, String> {
SystemTime::now()
Expand Down
17 changes: 17 additions & 0 deletions src/lib/components/process/ActionButtons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
faThumbtack,
faInfoCircle,
faXmark,
faFolderOpen,
} from "@fortawesome/free-solid-svg-icons";
import Fa from "svelte-fa";
import type { Process } from "$lib/types";
export let process: Process;
export let isPinned: boolean;
export let onTogglePin: (command: string) => void;
export let openProcessDirectory: (process: Process) => void;
export let onShowDetails: (process: Process) => void;
export let onKillProcess: (process: Process) => void;
</script>
Expand All @@ -24,6 +26,13 @@
>
<Fa icon={faThumbtack} />
</button>
<button
class="btn-action open-btn"
on:click={() => openProcessDirectory(process)}
title="Open Directory"
>
<Fa icon={faFolderOpen} />
</button>
<button
class="btn-action info-btn"
on:click={() => onShowDetails(process)}
Expand Down Expand Up @@ -109,6 +118,14 @@
opacity: 0.15;
}
.open-btn {
color: var(--green);
}
.open-btn::before {
background: var(--green);
}
.info-btn {
color: var(--lavender);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/process/ProcessRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
export let onTogglePin: (command: string) => void;
export let onShowDetails: (process: Process) => void;
export let onKillProcess: (process: Process) => void;
export let openProcessDirectory: (process: Process) => void;
</script>

<tr class:high-usage={isHighUsage} class:pinned={isPinned}>
Expand All @@ -31,6 +32,7 @@
{process}
{isPinned}
{onTogglePin}
{openProcessDirectory}
{onShowDetails}
{onKillProcess}
/>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/process/ProcessTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
export let onToggleSort: (field: keyof Process) => void;
export let onTogglePin: (command: string) => void;
export let openProcessDirectory: (process: Process) => void;
export let onShowDetails: (process: Process) => void;
export let onKillProcess: (process: Process) => void;
</script>
Expand All @@ -26,6 +27,7 @@
isHighUsage={process.cpu_usage > 50 ||
process.memory_usage / (systemStats?.memory_total || 0) > 0.1}
{onTogglePin}
{openProcessDirectory}
{onShowDetails}
{onKillProcess}
/>
Expand Down
17 changes: 17 additions & 0 deletions src/lib/stores/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ function createProcessStore() {
}
};

const openProcessDirectory = async (process: Process) => {
try {
const success = await invoke<boolean>("open_process_directory", {
pid: process.pid,
});
if (!success) {
throw new Error("Failed to open process directory");
}
} catch (e: unknown) {
update((state) => ({
...state,
error: e instanceof Error ? e.message : String(e),
}));
}
};

const toggleSort = (field: keyof Process) => {
update((state) => ({
...state,
Expand Down Expand Up @@ -202,6 +218,7 @@ function createProcessStore() {
setIsLoading,
getProcesses,
killProcess,
openProcessDirectory,
toggleSort,
togglePin,
setSearchTerm,
Expand Down
1 change: 1 addition & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
{pinnedProcesses}
onToggleSort={processStore.toggleSort}
onTogglePin={processStore.togglePin}
openProcessDirectory={processStore.openProcessDirectory}
onShowDetails={processStore.showProcessDetails}
onKillProcess={processStore.confirmKillProcess}
/>
Expand Down

0 comments on commit c7d8726

Please sign in to comment.