mirror of
https://github.com/eth-p/bat-extras.git
synced 2024-12-13 17:50:56 +01:00
Merge pull request #9 from HarrisonMc555/search-pattern
Add --search-pattern option to tell less to search for pattern
This commit is contained in:
commit
49dfb332c1
@ -22,6 +22,7 @@ A script that combines [ripgrep](https://github.com/burntsushi/ripgrep) with bat
|
||||
| `-A` | `--after-context=[LINES]` | Display the next *n* lines after a matched line. |
|
||||
| `-B` | `--before-context=[LINES]` | Display the previous `n` lines before a matched line. |
|
||||
| `-C` | `--context=[LINES]` | A combination of `--after-context` and `--before-context`. |
|
||||
| `-p` | `--search-pattern` | Tell pager to search for `PATTERN`. Currently supported pagers: `less`. |
|
||||
| | `--no-follow` | Do not follow symlinks. |
|
||||
| | `--no-snip` | Do not show the `snip` decoration.<br /><br />This is automatically enabled when `--context=0` or when `bat --version` is less than `0.12.x`. |
|
||||
| | `--no-highlight` | Do not highlight matching lines.<br /><br />This is automatically enabled when `--context=0`. |
|
||||
@ -75,4 +76,4 @@ This script is a part of the `bat-extras` suite of scripts. You can find install
|
||||
## Issues?
|
||||
|
||||
If you find an issue or have a feature suggestion, make a pull request or issue through GitHub!
|
||||
Contributions are always welcome.
|
||||
Contributions are always welcome.
|
||||
|
53
lib/pager.sh
53
lib/pager.sh
@ -6,31 +6,13 @@
|
||||
# Issues: https://github.com/eth-p/bat-extras/issues
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Defaults.
|
||||
_SCRIPT_PAGER_NAME=
|
||||
SCRIPT_PAGER_CMD=("$PAGER")
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
# Returns 0 (true) if the current script pager is less, otherwise 1 (false)
|
||||
is_pager_less() {
|
||||
[[ "$(pager_name)" = "less" ]]
|
||||
return $?
|
||||
}
|
||||
|
||||
# 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
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Gets the pager name.
|
||||
# Gets the name of the pager command.
|
||||
pager_name() {
|
||||
if [[ -z "${SCRIPT_PAGER_CMD[0]}" ]]; then return; fi
|
||||
if [[ -z "$_SCRIPT_PAGER_NAME" ]]; then
|
||||
@ -73,3 +55,26 @@ pager_display() {
|
||||
fi
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Defaults.
|
||||
SCRIPT_PAGER_CMD=("$PAGER")
|
||||
SCRIPT_PAGER_ARGS=()
|
||||
|
||||
# Add arguments for the less pager.
|
||||
if is_pager_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
|
||||
|
||||
|
@ -30,6 +30,8 @@ OPT_CONTEXT_AFTER=2
|
||||
OPT_FOLLOW=true
|
||||
OPT_SNIP=""
|
||||
OPT_HIGHLIGHT=true
|
||||
OPT_SEARCH_PATTERN=false
|
||||
OPT_FIXED_STRINGS=false
|
||||
BAT_STYLE="header,numbers"
|
||||
|
||||
# Set options based on the bat version.
|
||||
@ -50,7 +52,8 @@ while shiftopt; do
|
||||
-C|--context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL";
|
||||
OPT_CONTEXT_AFTER="$OPT_VAL";;
|
||||
|
||||
-F|--fixed-strings|\
|
||||
-F|--fixed-strings) OPT_FIXED_STRINGS=true; RG_ARGS+=("$OPT");;
|
||||
|
||||
-U|--multiline|\
|
||||
-P|--pcre2|\
|
||||
-z|--search-zip|\
|
||||
@ -76,6 +79,8 @@ while shiftopt; do
|
||||
--no-follow) OPT_FOLLOW=false;;
|
||||
--no-snip) OPT_SNIP="";;
|
||||
--no-highlight) OPT_HIGHLIGHT=false;;
|
||||
-p|--search-pattern) OPT_SEARCH_PATTERN=true;;
|
||||
--no-search-pattern) OPT_SEARCH_PATTERN=false;;
|
||||
|
||||
# Option Forwarding
|
||||
--rg:*) {
|
||||
@ -134,6 +139,32 @@ if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
|
||||
OPT_HIGHLIGHT=false
|
||||
fi
|
||||
|
||||
# Handle the --search-pattern option.
|
||||
if "$OPT_SEARCH_PATTERN"; then
|
||||
if is_pager_less; then
|
||||
if "$OPT_FIXED_STRINGS"; then
|
||||
# This strange character is a ^R, or Control-R, character. This instructs
|
||||
# less to NOT use regular expressions, which is what the -F flag does for
|
||||
# ripgrep. If we did not use this, then less would match a different pattern
|
||||
# than ripgrep searched for. See man less(1).
|
||||
SCRIPT_PAGER_ARGS+=(-p $'\x12'"$PATTERN")
|
||||
else
|
||||
SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
|
||||
fi
|
||||
elif [[ -z "$(pager_name)" ]]; then
|
||||
print_error "$(
|
||||
echo "The -p/--search-pattern option requires a pager, but" \
|
||||
"the pager was explicitly disabled by \$BAT_PAGER or the" \
|
||||
"--paging option."
|
||||
)"
|
||||
exit 1
|
||||
else
|
||||
print_error "Unsupported pager '%s' for option -p/--search-pattern" \
|
||||
"$(pager_name)"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Main:
|
||||
# -----------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user