Initial commit
This commit is contained in:
74
lib/format.sh
Normal file
74
lib/format.sh
Normal file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bash
|
||||
# Formatting utilities
|
||||
# Requires: lib/common.sh must be sourced first (provides color variables)
|
||||
|
||||
# Guard: ensure color variables are defined (sourced from common.sh)
|
||||
: "${GREEN:=}" "${RED:=}" "${YELLOW:=}" "${CYAN:=}" "${BOLD:=}" "${DIM:=}" "${RESET:=}"
|
||||
|
||||
human_bytes() {
|
||||
local bytes="${1:-0}"
|
||||
if (( bytes >= 1073741824 )); then
|
||||
local val
|
||||
val="$(echo "scale=1; $bytes / 1073741824" | bc)"
|
||||
printf "%s GiB" "${val/#./0.}"
|
||||
elif (( bytes >= 1048576 )); then
|
||||
printf "%d MiB" "$(( bytes / 1048576 ))"
|
||||
elif (( bytes >= 1024 )); then
|
||||
printf "%d KiB" "$(( bytes / 1024 ))"
|
||||
else
|
||||
printf "%d B" "$bytes"
|
||||
fi
|
||||
}
|
||||
|
||||
human_mib() {
|
||||
local mib="${1:-0}"
|
||||
if (( mib >= 1024 )); then
|
||||
local val
|
||||
val="$(echo "scale=1; $mib / 1024" | bc)"
|
||||
printf "%s GiB" "${val/#./0.}"
|
||||
else
|
||||
printf "%d MiB" "$mib"
|
||||
fi
|
||||
}
|
||||
|
||||
# Status indicators
|
||||
STATUS_PASS="${GREEN}[OK]${RESET}"
|
||||
STATUS_FAIL="${RED}[!!]${RESET}"
|
||||
STATUS_WARN="${YELLOW}[??]${RESET}"
|
||||
STATUS_INFO="${CYAN}[--]${RESET}"
|
||||
|
||||
print_status() {
|
||||
# Usage: print_status pass|fail|warn|info "label" "detail"
|
||||
local kind="$1" label="$2" detail="${3:-}"
|
||||
local indicator
|
||||
case "$kind" in
|
||||
pass) indicator="$STATUS_PASS" ;;
|
||||
fail) indicator="$STATUS_FAIL" ;;
|
||||
warn) indicator="$STATUS_WARN" ;;
|
||||
*) indicator="$STATUS_INFO" ;;
|
||||
esac
|
||||
printf " %b %-30s %s\n" "$indicator" "$label" "$detail"
|
||||
}
|
||||
|
||||
print_kv() {
|
||||
local key="$1" value="$2"
|
||||
printf " %b%-24s%b %s\n" "$DIM" "$key:" "$RESET" "$value"
|
||||
}
|
||||
|
||||
print_divider() {
|
||||
printf "%b%s%b\n" "$DIM" "$(printf '%.0s─' {1..60})" "$RESET"
|
||||
}
|
||||
|
||||
# Table helpers — format strings are caller-controlled constants, not user input
|
||||
print_table_header() {
|
||||
local fmt="$1"; shift
|
||||
# shellcheck disable=SC2059 — format string is a trusted constant from callers
|
||||
printf "${BOLD}${fmt}${RESET}\n" "$@"
|
||||
print_divider
|
||||
}
|
||||
|
||||
print_table_row() {
|
||||
local fmt="$1"; shift
|
||||
# shellcheck disable=SC2059 — format string is a trusted constant from callers
|
||||
printf "${fmt}\n" "$@"
|
||||
}
|
||||
Reference in New Issue
Block a user