batman: If fzf is installed, use batman to search through man pages

This commit is contained in:
Ethan P 2021-08-21 15:19:53 -07:00
parent f280553fb1
commit 82ac5c4391
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
3 changed files with 27 additions and 0 deletions

View File

@ -4,6 +4,7 @@ Read system manual pages (`man`) using `bat` as the manual page formatter.
Gone are the days of losing your place while reading through monotone manual pages. With `bat` and `batman`, you can read `man ifconfig` with beautiful 24-bit color and syntax higlighting.
If you have `fzf` installed, you can even use `batman` to search through manual pages!
## Usage

View File

@ -13,6 +13,7 @@ EXECUTABLE_BAT="$(command -v bat 2>/dev/null || command -v batcat 2>/dev/null ||
EXECUTABLE_GIT="git"
EXECUTABLE_DELTA="delta"
EXECUTABLE_RIPGREP="rg"
EXECUTABLE_FZF="fzf"
# Constants: Program
PROGRAM="$(basename "$0" .sh)"

View File

@ -42,5 +42,30 @@ fi
export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$EXECUTABLE_BAT")"' --language=man '$(printf "%q " "${BAT_ARGS[@]}")'"'
export MANROFFOPT='-c'
# 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;' \
| sed 's/\([^(]*\)(\([0-9]\))/\2\t\1/' \
| BAT_STYLE=plain xargs batman --color=always --paging=never
" | sed 's/^\(.*\) - .*$/\1/; s/, /\n/g'
)"
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
if [[ "$line" =~ ^(.*)\(([0-9]+)\)$ ]]; then
MAN_ARGS+=("${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}")
fi
done <<< "$selected_page"
fi
# Run man.
command man "${MAN_ARGS[@]}"
exit $?