This commit is contained in:
2025-04-04 21:51:31 +02:00
parent 5b006ff328
commit 9f33c94020
19 changed files with 1221 additions and 88 deletions

View File

@@ -1,6 +1,26 @@
# Process Module
The Process module provides functions for executing commands and managing processes on the system.
The Process module provides functions for running commands and managing processes on your system.
## Command Results
### Command Output Information
When you run a command, you get back information about what happened:
- `stdout`: The normal output of the command
- `stderr`: Any error messages from the command
- `success`: Whether the command worked (true) or failed (false)
- `code`: The exit code (0 usually means success)
### Process Information
When you get information about a running process, you can see:
- `pid`: The process ID number
- `name`: The name of the process
- `memory`: How much memory the process is using
- `cpu`: How much CPU the process is using
## Run Functions
@@ -11,7 +31,7 @@ Runs a command or multiline script with arguments.
**Parameters:**
- `command` (string): The command to run
**Returns:** A `CommandResult` object or throws an error if the operation fails.
**Returns:** The result of the command, including output and whether it succeeded.
**Example:**
```rhai
@@ -27,28 +47,6 @@ if result.success {
```
## Types
### `CommandResult`
A type that represents the result of a command execution.
**Properties:**
- `stdout` (string): The standard output of the command
- `stderr` (string): The standard error output of the command
- `success` (boolean): Whether the command executed successfully
- `code` (integer): The exit code of the command
### `ProcessInfo`
A type that represents information about a running process.
**Properties:**
- `pid` (integer): The process ID
- `name` (string): The name of the process
- `memory` (integer): The memory usage of the process
- `cpu` (float): The CPU usage of the process
### `run_silent(command)`
@@ -57,7 +55,7 @@ Runs a command or multiline script with arguments silently (without displaying o
**Parameters:**
- `command` (string): The command to run
**Returns:** A `CommandResult` object or throws an error if the operation fails.
**Returns:** The result of the command, without displaying the output.
**Example:**
@@ -73,8 +71,6 @@ if result.code == 0 {
}
```
Note we have tools for git too, no reason to do this manual.
### `new_run_options()`
Creates a new map with default run options.
@@ -99,7 +95,7 @@ Runs a command with options specified in a map.
- `command` (string): The command to run
- `options` (map): A map of options created with `new_run_options()`
**Returns:** A `CommandResult` object or throws an error if the operation fails.
**Returns:** The result of the command with your custom settings applied.
**Example:**
```rhai
@@ -123,7 +119,7 @@ Checks if a command exists in the PATH.
**Parameters:**
- `cmd` (string): The command to check
**Returns:** The full path to the command if found, or `()` (unit/null) if not found.
**Returns:** The full path to the command if found, or nothing if not found.
**Example:**
```rhai
@@ -144,7 +140,7 @@ Kills processes matching a pattern.
**Parameters:**
- `pattern` (string): The pattern to match process names against
**Returns:** A string with the result message or throws an error if the operation fails.
**Returns:** A message confirming the processes were killed.
**Example:**
```rhai
@@ -159,7 +155,7 @@ Lists processes matching a pattern (or all processes if the pattern is empty).
**Parameters:**
- `pattern` (string): The pattern to match process names against (can be empty to list all processes)
**Returns:** An array of `ProcessInfo` objects or throws an error if the operation fails.
**Returns:** A list of processes matching your search.
**Example:**
```rhai
@@ -182,7 +178,7 @@ Gets a single process matching the pattern. Throws an error if zero or more than
**Parameters:**
- `pattern` (string): The pattern to match process names against
**Returns:** A `ProcessInfo` object or throws an error if zero or multiple processes match.
**Returns:** Information about the matching process. This will only work if exactly one process matches.
**Example:**
```rhai
@@ -192,4 +188,5 @@ try {
print(`Found process: PID=${process.pid}, Name=${process.name}`);
} catch(err) {
print(`Error: ${err}`);
}
}
```