4.4. ANSI terminal color and style helpers

The ANSI_COLORS module provides helpers for wrapping strings with ANSI escape codes for colored and styled terminal output.

Color output is controlled by the use_tty_colors variable. Call init_ansi_colors to auto-detect support from command-line flags (--color / --no-color) and environment variables (TERM, NO_COLOR), or set use_tty_colors directly.

All functions and symbols are in “ansi_colors” module, use require to get access to it.

require daslib/ansi_colors

Example:

require daslib/ansi_colors

[export]
def main() {
    init_ansi_colors()
    print("{red_str("error")}: something went wrong\n")
    print("{green_str("ok")}: all good\n")
    print("{bold_str("important")}: pay attention\n")
}

4.4.1. Configuration

4.4.1.1. init_ansi_colors

init_ansi_colors()

Auto-detect color support from command-line arguments. Equivalent to init_ansi_colors(get_command_line_arguments()).

init_ansi_colors(args: array<string>)

4.4.2. Colors

blue_str(str: string): string

Wraps str with ANSI blue (\\x1B[34m) if colors are enabled.

Arguments:
  • str : string

cyan_str(str: string): string

Wraps str with ANSI cyan (\\x1B[36m) if colors are enabled.

Arguments:
  • str : string

green_str(str: string): string

Wraps str with ANSI green (\\x1B[32m) if colors are enabled.

Arguments:
  • str : string

magenta_str(str: string): string

Wraps str with ANSI magenta (\\x1B[35m) if colors are enabled.

Arguments:
  • str : string

red_str(str: string): string

Wraps str with ANSI red (\\x1B[31m) if colors are enabled.

Arguments:
  • str : string

yellow_str(str: string): string

Wraps str with ANSI yellow (\\x1B[33m) if colors are enabled.

Arguments:
  • str : string

4.4.3. Styles

bold_str(str: string): string

Wraps str with ANSI bold (\\x1B[1m) if colors are enabled.

Arguments:
  • str : string

dim_str(str: string): string

Wraps str with ANSI dim (\\x1B[2m) if colors are enabled.

Arguments:
  • str : string

reset_str(): string

Returns the ANSI reset sequence if colors are enabled, empty string otherwise.

underline_str(str: string): string

Wraps str with ANSI underline (\\x1B[4m) if colors are enabled.

Arguments:
  • str : string