bat-extras/lib/pager.sh

157 lines
3.7 KiB
Bash
Raw Normal View History

#!/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-03-31 23:51:00 +02:00
# Returns 0 (true) if the current pager is less, otherwise 1 (false).
is_pager_less() {
[[ "$(pager_name)" = "less" ]]
return $?
}
# Returns 0 (true) if the current pager is bat, otherwise 1 (false).
is_pager_bat() {
[[ "$(pager_name)" = "bat" ]]
return $?
}
2020-03-31 23:51:00 +02:00
# Returns 0 (true) if the current pager is disabled, otherwise 1 (false).
is_pager_disabled() {
[[ -z "$(pager_name)" ]]
return $?
}
2020-04-06 22:01:39 +02:00
# Prints the detected pager name.
2020-01-18 02:06:00 +01:00
pager_name() {
_detect_pager 1>&2
2020-01-18 02:06:00 +01:00
echo "$_SCRIPT_PAGER_NAME"
}
2020-04-06 22:01:39 +02:00
# Prints the detected pager version.
pager_version() {
_detect_pager 1>&2
2020-04-06 22:01:39 +02:00
echo "$_SCRIPT_PAGER_VERSION"
}
2020-03-31 23:51:00 +02:00
# Executes a command or function, and pipes its output to the pager (if it exists).
#
# Returns: The exit code of the command.
# Example:
# pager_exec echo hi
pager_exec() {
if [[ -n "$SCRIPT_PAGER_CMD" ]]; then
"$@" | pager_display
return $?
else
"$@"
return $?
fi
}
# Displays the output of a command or function inside the pager (if it exists).
#
# Example:
# bat | pager_display
pager_display() {
if [[ -n "$SCRIPT_PAGER_CMD" ]]; then
if [[ -n "$SCRIPT_PAGER_ARGS" ]]; then
"${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}"
return $?
else
"${SCRIPT_PAGER_CMD[@]}"
return $?
fi
else
cat
return $?
fi
}
# -----------------------------------------------------------------------------
2020-04-06 22:01:39 +02:00
# Detect the pager information.
# shellcheck disable=SC2120
_detect_pager() {
if [[ "$_SCRIPT_PAGER_DETECTED" = "true" ]]; then return; fi
2020-04-06 22:01:39 +02:00
_SCRIPT_PAGER_DETECTED=true
2020-04-06 22:01:39 +02:00
# If the pager command is empty, the pager is disabled.
if [[ -z "${SCRIPT_PAGER_CMD[0]}" ]]; then
_SCRIPT_PAGER_VERSION=0
_SCRIPT_PAGER_NAME=""
return;
fi
# Determine the pager name and version.
local output
local output1
output="$("${SCRIPT_PAGER_CMD[0]}" --version 2>&1)"
output1="$(head -n 1 <<<"$output")"
if [[ "$output1" =~ ^less[[:blank:]]([[:digit:]]+) ]]; then
_SCRIPT_PAGER_VERSION="${BASH_REMATCH[1]}"
_SCRIPT_PAGER_NAME="less"
elif [[ "$output1" =~ ^bat(cat)?[[:blank:]]([[:digit:]]+) ]]; then
# shellcheck disable=SC2034
__BAT_VERSION="${BASH_REMATCH[2]}"
_SCRIPT_PAGER_VERSION="${BASH_REMATCH[2]}"
_SCRIPT_PAGER_NAME="bat"
2020-04-06 22:01:39 +02:00
else
_SCRIPT_PAGER_VERSION=0
_SCRIPT_PAGER_NAME="$(basename "${SCRIPT_PAGER_CMD[0]}")"
fi
}
2020-04-06 22:01:39 +02:00
# Configure the script pager.
# This attempts to mimic how bat determines the pager and pager arguments.
#
# 1. Use BAT_PAGER
# 2. Use PAGER with special arguments for less
# 3. Use PAGER
_configure_pager() {
# shellcheck disable=SC2206
SCRIPT_PAGER_ARGS=()
if [[ -n "${PAGER+x}" ]]; then
SCRIPT_PAGER_CMD=($PAGER)
else
SCRIPT_PAGER_CMD=("less")
fi
# Prefer the BAT_PAGER environment variable.
2020-04-06 22:01:39 +02:00
if [[ -n "${BAT_PAGER+x}" ]]; then
# [note]: This is intentional.
# shellcheck disable=SC2206
SCRIPT_PAGER_CMD=($BAT_PAGER)
SCRIPT_PAGER_ARGS=()
return
fi
# If the pager is bat, use less instead.
if is_pager_bat; then
SCRIPT_PAGER_CMD=("less")
SCRIPT_PAGER_ARGS=()
fi
2020-04-06 22:01:39 +02:00
# Add arguments for the less pager.
if is_pager_less; then
SCRIPT_PAGER_CMD=("${SCRIPT_PAGER_CMD[0]}" -R --quit-if-one-screen)
2020-04-06 22:01:39 +02:00
if [[ "$(pager_version)" -lt 500 ]]; then
SCRIPT_PAGER_CMD+=(--no-init)
2020-04-06 22:01:39 +02:00
fi
fi
}
# -----------------------------------------------------------------------------
if [[ -t 1 ]]; then
# Detect and choose the arguments for the pager.
_configure_pager
else
# Prefer no pager if not a tty.
SCRIPT_PAGER_CMD=()
SCRIPT_PAGER_ARGS=()
fi