batpipe: Replace 'bat_if_not_bat' with a smarter 'bat' function

This commit is contained in:
Ethan P 2021-04-04 14:13:16 -07:00
parent 94bb4b6a86
commit 82b444f93b
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA

View File

@ -31,8 +31,7 @@
# batpipe_header [pattern] [...] -- Print a viewer header line. # batpipe_header [pattern] [...] -- Print a viewer header line.
# batpipe_subheader [pattern] [...] -- Print a viewer subheader line. # batpipe_subheader [pattern] [...] -- Print a viewer subheader line.
# #
# bat -- Use `bat` for highlighting. # bat -- Use `bat` for highlighting. If running inside `bat`, does nothing.
# bat_if_not_bat [...] -- Use `bat` for highlighting (when running from `less`).
# #
# strip_trailing_slashes [path] -- Strips trailing slashes from a path. # strip_trailing_slashes [path] -- Strips trailing slashes from a path.
# #
@ -97,12 +96,10 @@ BATPIPE_INSIDE_LESS=false
BATPIPE_INSIDE_BAT=false BATPIPE_INSIDE_BAT=false
TERM_WIDTH="$(term_width)" TERM_WIDTH="$(term_width)"
bat_if_not_bat() { bat "$@"; return $?; } if [[ "$(basename -- "$(parent_executable "$(parent_executable_pid)" | cut -f1 -d' ')")" == less ]]; then
if [[ "$(basename -- "$(parent_executable "$(parent_executable_pid)"|cut -f1 -d' ')")" == less ]]; then
BATPIPE_INSIDE_LESS=true BATPIPE_INSIDE_LESS=true
elif [[ "$(basename -- "$(parent_executable|cut -f1 -d' ')")" == "$(basename -- "$EXECUTABLE_BAT")" ]]; then elif [[ "$(basename -- "$(parent_executable | cut -f1 -d' ')")" == "$(basename -- "$EXECUTABLE_BAT")" ]]; then
BATPIPE_INSIDE_BAT=true BATPIPE_INSIDE_BAT=true
bat_if_not_bat() { cat "$@"; }
fi fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -158,7 +155,7 @@ viewer_tar_supports() {
viewer_tar_process() { viewer_tar_process() {
if [[ -n "$2" ]]; then if [[ -n "$2" ]]; then
tar -xf "$1" -O "$2" | bat_if_not_bat --file-name="$1/$2" tar -xf "$1" -O "$2" | bat --file-name="$1/$2"
else else
batpipe_header "Viewing contents of archive: %{PATH}%s" "$1" batpipe_header "Viewing contents of archive: %{PATH}%s" "$1"
batpipe_subheader "To view files within the archive, add the file path after the archive." batpipe_subheader "To view files within the archive, add the file path after the archive."
@ -181,7 +178,7 @@ viewer_unzip_supports() {
viewer_unzip_process() { viewer_unzip_process() {
if [[ -n "$2" ]]; then if [[ -n "$2" ]]; then
unzip -p "$1" "$2" | bat_if_not_bat --file-name="$1/$2" unzip -p "$1" "$2" | bat --file-name="$1/$2"
else else
batpipe_header "Viewing contents of archive: %{PATH}%s" "$1" batpipe_header "Viewing contents of archive: %{PATH}%s" "$1"
batpipe_subheader "To view files within the archive, add the file path after the archive." batpipe_subheader "To view files within the archive, add the file path after the archive."
@ -248,6 +245,10 @@ batpipe_subheader() {
printc "%{SUBHEADER}==> $pattern%{C}\n" "${@:2}" printc "%{SUBHEADER}==> $pattern%{C}\n" "${@:2}"
} }
# Executes `bat` (or `cat`, if already running from within `bat`).
# Supports the `--file-name` argument if the bat version is new enough.
#
# NOTE: The `--key=value` option syntax is required for full compatibility.
bat() { bat() {
# Conditionally enable forwarding of certain arguments. # Conditionally enable forwarding of certain arguments.
if [[ -z "$__BAT_VERSION" ]]; then if [[ -z "$__BAT_VERSION" ]]; then
@ -265,17 +266,21 @@ bat() {
# Parse arguments intended for bat. # Parse arguments intended for bat.
__bat_batpipe_args=() __bat_batpipe_args=()
__bat_forward_args=() __bat_forward_args=()
__bat_files=()
setargs "$@" setargs "$@"
while shiftopt; do while shiftopt; do
case "$OPT" in case "$OPT" in
--file-name) shiftval; __bat_forward_arg_file_name "$OPT_VAL";; --file-name)
shiftval
__bat_forward_arg_file_name "$OPT_VAL"
;;
# Disallowed forwarding. # Disallowed forwarding.
--paging) shiftval;; --paging) shiftval ;;
--decorations) shiftval;; --decorations) shiftval ;;
--style) shiftval;; --style) shiftval ;;
--terminal-width) shiftval;; --terminal-width) shiftval ;;
--plain|-p|-pp|-ppp) :;; --plain | -p | -pp | -ppp) : ;;
# Forward remaining. # Forward remaining.
-*) { -*) {
@ -283,9 +288,12 @@ bat() {
if [[ -n "$OPT_VAL" ]]; then if [[ -n "$OPT_VAL" ]]; then
__bat_forward_args+=("$OPT_VAL") __bat_forward_args+=("$OPT_VAL")
fi fi
};; } ;;
*) __bat_forward_args+=("$OPT");; *) {
__bat_forward_args+=("$OPT")
__bat_files=("$OPT")
} ;;
esac esac
done done
@ -295,11 +303,17 @@ bat() {
__bat_batpipe_args+=(--terminal-width="$TERM_WIDTH") __bat_batpipe_args+=(--terminal-width="$TERM_WIDTH")
if "$BATPIPE_ENABLE_COLOR"; then if "$BATPIPE_ENABLE_COLOR"; then
__bat_batpipe_args+=(--color=always) __bat_batpipe_args+=(--color=always)
else
__bat_batpipe_args+=(--color=never)
fi fi
fi fi
if "$BATPIPE_INSIDE_BAT"; then if "$BATPIPE_INSIDE_BAT"; then
__bat_batpipe_args+=(--decorations=never --color=never) if [[ "${#__bat_files[@]}" -eq 0 ]]; then
cat
else
cat "${__bat_files[@]}"
fi
fi fi
# Execute the real bat. # Execute the real bat.
@ -386,11 +400,11 @@ for viewer in "${BATPIPE_VIEWERS[@]}"; do
done done
# No supported viewer. Just pass it through (if using bat). # No supported viewer. Just pass it through (if using bat).
if [[ "$BATPIPE_INSIDE_BAT" = true ]]; then if [[ "$BATPIPE_INSIDE_BAT" == true ]]; then
exit 1 exit 1
fi fi
# No supported viewer... highlight it using bat. # No supported viewer... highlight it using bat.
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
bat_if_not_bat "$1" bat "$1"
fi fi