Add --less-search-pattern option to tell less to search for pattern

The `less` pager can search for a pattern. In addition, you can instruct it to
search for a given pattern on startup by passing in the `-p` flag followed by
the pattern to search for. Since the `batgrep` command is searching for a
pattern and invoking the pager, it has the opportunity to take advantage of this
functionality.

This patch adds a `-p` flag to `batgrep` which, in turn, passes `-p $PATTERN` to
the pager invoked. This may be specific to `less`, which is
unfortunate. However, it doesn't seem like there would be a convenient way to
make this more general. In addition, most pagers forward the actual paging to
`less` anyways.

To clarify that the behavior is specific to `less`, the long name is
`--less-search-pattern`.
This commit is contained in:
Harrison McCullough 2020-01-17 10:25:38 -05:00
parent eca97339e0
commit cf7b33443a

View File

@ -30,6 +30,7 @@ OPT_CONTEXT_AFTER=2
OPT_FOLLOW=true OPT_FOLLOW=true
OPT_SNIP="" OPT_SNIP=""
OPT_HIGHLIGHT=true OPT_HIGHLIGHT=true
OPT_SEARCH_PATTERN=false
BAT_STYLE="header,numbers" BAT_STYLE="header,numbers"
# Set options based on the bat version. # Set options based on the bat version.
@ -76,6 +77,8 @@ 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;;
-p|--less-search-pattern) OPT_LESS_SEARCH_PATTERN=true;;
--no-search-pattern) OPT_SEARCH_PATTERN=false;;
# Option Forwarding # Option Forwarding
--rg:*) { --rg:*) {
@ -134,6 +137,10 @@ if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
OPT_HIGHLIGHT=false OPT_HIGHLIGHT=false
fi fi
if [[ "$OPT_LESS_SEARCH_PATTERN" = true ]]; then
SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Main: # Main:
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------