mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-07-16 04:05:03 +02:00
batman: Use self as MANPAGER
With certain implementations of `man`, the pager is called with execve and naiive argument splitting instead of going through the shell. This change uses the script as the pager itself, working around the issue.
This commit is contained in:
@ -6,7 +6,9 @@
|
|||||||
# Issues: https://github.com/eth-p/bat-extras/issues
|
# Issues: https://github.com/eth-p/bat-extras/issues
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# shellcheck disable=SC1090 disable=SC2155
|
# shellcheck disable=SC1090 disable=SC2155
|
||||||
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
|
SELF_NC="${BASH_SOURCE:-$0}"
|
||||||
|
SELF="$(cd "$(dirname "${SELF_NC}")" && cd "$(dirname "$(readlink "${SELF_NC}" || echo ".")")" && pwd)/$(basename "$(readlink "${SELF_NC}" || echo "${SELF_NC}")")"
|
||||||
|
LIB="$(cd "$(dirname "${SELF_NC}")" && cd "$(dirname "$(readlink "${SELF_NC}" || echo ".")")/../lib" && pwd)"
|
||||||
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
|
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
|
||||||
source "${LIB}/constants.sh"
|
source "${LIB}/constants.sh"
|
||||||
source "${LIB}/pager.sh"
|
source "${LIB}/pager.sh"
|
||||||
@ -18,12 +20,14 @@ source "${LIB}/opt_hook_version.sh"
|
|||||||
hook_color
|
hook_color
|
||||||
hook_version
|
hook_version
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
FORWARDED_ARGS=()
|
||||||
MAN_ARGS=()
|
MAN_ARGS=()
|
||||||
BAT_ARGS=()
|
BAT_ARGS=()
|
||||||
|
|
||||||
while shiftopt; do
|
while shiftopt; do
|
||||||
case "$OPT" in
|
case "$OPT" in
|
||||||
--paging|--pager) shiftval; BAT_ARGS+=("${OPT}=${OPT_VAL}") ;;
|
--paging|--pager) shiftval; FORWARDED_ARGS+=("${OPT}=${OPT_VAL}");
|
||||||
|
BAT_ARGS+=("${OPT}=${OPT_VAL}") ;;
|
||||||
*) MAN_ARGS+=("$OPT") ;;
|
*) MAN_ARGS+=("$OPT") ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@ -39,7 +43,27 @@ if [[ -z "${BAT_STYLE+x}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$EXECUTABLE_BAT")"' --language=man '$(printf "%q " "${BAT_ARGS[@]}")'"'
|
# When called as the manpager, do some preprocessing and feed everything to bat.
|
||||||
|
|
||||||
|
if [[ "${BATMAN_IS_BEING_MANPAGER:-}" = "yes" ]]; then
|
||||||
|
print_manpage() {
|
||||||
|
col -bx | "$EXECUTABLE_BAT" --language=man "${BAT_ARGS[@]}"
|
||||||
|
exit $?
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ "${#MAN_ARGS[@]}" -eq 1 ]]; then
|
||||||
|
# The input was passed as a file.
|
||||||
|
cat "${MAN_ARGS[0]}" | print_manpage
|
||||||
|
else
|
||||||
|
# The input was passed via stdin.
|
||||||
|
cat | print_manpage
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
export MANPAGER="env BATMAN_IS_BEING_MANPAGER=yes bash $(printf "%q " "$SELF" "${FORWARDED_ARGS[@]}")"
|
||||||
export MANROFFOPT='-c'
|
export MANROFFOPT='-c'
|
||||||
|
|
||||||
# If no argument is provided and fzf is installed, use fzf to search for man pages.
|
# If no argument is provided and fzf is installed, use fzf to search for man pages.
|
||||||
|
Reference in New Issue
Block a user