Skip to content

ansi semantic.sh

Eugene Lazutkin edited this page Apr 14, 2026 · 1 revision

ansi-semantic.sh

Semantic color globals for CLI feedback: ERROR, WARN, INFO, OK. Eliminates manual composition of ANSI styles for common message types.

Bash requirement: 4.0+ Dependencies: ansi.sh (auto-loaded) Include guard: ansi::semantic::define

Usage

source "/path/to/options.bash/ansi-semantic.sh"

ansi::out "${ERROR} ERROR ${RESET_ALL} Something went wrong"
ansi::out "${WARN} WARN ${RESET_ALL} Check your configuration"
ansi::out "${INFO}Processing:${RESET_ALL} 42 files"
ansi::out "${OK}Done:${RESET_ALL} All checks passed"

Semantic globals

On source, the module defines these globals:

Global Default composition Appearance
SEMANTIC_ERROR bold bright-white on red background Error messages
SEMANTIC_WARN bold bright-white on blue background Warning messages
SEMANTIC_INFO bold cyan Informational messages
SEMANTIC_OK bold green Success messages

The SEMANTIC_-prefixed forms are always defined. Simple names (ERROR, WARN, INFO, OK) are aliases defined by default.

Suppress simple names: Set ANSI_NO_SEMANTIC_NAMES=1 before sourcing. The SEMANTIC_* globals remain available.

ANSI_NO_SEMANTIC_NAMES=1
source "/path/to/options.bash/ansi-semantic.sh"

# SEMANTIC_ERROR works, ERROR does not
ansi::out "${SEMANTIC_ERROR}Error${RESET_ALL}"

Customizing colors

Override before sourcing with ANSI_SEMANTIC_* environment variables:

ANSI_SEMANTIC_WARN="${TEXT_BOLD}${FG_BRIGHT_WHITE}${BG_YELLOW}"
source "/path/to/options.bash/ansi-semantic.sh"
# WARN is now bold bright-white on yellow

Or reassign after sourcing and re-evaluate:

source "/path/to/options.bash/ansi-semantic.sh"

SEMANTIC_OK="${TEXT_BOLD}${FG_BRIGHT_YELLOW}"
eval "$(ansi::semantic::define)"
# OK is now bold bright yellow

Functions

ansi::semantic::define [PREFIX]

Outputs eval-able shell code that defines semantic name variables. Each variable references its SEMANTIC_* counterpart.

Without arguments, defines ERROR, WARN, INFO, OK. With a prefix, defines PREFIX_ERROR, etc.

# Define with custom prefix
eval "$(ansi::semantic::define "APP_")"
ansi::out "${APP_ERROR}Error${RESET_ALL}"

Bootstrap integration

Add to your bootstrap file after ansi.sh:

. ~/.local/share/libs/scripts/ansi.sh
. ~/.local/share/libs/scripts/ansi-semantic.sh

# Now ERROR, WARN, INFO, OK are available in all scripts

Examples

Status messages

source "/path/to/options.bash/ansi-semantic.sh"

ansi::out "${OK}Build succeeded${RESET_ALL}"
ansi::err "${ERROR}Build failed${RESET_ALL}"
ansi::warn "${WARN}Deprecated option${RESET_ALL}"
ansi::out "${INFO}Retrying in 5s...${RESET_ALL}"

Label + message pattern

ansi::out "${ERROR} ERROR ${RESET_ALL} File not found: config.yaml"
ansi::out "${WARN} WARN ${RESET_ALL} Using fallback defaults"
ansi::out "${OK} DONE ${RESET_ALL} Deployed to production"

Inline status

ansi::out "Tests: ${OK}PASSED${RESET_ALL} (42/42)"
ansi::out "Tests: ${ERROR}FAILED${RESET_ALL} (3 errors)"

Clone this wiki locally