bat-extras/lib/print.sh

86 lines
2.1 KiB
Bash
Raw Normal View History

2019-06-19 23:15:10 +02:00
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# bat-extras | Copyright (C) 2019 eth-p | MIT License
#
# Repository: https://github.com/eth-p/bat-extras
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
# Printf, but with optional colors.
# This uses the same syntax and arguments as printf.
#
# Example:
# printc "%{RED}This is red %s.%{CLEAR}\n" "text"
#
printc() {
printf "$(sed "$_PRINTC_PATTERN" <<< "$1")" "${@:2}"
}
# Initializes the color tags for printc.
#
# Arguments:
2019-09-25 23:48:21 +02:00
# true -- Turns on color output.
# false -- Turns off color output.
2019-06-19 23:15:10 +02:00
printc_init() {
case "$1" in
2019-09-25 23:48:21 +02:00
true) _PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI";;
false) _PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN";;
2019-06-19 23:15:10 +02:00
"") {
_PRINTC_PATTERN_ANSI=""
_PRINTC_PATTERN_PLAIN=""
local name
local ansi
while read -r name ansi; do
if [[ -z "${name}" && -z "${ansi}" ]] || [[ "${name:0:1}" = "#" ]]; then
continue
fi
ansi="$(sed 's/\\/\\\\/' <<< "$ansi")"
_PRINTC_PATTERN_PLAIN="${_PRINTC_PATTERN_PLAIN}s/%{${name}}//g;"
_PRINTC_PATTERN_ANSI="${_PRINTC_PATTERN_ANSI}s/%{${name}}/${ansi}/g;"
done
if [ -t 1 ]; then
_PRINTC_PATTERN="$_PRINTC_PATTERN_ANSI"
else
_PRINTC_PATTERN="$_PRINTC_PATTERN_PLAIN"
fi
};;
esac
}
# Print a warning message to STDERR.
# Arguments:
# 1 -- The printc formatting string.
# ... -- The printc formatting arguments.
print_warning() {
printc "%{YELLOW}[%s warning]%{CLEAR}: $1%{CLEAR}\n" "$PROGRAM" "${@:2}" 1>&2
}
# Print an error message to STDERR.
# Arguments:
# 1 -- The printc formatting string.
# ... -- The printc formatting arguments.
print_error() {
printc "%{RED}[%s error]%{CLEAR}: $1%{CLEAR}\n" "$PROGRAM" "${@:2}" 1>&2
}
# -----------------------------------------------------------------------------
# Initialization:
# -----------------------------------------------------------------------------
2019-06-19 23:15:10 +02:00
printc_init <<END
CLEAR \x1B[0m
RED \x1B[31m
GREEN \x1B[32m
YELLOW \x1B[33m
BLUE \x1B[34m
MAGENTA \x1B[35m
CYAN \x1B[36m
DIM \x1B[2m
END