Refactor common messages into functions

This commit is contained in:
Ethan P 2019-09-25 15:59:54 -07:00
parent 12bf16b199
commit a472f1c651
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
3 changed files with 22 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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