mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-06-21 04:07:47 +02:00
Add experimental pager support to batgrep
This commit is contained in:
parent
c8e22890fe
commit
e71de135e6
95
lib/pager.sh
Normal file
95
lib/pager.sh
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
#!/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
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Defaults.
|
||||||
|
SCRIPT_PAGER_CMD=("$PAGER")
|
||||||
|
SCRIPT_PAGER_ARGS=()
|
||||||
|
|
||||||
|
# Add arguments for the less pager.
|
||||||
|
if [[ "$(basename "${SCRIPT_PAGER_CMD[0]}")" = "less" ]]; then
|
||||||
|
SCRIPT_PAGER_ARGS=(-R)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prefer the bat pager.
|
||||||
|
if [[ -n "${BAT_PAGER+x}" ]]; then
|
||||||
|
SCRIPT_PAGER_CMD=($BAT_PAGER)
|
||||||
|
SCRIPT_PAGER_ARGS=()
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prefer no pager if not a tty.
|
||||||
|
if ! [[ -t 1 ]]; then
|
||||||
|
SCRIPT_PAGER_CMD=()
|
||||||
|
SCRIPT_PAGER_ARGS=()
|
||||||
|
fi
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Parse arguments for the pager.
|
||||||
|
# This should be called inside the shiftopt/shiftval loop.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# while shiftopt; do
|
||||||
|
# case "$OPT" in
|
||||||
|
# # ...
|
||||||
|
# esac
|
||||||
|
# pager_shiftopt && continue
|
||||||
|
# done
|
||||||
|
pager_shiftopt() {
|
||||||
|
case "$OPT" in
|
||||||
|
# Specify paging.
|
||||||
|
--no-pager) shiftval; SCRIPT_PAGER_CMD='';;
|
||||||
|
--paging) shiftval; {
|
||||||
|
case "$OPT_VAL" in
|
||||||
|
never) SCRIPT_PAGER_CMD='';;
|
||||||
|
always) :;;
|
||||||
|
auto) :;;
|
||||||
|
esac
|
||||||
|
};;
|
||||||
|
|
||||||
|
# Specify the pager.
|
||||||
|
--pager) {
|
||||||
|
shiftval;
|
||||||
|
SCRIPT_PAGER_CMD=($OPT_VAL);
|
||||||
|
PAGER_ARGS=()
|
||||||
|
};;
|
||||||
|
|
||||||
|
*) return 1;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Executes a command or function, and pipes its output to the pager (if exists).
|
||||||
|
#
|
||||||
|
# Returns: The exit code of the command.
|
||||||
|
# Example:
|
||||||
|
# pager_exec echo hi
|
||||||
|
pager_exec() {
|
||||||
|
if [[ -n "$1" ]]; then
|
||||||
|
if [[ -n "$SCRIPT_PAGER_CMD" ]]; then
|
||||||
|
"$@" | "${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}"
|
||||||
|
return $?
|
||||||
|
else
|
||||||
|
"$@"
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Displays the output of a command or function inside the pager (if exists).
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# bat | pager_display
|
||||||
|
pager_display() {
|
||||||
|
if [[ -n "$SCRIPT_PAGER_CMD" ]]; then
|
||||||
|
"${SCRIPT_PAGER_CMD[@]}" "${SCRIPT_PAGER_ARGS[@]}"
|
||||||
|
else
|
||||||
|
cat
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@
|
|||||||
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
|
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
|
||||||
BAT="bat"
|
BAT="bat"
|
||||||
source "${LIB}/print.sh"
|
source "${LIB}/print.sh"
|
||||||
|
source "${LIB}/pager.sh"
|
||||||
source "${LIB}/opt.sh"
|
source "${LIB}/opt.sh"
|
||||||
source "${LIB}/version.sh"
|
source "${LIB}/version.sh"
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
@ -21,6 +22,7 @@ OPT_CONTEXT_AFTER=2
|
|||||||
OPT_FOLLOW=true
|
OPT_FOLLOW=true
|
||||||
OPT_SNIP=""
|
OPT_SNIP=""
|
||||||
OPT_HIGHLIGHT=true
|
OPT_HIGHLIGHT=true
|
||||||
|
OPT_COLOR=false
|
||||||
BAT_STYLE="header,numbers"
|
BAT_STYLE="header,numbers"
|
||||||
|
|
||||||
# Set options based on the bat version.
|
# Set options based on the bat version.
|
||||||
@ -28,6 +30,11 @@ if version_compare "$(bat_version)" -gt "0.12"; then
|
|||||||
OPT_SNIP=",snip"
|
OPT_SNIP=",snip"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Set options based on tty.
|
||||||
|
if [[ -t 1 ]]; then
|
||||||
|
OPT_COLOR=true
|
||||||
|
fi
|
||||||
|
|
||||||
# Parse arguments.
|
# Parse arguments.
|
||||||
while shiftopt; do
|
while shiftopt; do
|
||||||
case "$OPT" in
|
case "$OPT" in
|
||||||
@ -65,9 +72,15 @@ while shiftopt; do
|
|||||||
--no-follow) OPT_FOLLOW=false;;
|
--no-follow) OPT_FOLLOW=false;;
|
||||||
--no-snip) OPT_SNIP="";;
|
--no-snip) OPT_SNIP="";;
|
||||||
--no-highlight) OPT_HIGHLIGHT=false;;
|
--no-highlight) OPT_HIGHLIGHT=false;;
|
||||||
|
--color) OPT_COLOR=true;;
|
||||||
|
--no-color) OPT_COLOR=false;;
|
||||||
|
|
||||||
# ???
|
# ???
|
||||||
-*) {
|
-*) {
|
||||||
|
if pager_shiftopt; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2
|
printc "%{RED}%s: unknown option '%s'%{CLEAR}\n" "$PROGRAM" "$OPT" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
};;
|
};;
|
||||||
@ -88,16 +101,22 @@ if [[ -z "$PATTERN" ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Append ripgrep and bat arguments.
|
||||||
if "$OPT_FOLLOW"; then
|
if "$OPT_FOLLOW"; then
|
||||||
RG_ARGS+=("--follow")
|
RG_ARGS+=("--follow")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if "$OPT_COLOR"; then
|
||||||
|
BAT_ARGS+=("--color=always")
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
||||||
OPT_SNIP=""
|
OPT_SNIP=""
|
||||||
OPT_HIGHLIGHT=false
|
OPT_HIGHLIGHT=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Invoke ripgrep.
|
# Declare the main function.
|
||||||
|
main() {
|
||||||
FOUND_FILES=()
|
FOUND_FILES=()
|
||||||
FOUND=0
|
FOUND=0
|
||||||
FIRST_PRINT=true
|
FIRST_PRINT=true
|
||||||
@ -148,4 +167,8 @@ do_print
|
|||||||
if [[ "$FOUND" -eq 0 ]]; then
|
if [[ "$FOUND" -eq 0 ]]; then
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute main function with pager.
|
||||||
|
pager_exec main
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user