Utils Module
This module provides shared, dependency-light utility functions and timing/display helpers used across the bearflare.sh pipeline.
Module Overview
- Purpose: Provide shared, dependency-light utility functions and timing/display helpers used across the
bearflare.shpipeline. - Primary modules:
src/lib/utils/core.sh: Core utilities (command checks, string helpers, Bash 3.2-compatible "maps", fatal error handler).src/lib/utils/timing.sh: Timing helpers and terminal section/timing display helpers.
Dependencies
- Standalone by design (pure shell functions).
- Optional:
log()(expected fromsrc/lib/logging/; utilities gracefully skip logging whenlog()is absent).
Core Utilities (src/lib/utils/core.sh)
have_cmd(command_name): Check whether a command exists onPATHmap_key_sanitize(key): Convert an arbitrary key to a safe variable-name fragment for Bash 3.2 "maps"map_set(map_name, key, value): Set a value in a Bash 3.2-compatible "map"map_get(map_name, key, [default]): Read a value from a Bash 3.2-compatible "map"strip_ansi(string): Remove ANSI escape codes from a stringformat_number(number): Format integers with thousands separators for readabilitytruncate_string(string, [max_len]): Truncate long strings for terminal/log displaydie(message...): Fatal error handler: prints message(s) to stderr and exits non-zero
Timing Utilities (src/lib/utils/timing.sh)
now_ns(): Get a nanosecond-resolution timestamp, with a macOS-compatible fallbackprint_timing(operation_name, elapsed_ms): Display a concise "completed in Xms" line in terminal outputprint_section(title): Print a visually distinct section header, and optionally log the same boundary
Bash 3.2 Compatibility
macOS ships with Bash 3.2 by default, which does not support associative arrays (declare -A). This codebase uses map_* functions to implement map-like storage by dynamically naming variables:
- Storage key:
${MAPNAME}__${SANITIZED_KEY} - Setting:
printf -v "$var" '%s' "$value" - Getting: indirect expansion via
${!var}with an existence check using${!var+x}
Integration Guidelines
Sourcing Order
- Source
src/lib/utils/core.sh - Source
src/lib/utils/timing.sh - Source logging module (if present) that defines
log()
Navigation
- Architecture Overview - High-level architecture
- Modular Architecture - Module hierarchy and dependencies
- Logging Module - Logging system
- Config Module - Configuration management