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 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 printc_init <<END
CLEAR \x1B[0m CLEAR \x1B[0m
RED \x1B[31m RED \x1B[31m

View File

@ -92,7 +92,7 @@ while shiftopt; do
done done
if [[ -z "$PATTERN" ]]; then if [[ -z "$PATTERN" ]]; then
printc "%{RED}%s: no pattern provided%{CLEAR}\n" "$PROGRAM" 1>&2 print_error "no pattern provided"
exit 1 exit 1
fi fi

View File

@ -152,14 +152,14 @@ process_file() {
data_raw="$(cat -)" data_raw="$(cat -)"
data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<< "$data_raw")" data_formatted="$("formatter_${formatter}_process" "$file" 2>/dev/null <<< "$data_raw")"
if [[ $? -ne 0 ]]; then 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" print_file --language="$lang" - <<< "$data_raw"
return 1 return 1
fi fi
else else
data_formatted="$("formatter_${formatter}_process" "$file" < "$file")" data_formatted="$("formatter_${formatter}_process" "$file" < "$file")"
if [[ $? -ne 0 ]]; then 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" print_file --language="$lang" "$file"
return 1 return 1
fi fi