From 0cda5641751221961a1a27b3e23d17a1da0db038 Mon Sep 17 00:00:00 2001 From: Harrison McCullough Date: Fri, 17 Jan 2020 23:09:59 -0500 Subject: [PATCH] Make changes according to feedback Most of these are simple changes. I added an `is_pager_less` and a `get_pager` function to `pager.sh`. In order for this function to be available for use inside `pager.sh`, I had to define the functions before setting defaults. The `is_pager_less` function is then used for the `--search-pattern` to see if the `$SCRIPT_PAGER` supports the `--search-pattern` flag. Right now, the only supported pager is `less`. --- doc/batgrep.md | 1 + lib/pager.sh | 51 +++++++++++++++++++++++++++++--------------------- src/batgrep.sh | 26 ++++++++++++++----------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/doc/batgrep.md b/doc/batgrep.md index cee93ea..b1ea510 100644 --- a/doc/batgrep.md +++ b/doc/batgrep.md @@ -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` | `--less-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.

This is automatically enabled when `--context=0` or when `bat --version` is less than `0.12.x`. | | | `--no-highlight` | Do not highlight matching lines.

This is automatically enabled when `--context=0`. | diff --git a/lib/pager.sh b/lib/pager.sh index 516f467..25a9b95 100644 --- a/lib/pager.sh +++ b/lib/pager.sh @@ -6,28 +6,15 @@ # Issues: https://github.com/eth-p/bat-extras/issues # ----------------------------------------------------------------------------- -# Defaults. -SCRIPT_PAGER_CMD=("$PAGER") -SCRIPT_PAGER_ARGS=() +# Returns 0 (true) if the current script pager is less, otherwise 1 (false) +is_pager_less() { + [[ "$(get_pager)" = "less" ]] +} -# 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 - -# ----------------------------------------------------------------------------- +# Echos the name of the pager command +get_pager() { + echo "$(basename "${SCRIPT_PAGER_CMD[0]}")" +} # Executes a command or function, and pipes its output to the pager (if exists). # @@ -58,3 +45,25 @@ pager_display() { fi } +# ----------------------------------------------------------------------------- + +# Defaults. +SCRIPT_PAGER_CMD=("$PAGER") +SCRIPT_PAGER_ARGS=() + +# Add arguments for the less pager. +if is_pager_less "$(basename "${SCRIPT_PAGER_CMD[0]}")"; 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 diff --git a/src/batgrep.sh b/src/batgrep.sh index d9c91c1..2021d0c 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -79,7 +79,7 @@ while shiftopt; do --no-follow) OPT_FOLLOW=false;; --no-snip) OPT_SNIP="";; --no-highlight) OPT_HIGHLIGHT=false;; - -p|--less-search-pattern) OPT_LESS_SEARCH_PATTERN=true;; + -p|--search-pattern) OPT_SEARCH_PATTERN=true;; --no-search-pattern) OPT_SEARCH_PATTERN=false;; # Option Forwarding @@ -139,17 +139,21 @@ if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then OPT_HIGHLIGHT=false fi -if [[ "$OPT_LESS_SEARCH_PATTERN" = true ]]; then - if [[ "$OPT_FIXED_STRINGS" = true ]]; then - # Depending on your editor, this may look identical. However, there is a - # special control character at the beginning of the pattern string, right - # after the first double quote. It is a 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. - SCRIPT_PAGER_ARGS+=(-p "$PATTERN") +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 else - SCRIPT_PAGER_ARGS+=(-p "$PATTERN") + print_error "Unsupported pager '$(get_pager)' for option "\ + "-p/--less-search-pattern" + exit 1 fi fi