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
|
||||||
|
}
|
||||||
|
|
115
src/batgrep.sh
115
src/batgrep.sh
@ -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,12 +30,17 @@ 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
|
||||||
|
|
||||||
# Ripgrep Options
|
# Ripgrep Options
|
||||||
-i|--ignore-case) RG_ARGS+=("--ignore-case");;
|
-i|--ignore-case) RG_ARGS+=("--ignore-case");;
|
||||||
-A|--after-context) shiftval; OPT_CONTEXT_AFTER="$OPT_VAL";;
|
-A|--after-context) shiftval; OPT_CONTEXT_AFTER="$OPT_VAL";;
|
||||||
-B|--before-context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";;
|
-B|--before-context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";;
|
||||||
-C|--context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";
|
-C|--context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";
|
||||||
@ -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,64 +101,74 @@ 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.
|
||||||
FOUND_FILES=()
|
main() {
|
||||||
FOUND=0
|
FOUND_FILES=()
|
||||||
FIRST_PRINT=true
|
FOUND=0
|
||||||
LAST_LR=()
|
FIRST_PRINT=true
|
||||||
LAST_LH=()
|
LAST_LR=()
|
||||||
LAST_FILE=''
|
LAST_LH=()
|
||||||
|
LAST_FILE=''
|
||||||
|
|
||||||
do_print() {
|
do_print() {
|
||||||
[[ -z "$LAST_FILE" ]] && return 0
|
[[ -z "$LAST_FILE" ]] && return 0
|
||||||
|
|
||||||
# Print the separator.
|
# Print the separator.
|
||||||
"$FIRST_PRINT" && echo "$SEP"
|
"$FIRST_PRINT" && echo "$SEP"
|
||||||
FIRST_PRINT=false
|
FIRST_PRINT=false
|
||||||
|
|
||||||
# Print the file.
|
# Print the file.
|
||||||
"$BAT" "${BAT_ARGS[@]}" \
|
"$BAT" "${BAT_ARGS[@]}" \
|
||||||
"${LAST_LR[@]}" \
|
"${LAST_LR[@]}" \
|
||||||
"${LAST_LH[@]}" \
|
"${LAST_LH[@]}" \
|
||||||
--style="${BAT_STYLE}${OPT_SNIP}" \
|
--style="${BAT_STYLE}${OPT_SNIP}" \
|
||||||
--paging=never \
|
--paging=never \
|
||||||
"$LAST_FILE"
|
"$LAST_FILE"
|
||||||
|
|
||||||
# Print the separator.
|
# Print the separator.
|
||||||
echo "$SEP"
|
echo "$SEP"
|
||||||
|
}
|
||||||
|
|
||||||
|
while IFS=':' read -r file line column; do
|
||||||
|
((FOUND++))
|
||||||
|
|
||||||
|
if [[ "$LAST_FILE" != "$file" ]]; then
|
||||||
|
do_print
|
||||||
|
LAST_FILE="$file"
|
||||||
|
LAST_LR=()
|
||||||
|
LAST_LH=()
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Calculate the context line numbers.
|
||||||
|
line_start=$((line - OPT_CONTEXT_BEFORE))
|
||||||
|
line_end=$((line + OPT_CONTEXT_AFTER))
|
||||||
|
[[ "$line_start" -gt 0 ]] || line_start=''
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# Exit.
|
||||||
|
if [[ "$FOUND" -eq 0 ]]; then
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
while IFS=':' read -r file line column; do
|
# Execute main function with pager.
|
||||||
((FOUND++))
|
pager_exec main
|
||||||
|
|
||||||
if [[ "$LAST_FILE" != "$file" ]]; then
|
|
||||||
do_print
|
|
||||||
LAST_FILE="$file"
|
|
||||||
LAST_LR=()
|
|
||||||
LAST_LH=()
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Calculate the context line numbers.
|
|
||||||
line_start=$((line - OPT_CONTEXT_BEFORE))
|
|
||||||
line_end=$((line + OPT_CONTEXT_AFTER))
|
|
||||||
[[ "$line_start" -gt 0 ]] || line_start=''
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
# Exit.
|
|
||||||
if [[ "$FOUND" -eq 0 ]]; then
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user