From 865c8b252b05dd460693b16593703e448525ca11 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Mon, 27 Apr 2020 02:08:34 -0700 Subject: [PATCH] prettybat: Add support for bat 0.14.0 --file-name option --- src/prettybat.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/prettybat.sh b/src/prettybat.sh index 65aef40..ad17250 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -12,6 +12,7 @@ source "${LIB}/opt.sh" source "${LIB}/opt_hook_version.sh" source "${LIB}/str.sh" source "${LIB}/print.sh" +source "${LIB}/version.sh" # ----------------------------------------------------------------------------- # Init: # ----------------------------------------------------------------------------- @@ -93,7 +94,7 @@ formatter_shfmt_process() { # ----------------------------------------------------------------------------- # 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. map_language_to_extension() { local ext=".txt" @@ -138,22 +139,24 @@ extname() { } print_file() { - if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then + if [[ "${#PRINT_ARGS[@]}" -eq 0 ]]; then "$EXECUTABLE_BAT" "$@" return $? else - "$EXECUTABLE_BAT" "${BAT_ARGS[@]}" "$@" + "$EXECUTABLE_BAT" "${PRINT_ARGS[@]}" "$@" return $? fi } process_file() { + PRINT_ARGS=("${BAT_ARGS[@]}") local file="$1" local ext="$2" local fext="$ext" local lang="${ext:1}" local formatter + # Determine the formatter. if [[ -n "$OPT_LANGUAGE" ]]; then lang="$OPT_LANGUAGE" fext="$(map_language_to_extension "$lang")" @@ -167,6 +170,9 @@ process_file() { return 0 fi + # Calculate additional print arguments. + forward_file_name "$file" + # Print the formatted file. if [[ "$formatter" = "none" ]]; then if [[ -z "$OPT_LANGUAGE" ]]; then @@ -205,6 +211,19 @@ process_file() { 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: # -----------------------------------------------------------------------------