mirror of
https://github.com/eth-p/bat-extras.git
synced 2024-11-07 16:24:06 +01:00
Refactor common messages into functions
This commit is contained in:
parent
12bf16b199
commit
a472f1c651
20
lib/print.sh
20
lib/print.sh
@ -52,7 +52,25 @@ printc_init() {
|
||||
esac
|
||||
}
|
||||
|
||||
# Initialization.
|
||||
# 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:
|
||||
# -----------------------------------------------------------------------------
|
||||
printc_init <<END
|
||||
CLEAR \x1B[0m
|
||||
RED \x1B[31m
|
||||
|
@ -92,7 +92,7 @@ while shiftopt; do
|
||||
done
|
||||
|
||||
if [[ -z "$PATTERN" ]]; then
|
||||
printc "%{RED}%s: no pattern provided%{CLEAR}\n" "$PROGRAM" 1>&2
|
||||
print_error "no pattern provided"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -152,14 +152,14 @@ process_file() {
|
||||
data_raw="$(cat -)"
|
||||
data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<< "$data_raw")"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
printc "%{YELLOW}[%s warning]%{CLEAR}: 'STDIN': Unable to format with '%s'\n" "$PROGRAM" "$formatter" 1>&2
|
||||
print_warning "'STDIN': Unable to format with '%s'" "$formatter"
|
||||
print_file --language="$lang" - <<< "$data_raw"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
data_formatted="$("formatter_${formatter}_process" "$file" < "$file")"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
printc "%{YELLOW}[%s warning]%{CLEAR}: '%s': Unable to format with '%s'\n" "$PROGRAM" "$file" "$formatter" 1>&2
|
||||
print_warning "'%s': Unable to format with '%s'" "$file" "$formatter"
|
||||
print_file --language="$lang" "$file"
|
||||
return 1
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user