-
Notifications
You must be signed in to change notification settings - Fork 0
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
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"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}"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 yellowOr 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 yellowOutputs 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}"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 scriptssource "/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}"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"ansi::out "Tests: ${OK}PASSED${RESET_ALL} (42/42)"
ansi::out "Tests: ${ERROR}FAILED${RESET_ALL} (3 errors)"