prettybat: Add support for bat 0.14.0 --file-name option

This commit is contained in:
Ethan P 2020-04-27 02:08:34 -07:00
parent e8baf911d1
commit 865c8b252b
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA

View File

@ -12,6 +12,7 @@ source "${LIB}/opt.sh"
source "${LIB}/opt_hook_version.sh" source "${LIB}/opt_hook_version.sh"
source "${LIB}/str.sh" source "${LIB}/str.sh"
source "${LIB}/print.sh" source "${LIB}/print.sh"
source "${LIB}/version.sh"
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Init: # Init:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -93,7 +94,7 @@ formatter_shfmt_process() {
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# This function will map a bat `--language=...` argument into an appropriate # This function will map a bat `--language=...` argument into an appropriate
# file extension for the language provided. This must be hardcoded for # file extension for the language provided. This should be hardcoded for
# performance reasons. # performance reasons.
map_language_to_extension() { map_language_to_extension() {
local ext=".txt" local ext=".txt"
@ -138,22 +139,24 @@ extname() {
} }
print_file() { print_file() {
if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then if [[ "${#PRINT_ARGS[@]}" -eq 0 ]]; then
"$EXECUTABLE_BAT" "$@" "$EXECUTABLE_BAT" "$@"
return $? return $?
else else
"$EXECUTABLE_BAT" "${BAT_ARGS[@]}" "$@" "$EXECUTABLE_BAT" "${PRINT_ARGS[@]}" "$@"
return $? return $?
fi fi
} }
process_file() { process_file() {
PRINT_ARGS=("${BAT_ARGS[@]}")
local file="$1" local file="$1"
local ext="$2" local ext="$2"
local fext="$ext" local fext="$ext"
local lang="${ext:1}" local lang="${ext:1}"
local formatter local formatter
# Determine the formatter.
if [[ -n "$OPT_LANGUAGE" ]]; then if [[ -n "$OPT_LANGUAGE" ]]; then
lang="$OPT_LANGUAGE" lang="$OPT_LANGUAGE"
fext="$(map_language_to_extension "$lang")" fext="$(map_language_to_extension "$lang")"
@ -167,6 +170,9 @@ process_file() {
return 0 return 0
fi fi
# Calculate additional print arguments.
forward_file_name "$file"
# Print the formatted file. # Print the formatted file.
if [[ "$formatter" = "none" ]]; then if [[ "$formatter" = "none" ]]; then
if [[ -z "$OPT_LANGUAGE" ]]; then if [[ -z "$OPT_LANGUAGE" ]]; then
@ -205,6 +211,19 @@ process_file() {
return $? return $?
} }
# -----------------------------------------------------------------------------
# Version-Specific Features:
# -----------------------------------------------------------------------------
BAT_VERSION="$(bat_version)"
forward_file_name() { :; }
if version_compare "$BAT_VERSION" -ge "0.14"; then
forward_file_name() {
PRINT_ARGS+=("--file-name" "$1")
}
fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Main: # Main:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------