mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-06-20 19:57:46 +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"
|
||||
BAT="bat"
|
||||
source "${LIB}/print.sh"
|
||||
source "${LIB}/pager.sh"
|
||||
source "${LIB}/opt.sh"
|
||||
source "${LIB}/version.sh"
|
||||
# -----------------------------------------------------------------------------
|
||||
@ -21,6 +22,7 @@ OPT_CONTEXT_AFTER=2
|
||||
OPT_FOLLOW=true
|
||||
OPT_SNIP=""
|
||||
OPT_HIGHLIGHT=true
|
||||
OPT_COLOR=false
|
||||
BAT_STYLE="header,numbers"
|
||||
|
||||
# Set options based on the bat version.
|
||||
@ -28,6 +30,11 @@ if version_compare "$(bat_version)" -gt "0.12"; then
|
||||
OPT_SNIP=",snip"
|
||||
fi
|
||||
|
||||
# Set options based on tty.
|
||||
if [[ -t 1 ]]; then
|
||||
OPT_COLOR=true
|
||||
fi
|
||||
|
||||
# Parse arguments.
|
||||
while shiftopt; do
|
||||
case "$OPT" in
|
||||
@ -65,9 +72,15 @@ while shiftopt; do
|
||||
--no-follow) OPT_FOLLOW=false;;
|
||||
--no-snip) OPT_SNIP="";;
|
||||
--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
|
||||
exit 1
|
||||
};;
|
||||
@ -88,24 +101,30 @@ if [[ -z "$PATTERN" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Append ripgrep and bat arguments.
|
||||
if "$OPT_FOLLOW"; then
|
||||
RG_ARGS+=("--follow")
|
||||
fi
|
||||
|
||||
if "$OPT_COLOR"; then
|
||||
BAT_ARGS+=("--color=always")
|
||||
fi
|
||||
|
||||
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
||||
OPT_SNIP=""
|
||||
OPT_HIGHLIGHT=false
|
||||
fi
|
||||
|
||||
# Invoke ripgrep.
|
||||
FOUND_FILES=()
|
||||
FOUND=0
|
||||
FIRST_PRINT=true
|
||||
LAST_LR=()
|
||||
LAST_LH=()
|
||||
LAST_FILE=''
|
||||
# Declare the main function.
|
||||
main() {
|
||||
FOUND_FILES=()
|
||||
FOUND=0
|
||||
FIRST_PRINT=true
|
||||
LAST_LR=()
|
||||
LAST_LH=()
|
||||
LAST_FILE=''
|
||||
|
||||
do_print() {
|
||||
do_print() {
|
||||
[[ -z "$LAST_FILE" ]] && return 0
|
||||
|
||||
# Print the separator.
|
||||
@ -122,9 +141,9 @@ do_print() {
|
||||
|
||||
# Print the separator.
|
||||
echo "$SEP"
|
||||
}
|
||||
}
|
||||
|
||||
while IFS=':' read -r file line column; do
|
||||
while IFS=':' read -r file line column; do
|
||||
((FOUND++))
|
||||
|
||||
if [[ "$LAST_FILE" != "$file" ]]; then
|
||||
@ -141,11 +160,15 @@ while IFS=':' read -r file line column; do
|
||||
|
||||
LAST_LR+=("--line-range=${line_start}:${line_end}")
|
||||
[[ "$OPT_HIGHLIGHT" = "true" ]] && LAST_LH+=("--highlight-line=${line}")
|
||||
done < <(rg --with-filename --vimgrep "${RG_ARGS[@]}" --sort path "$PATTERN" "${FILES[@]}")
|
||||
do_print
|
||||
done < <(rg --with-filename --vimgrep "${RG_ARGS[@]}" --sort path "$PATTERN" "${FILES[@]}")
|
||||
do_print
|
||||
|
||||
# Exit.
|
||||
if [[ "$FOUND" -eq 0 ]]; then
|
||||
# Exit.
|
||||
if [[ "$FOUND" -eq 0 ]]; then
|
||||
exit 2
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Execute main function with pager.
|
||||
pager_exec main
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user