Environment

bold

pub fun bold(message: Text): Text

Returns a text as bold.

Usage

import { bold } from "std/env"

printf("%s\n", [bold("Important message")])

echo_colored

pub fun echo_colored(message: Text, color: Text | Int): Null

Prints a text with a specified color.

Usage

import { echo_colored } from "std/env"

echo_colored("Red text", "red")
echo_colored("Blue text", 34)

Supported color names

Color name Code
black 30
red 31
green 32
yellow 93
orange 33
blue 34
purple 35
cyan 36
gray 37
white 97

For all supported color codes, please visit https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit

echo_error

pub fun echo_error(message: Text, exit_code: Int = 1): Null

Prints a text as an error and exits if the status code is greater than 0.

Usage

import { echo_error } from "std/env"

echo_error("Fatal error occurred", 1)

echo_info

pub fun echo_info(message: Text): Null

Prints a text as an info message.

Usage

import { echo_info } from "std/env"

echo_info("Information message")

echo_success

pub fun echo_success(message: Text): Null

Prints a text as a success message.

Usage

import { echo_success } from "std/env"

echo_success("Operation completed successfully")

echo_warning

pub fun echo_warning(message: Text): Null

Prints a text as a warning.

Usage

import { echo_warning } from "std/env"

echo_warning("Warning: Disk space low")

env_const_set

pub fun env_const_set(name: Text, val: Text | Int | Bool): Null?

Sets a constant inside the shell session. Note that true is saved as int (1).

Usage

import { env_const_set } from "std/env"

env_const_set("API_KEY", "secret123")

env_file_load

pub fun env_file_load(file: Text = ".env"): Null

Loads the env file in the environment, using xargs.

Usage

import { env_file_load } from "std/env"

env_file_load(".env")

env_var_get

pub fun env_var_get(name: Text): Text?

Gets a variable or constant inside the shell session.

Usage

import { env_var_get } from "std/env"

const debug = env_var_get("DEBUG")

env_var_load

pub fun env_var_load(var: Text, file: Text = ".env"): Text

Retrieves the value of an environment variable, optionally sourcing it from a file if not already set.

Usage

import { env_var_load } from "std/env"

const value = env_var_load("MY_VAR", ".env.local")

env_var_set

pub fun env_var_set(name: Text, val: Text | Int | Bool): Null?

Sets a variable inside the shell session. Note that true is saved as int (1).

Usage

import { env_var_set } from "std/env"

env_var_set("STATUS", "succeeded")
env_var_set("COUNT", 100)
env_var_set("DEBUG", true) // saved as int (`1`)

env_var_test

pub fun env_var_test(name: Text): Bool

Checks if a variable inside the shell session exists.

Usage

import { env_var_test } from "std/env"

if env_var_test("PATH") {
    echo("PATH exists")
}

env_var_unset

pub fun env_var_unset(name: Text): Null?

Removes a variable inside the shell session.

Usage

import { env_var_unset } from "std/env"

env_var_unset("TEMP_VAR")

escaped

pub fun escaped(text: Text): Text

Escapes the text to be used with printf.

Usage

import { escaped } from "std/env"

printf("%s\n", [escaped("100% done\\n")])

has_failed

pub fun has_failed(command: Text): Bool

Checks if the command has failed.

Usage

import { has_failed } from "std/env"

if has_failed("test -f config.txt") {
    echo("File doesn't exist")
}

input_confirm

pub fun input_confirm(prompt: Text, default_yes: Bool = false): Bool

Creates a confirm prompt (Yes/No), and returns true if the choice is Yes.

"No" is the default choice, set default_yes to true for "Yes" as default choice.

Usage

import { input_confirm } from "std/env"

if input_confirm("Continue?", false) {
    echo("Continuing...")
}

input_hidden

pub fun input_hidden(prompt: Text): Text

Creates a prompt, hides any user input and returns the value.

Usage

import { input_hidden } from "std/env"

const password = input_hidden("Enter password: ")

input_prompt

pub fun input_prompt(prompt: Text): Text

Creates a prompt and returns the value.

Usage

import { input_prompt } from "std/env"

const name = input_prompt("Enter your name: ")

is_command

pub fun is_command(command: Text): Bool

Checks if a command exists.

Usage

import { is_command } from "std/env"

if is_command("git") {
    echo("Git is installed")
}

is_root

pub fun is_root(): Bool

Checks if the script is running with a user with root permission.

Usage

import { is_root } from "std/env"

if is_root() {
    echo("Running as root")
}

italic

pub fun italic(message: Text): Text

Returns a text as italic.

Usage

import { italic } from "std/env"

printf("%s\n", [italic("Emphasized text")])

kill

pub fun kill(process_id: Int, signal: Text = "TERM"): Null?

Sends a signal to a process by PID.

Usage

import { kill } from "std/env"

kill(1234)?                  // Send SIGTERM (default)
kill(1234, "SIGKILL")?       // Send SIGKILL
kill(1234, "9")?             // Send signal 9 (SIGKILL)

mount

pub fun mount(source: Text, target: Text, options: Text = ""): Null?

Mounts a filesystem. Requires root privileges.

Usage

import { mount } from "std/env"

mount("/dev/sda1", "/mnt/disk")?
mount("/root", "/test", "bind,ro")? // mount /root to /test directory with read-only permission

pgrep

pub fun pgrep(pattern: Text): [Int]

Finds process IDs by name pattern.

Usage

import { pgrep } from "std/env"

const pids = pgrep("nginx")
for pid in pids {
    echo(pid)
}

pgrep_exact

pub fun pgrep_exact(name: Text): [Int]

Finds process IDs by exact name.

Usage

import { pgrep_exact } from "std/env"

const pids = pgrep_exact("nginx")

pkill

pub fun pkill(pattern: Text): Null?

Kills processes by name pattern.

Usage

import { pkill } from "std/env"

pkill("nginx")?

pkill_exact

pub fun pkill_exact(name: Text): Null?

Kills processes by exact name.

Usage

import { pkill_exact } from "std/env"

pkill_exact("nginx")?

pkill_force

pub fun pkill_force(pattern: Text): Null?

Forcefully kills processes by name pattern (SIGKILL).

Usage

import { pkill_force } from "std/env"

pkill_force("nginx")?

printf

pub fun printf(format: Text, args: [Text] = []): Null

printf the text following the arguments.

Usage

import { printf } from "std/env"

printf("Hello %s!", ["World"])

shopt_disable

pub fun shopt_disable(optname: Text, set_opt: Bool = false): Null?

Disables shopt or set option.

Usage

import { shopt_disable } from "std/env"

shopt_disable("dotglob")? // Hides files starting with "." during filename expansion
shopt_disable("noglob", true)? // Enables filename expansion (globbing)

For all available options, see:

shopt_enable

pub fun shopt_enable(optname: Text, set_opt: Bool = false): Null?

Enables shopt or set option.

Usage

import { shopt_enable } from "std/env"

shopt_enable("globstar")? // Enables star (*) expansion for filenames
shopt_enable("noglob", true)? // Disables filename expansion (globbing). Note that this option doesn't properly work in a limited environment, e.g. GitHub Actions

For all available options, see:

styled

pub fun styled(message: Text, style: Int, fg: Int | Text, bg: Int | Text): Text

Prepares a text with formatting options for printf.

Usage

import { styled } from "std/env"

printf("%s\n", [styled("Error!", 1, 31, 40)])
printf("%s\n", [styled("Warning!", 1, "white", "yellow")])

Supported color names

Color name Foreground code Background code
black 30 40
red 31 41
green 32 42
yellow 33 43
orange 93 103
blue 34 44
purple 35 45
cyan 36 46
gray 37 47
white 97 107

For all supported color codes, please visit https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit

umount

pub fun umount(target: Text): Null?

Unmounts a filesystem. Requires root privileges.

Usage

import { umount } from "std/env"

umount("/mnt/disk")?

umount_force

pub fun umount_force(target: Text): Null?

Force unmounts a filesystem. Requires root privileges.

Usage

import { umount_force } from "std/env"

umount_force("/mnt/disk")?

uname_all

pub fun uname_all(): Text

Returns all system information from uname.

Usage

import { uname_all } from "std/env"

const info = uname_all()
echo(info) // e.g., "Linux my-host 5.15.0 #1 SMP x86_64 GNU/Linux"

uname_kernel_name

pub fun uname_kernel_name(): Text

Returns the kernel name (e.g., "Linux", "Darwin").

Usage

import { uname_kernel_name } from "std/env"

const kernel = uname_kernel_name()
echo(kernel) // "Linux" or "Darwin"

uname_kernel_release

pub fun uname_kernel_release(): Text

Returns the kernel release version.

Usage

import { uname_kernel_release } from "std/env"

const release = uname_kernel_release()
echo(release) // e.g., "5.15.0-generic"

uname_kernel_version

pub fun uname_kernel_version(): Text

Returns the kernel version.

Usage

import { uname_kernel_version } from "std/env"

const version = uname_kernel_version()
echo(version)

uname_machine

pub fun uname_machine(): Text

Returns the machine hardware name (architecture).

Usage

import { uname_machine } from "std/env"

const arch = uname_machine()
echo(arch) // e.g., "x86_64" or "arm64"

uname_nodename

pub fun uname_nodename(): Text

Returns the network node hostname.

Usage

import { uname_nodename } from "std/env"

const host = uname_nodename()
echo(host) // e.g., "my-computer"

uname_os

pub fun uname_os(): Text

Returns the operating system name.

Usage

import { uname_os } from "std/env"

const os = uname_os()
echo(os) // e.g., "GNU/Linux" or "Darwin"

underlined

pub fun underlined(message: Text): Text

Returns a text as underlined.

Usage

import { underlined } from "std/env"

printf("%s\n", [underlined("Underlined text")])