herolib_rust/src/process/README.md
2025-04-02 08:45:47 +02:00

2.1 KiB

Process Module

====================

Overview

The process module is responsible for managing and running system processes.

Usage

To use the process module, import it in your Rust file and call the desired functions.

Functions

mgmt.rs

The mgmt.rs file contains functions for managing system processes.

run.rs

The run.rs file contains functions for running system processes.

Input Flexibility

The run function in run.rs is designed to be flexible with its input:

  1. One-liner Commands: If the input is a single line, it's treated as a command with arguments.

    run("ls -la");  // Runs the 'ls -la' command
    
  2. Multi-line Scripts: If the input contains newlines, it's treated as a script. The script is automatically dedented using the dedent function from src/text/dedent.rs before execution.

    run("    echo 'Hello'\n    ls -la");  // Common indentation is removed before execution
    

Examples

Example 1: Running a Process

To run a process, use the run function from the run.rs file:

use process::run;

fn main() {
    run("ls -l");
}

Example 2: Running a Multi-line Script

use process::run;

fn main() {
    let result = run(r#"
        echo "Hello, world!"
        ls -la
        echo "Script complete"
    "#);
}

Example 2: Managing a Process

To manage a process, use the mgmt function from the mgmt.rs file:

use process::mgmt;

fn main() {
    mgmt("start");
}

Automatic Dedentation

When a multi-line script is provided to the run function, it automatically uses the dedent function from src/text/dedent.rs to remove common leading whitespace from all lines. This allows you to write scripts with proper indentation in your code without affecting the execution.

For example, this indented script:

run(r#"
    echo "This line has 4 spaces of indentation in the source"
    echo "This line also has 4 spaces"
        echo "This line has 8 spaces (4 more than the common indentation)"
"#);

Will be executed with the common indentation (4 spaces) removed, preserving only the relative indentation between lines.