2019-09-08 00:19:12 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# bat-extras | Copyright (C) 2019 eth-p | MIT License
|
|
|
|
#
|
|
|
|
# Repository: https://github.com/eth-p/bat-extras
|
|
|
|
# Issues: https://github.com/eth-p/bat-extras/issues
|
|
|
|
# -----------------------------------------------------------------------------
|
2020-04-01 21:49:55 +02:00
|
|
|
# shellcheck disable=SC1090 disable=SC2155
|
2020-01-21 23:09:43 +01:00
|
|
|
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")/../lib" && pwd)"
|
2020-02-16 00:10:18 +01:00
|
|
|
if [[ -n "${MANPAGER}" ]]; then BAT_PAGER="$MANPAGER"; fi
|
2020-04-08 11:56:17 +02:00
|
|
|
source "${LIB}/constants.sh"
|
2019-12-24 01:35:28 +01:00
|
|
|
source "${LIB}/pager.sh"
|
2019-09-08 00:19:12 +02:00
|
|
|
source "${LIB}/print.sh"
|
|
|
|
source "${LIB}/opt.sh"
|
2020-05-01 07:11:36 +02:00
|
|
|
source "${LIB}/opt_hook_color.sh"
|
|
|
|
source "${LIB}/opt_hook_version.sh"
|
2019-09-08 00:19:12 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
2019-10-23 00:35:10 +02:00
|
|
|
hook_color
|
2020-04-08 11:56:17 +02:00
|
|
|
hook_version
|
2019-10-23 00:35:10 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
MAN_ARGS=()
|
|
|
|
BAT_ARGS=()
|
2019-09-08 00:19:12 +02:00
|
|
|
|
2020-05-01 05:20:56 +02:00
|
|
|
while shiftopt; do
|
|
|
|
case "$OPT" in
|
|
|
|
--paging|--pager) shiftval; BAT_ARGS+=("${OPT}=${OPT_VAL}") ;;
|
|
|
|
*) MAN_ARGS+=("$OPT") ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-10-23 00:35:10 +02:00
|
|
|
if "$OPT_COLOR"; then
|
2020-05-01 05:20:56 +02:00
|
|
|
BAT_ARGS+=("--color=always" "--decorations=always")
|
2019-10-23 00:35:10 +02:00
|
|
|
else
|
2020-05-01 05:20:56 +02:00
|
|
|
BAT_ARGS+=("--color=never" "--decorations=never")
|
2019-10-23 00:35:10 +02:00
|
|
|
fi
|
2020-05-01 05:20:56 +02:00
|
|
|
|
2021-08-22 00:19:14 +02:00
|
|
|
if [[ -z "${BAT_STYLE+x}" ]]; then
|
|
|
|
export BAT_STYLE="grid"
|
|
|
|
fi
|
|
|
|
|
2019-10-23 00:35:10 +02:00
|
|
|
# -----------------------------------------------------------------------------
|
2021-08-22 00:19:14 +02:00
|
|
|
export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$EXECUTABLE_BAT")"' --language=man '$(printf "%q " "${BAT_ARGS[@]}")'"'
|
2019-09-08 00:19:12 +02:00
|
|
|
export MANROFFOPT='-c'
|
|
|
|
|
2021-08-22 00:19:53 +02:00
|
|
|
# If no argument is provided and fzf is installed, use fzf to search for man pages.
|
|
|
|
if [[ "${#MAN_ARGS[@]}" -eq 0 ]] && [[ -z "$BATMAN_LEVEL" ]] && command -v "$EXECUTABLE_FZF" &>/dev/null; then
|
|
|
|
export BATMAN_LEVEL=1
|
|
|
|
|
|
|
|
selected_page="$(man -k . | "$EXECUTABLE_FZF" --delimiter=" - " --reverse -e --preview="
|
|
|
|
echo {1} \
|
|
|
|
| sed 's/, /\n/g;' \
|
2023-03-21 09:36:00 +01:00
|
|
|
| sed 's/\([^(]*\)(\([0-9A-Za-z]\))/\2\t\1/g' \
|
|
|
|
| BAT_STYLE=plain xargs -n2 batman --color=always --paging=never
|
|
|
|
")"
|
2021-08-22 00:19:53 +02:00
|
|
|
|
|
|
|
if [[ -z "$selected_page" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Convert the page(section) format to something that can be fed to the man command.
|
|
|
|
while read -r line; do
|
2023-03-21 09:26:11 +01:00
|
|
|
if [[ "$line" =~ ^(.*)\(([0-9a-zA-Z]+)\) ]]; then
|
2021-09-17 21:26:12 +02:00
|
|
|
MAN_ARGS+=("${BASH_REMATCH[2]}" "$(echo ${BASH_REMATCH[1]} | xargs)")
|
2021-08-22 00:19:53 +02:00
|
|
|
fi
|
|
|
|
done <<< "$selected_page"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run man.
|
2019-10-23 00:35:10 +02:00
|
|
|
command man "${MAN_ARGS[@]}"
|
2019-09-08 00:19:12 +02:00
|
|
|
exit $?
|