diff --git a/README.md b/README.md index a5253d2..f3b3cc6 100644 --- a/README.md +++ b/README.md @@ -48,4 +48,12 @@ This uses [shfmt](https://github.com/mvdan/sh) to perform minification. **Installation:** -You can also specify `--install` and `--prefix=PATH` to have the build script automatically install the scripts for all users on the system. You may need to run the build script as root. \ No newline at end of file +You can also specify `--install` and `--prefix=PATH` to have the build script automatically install the scripts for all users on the system. You may need to run the build script as root. + + + +**Alternate Executable:** + +Depending on the distribution, bat may have been renamed to avoid package conflicts. +If you wish to use these scripts on a distribution where this is the case, there is an `--alternate-executable=NAME` option which will build the scripts to use an alternate executable name. + diff --git a/build.sh b/build.sh index ca62974..6f32160 100755 --- a/build.sh +++ b/build.sh @@ -55,7 +55,7 @@ step_read() { # Build step: preprocess # Preprocesses the script. # -# This will embed library scripts. +# This will embed library scripts and replace the BAT variable. # # Input: # The original file contents. @@ -69,6 +69,12 @@ step_preprocess() { while IFS='' read -r line; do # Skip certain lines. [[ "$line" =~ ^LIB=.*$ ]] && continue + + # Replace the BAT variable with the build option. + if [[ "$line" =~ ^BAT=.*$ ]]; then + printf "BAT=%q\n" "$OPT_BAT" + continue + fi # Embed library scripts. if [[ "$line" =~ ^[[:space:]]*source[[:space:]]+[\"\']\$\{?LIB\}/([a-z-]+\.sh)[\"\'] ]]; then @@ -169,18 +175,29 @@ pp_minify() { OPT_INSTALL=false OPT_MINIFY="lib" OPT_PREFIX="/usr/local" +OPT_BAT="bat" while shiftopt; do case "$OPT" in - --install) OPT_INSTALL=true;; - --prefix) shiftval; OPT_PREFIX="$OPT_VAL";; - --minify) shiftval; OPT_MINIFY="$OPT_VAL";; + --install) OPT_INSTALL=true;; + --prefix) shiftval; OPT_PREFIX="$OPT_VAL";; + --alternate-executable) shiftval; OPT_BAT="$OPT_VAL";; + --minify) shiftval; OPT_MINIFY="$OPT_VAL";; *) printc "%{RED}%s: unknown option '%s'%{CLEAR}" "$PROGRAM" "$OPT"; exit 1;; esac done +if [[ "$OPT_BAT" != "bat" ]]; then + printc "%{YELLOW}Building executable scripts with an alternate bat executable at %{CLEAR}%s%{YELLOW}.%{CLEAR}\n" "$OPT_BAT" 1>&2 + if ! command -v "$OPT_BAT"; then + printc "%{YELLOW}WARNING: Bash cannot execute the specified file.\n" 1>&2 + printc "%{YELLOW} The finished scripts may not run properly.%{CLEAR}\n" 1>&2 + fi + printc "\n" 1>&2 +fi + if [[ "$OPT_INSTALL" = true ]]; then printc "%{YELLOW}Installing to %{MAGENTA}%s%{YELLOW}.%{CLEAR}\n" "$OPT_PREFIX" 1>&2 else diff --git a/lib/version.sh b/lib/version.sh index 92302a0..6c49ad0 100644 --- a/lib/version.sh +++ b/lib/version.sh @@ -8,7 +8,7 @@ # Gets the current bat version. bat_version() { - bat --version | cut -d ' ' -f 2 + "$BAT" --version | cut -d ' ' -f 2 return } diff --git a/src/batgrep.sh b/src/batgrep.sh index a2ac878..72af150 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -6,6 +6,7 @@ # Issues: https://github.com/eth-p/bat-extras/issues # ----------------------------------------------------------------------------- LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib" +BAT="bat" source "${LIB}/print.sh" source "${LIB}/opt.sh" source "${LIB}/version.sh" @@ -103,12 +104,12 @@ do_print() { FIRST_PRINT=false # Print the file. - bat "${BAT_ARGS[@]}" \ - "${LAST_LR[@]}" \ - "${LAST_LH[@]}" \ - --style="${BAT_STYLE}" \ - --paging=never \ - "$LAST_FILE" + "$BAT" "${BAT_ARGS[@]}" \ + "${LAST_LR[@]}" \ + "${LAST_LH[@]}" \ + --style="${BAT_STYLE}" \ + --paging=never \ + "$LAST_FILE" # Print the separator. echo "$SEP" diff --git a/src/batman.sh b/src/batman.sh index d1b10a3..725c40b 100755 --- a/src/batman.sh +++ b/src/batman.sh @@ -6,12 +6,13 @@ # Issues: https://github.com/eth-p/bat-extras/issues # ----------------------------------------------------------------------------- LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib" +BAT="bat" source "${LIB}/print.sh" source "${LIB}/opt.sh" source "${LIB}/version.sh" # ----------------------------------------------------------------------------- -export MANPAGER='sh -c "col -bx | bat --language man --style grid"' +export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language man --style grid"' export MANROFFOPT='-c' export BAT_PAGER="$PAGER" diff --git a/src/prettybat.sh b/src/prettybat.sh index 9a4da02..7355077 100755 --- a/src/prettybat.sh +++ b/src/prettybat.sh @@ -6,6 +6,7 @@ # Issues: https://github.com/eth-p/bat-extras/issues # ----------------------------------------------------------------------------- LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib" +BAT="bat" source "${LIB}/opt.sh" source "${LIB}/str.sh" source "${LIB}/print.sh" @@ -117,10 +118,10 @@ extname() { print_file() { if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then - bat "$@" + "$BAT" "$@" return $? else - bat "${BAT_ARGS[@]}" "$@" + "$BAT" "${BAT_ARGS[@]}" "$@" return $? fi }