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`.
This commit is contained in:
Harrison McCullough 2020-01-17 23:09:59 -05:00
parent 67f1ff9c0e
commit 0cda564175
3 changed files with 46 additions and 32 deletions

View File

@ -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. | | `-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. | | `-B` | `--before-context=[LINES]` | Display the previous `n` lines before a matched line. |
| `-C` | `--context=[LINES]` | A combination of `--after-context` and `--before-context`. | | `-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-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-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`. | | | `--no-highlight` | Do not highlight matching lines.<br /><br />This is automatically enabled when `--context=0`. |

View File

@ -6,28 +6,15 @@
# Issues: https://github.com/eth-p/bat-extras/issues # Issues: https://github.com/eth-p/bat-extras/issues
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Defaults. # Returns 0 (true) if the current script pager is less, otherwise 1 (false)
SCRIPT_PAGER_CMD=("$PAGER") is_pager_less() {
SCRIPT_PAGER_ARGS=() [[ "$(get_pager)" = "less" ]]
}
# Add arguments for the less pager. # Echos the name of the pager command
if [[ "$(basename "${SCRIPT_PAGER_CMD[0]}")" = "less" ]]; then get_pager() {
SCRIPT_PAGER_ARGS=(-R) echo "$(basename "${SCRIPT_PAGER_CMD[0]}")"
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
# -----------------------------------------------------------------------------
# Executes a command or function, and pipes its output to the pager (if exists). # Executes a command or function, and pipes its output to the pager (if exists).
# #
@ -58,3 +45,25 @@ pager_display() {
fi 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

View File

@ -79,7 +79,7 @@ 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;; -p|--search-pattern) OPT_SEARCH_PATTERN=true;;
--no-search-pattern) OPT_SEARCH_PATTERN=false;; --no-search-pattern) OPT_SEARCH_PATTERN=false;;
# Option Forwarding # Option Forwarding
@ -139,18 +139,22 @@ 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 if [[ "$OPT_SEARCH_PATTERN" ]]; then
if [[ "$OPT_FIXED_STRINGS" = true ]]; then if is_pager_less; then
# Depending on your editor, this may look identical. However, there is a if [[ "$OPT_FIXED_STRINGS" ]]; then
# special control character at the beginning of the pattern string, right # This strange character is a ^R, or Control-R, character. This instructs
# after the first double quote. It is a a ^R, or Control-R, character. This # less to NOT use regular expressions, which is what the -F flag does for
# instructs less to NOT use regular expressions, which is what the -F flag # ripgrep. If we did not use this, then less would match a different pattern
# does for ripgrep. If we did not use this, then less would match a # than ripgrep searched for. See man less(1).
# different pattern than ripgrep searched for. SCRIPT_PAGER_ARGS+=(-p $'\x12'"$PATTERN")
SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
else else
SCRIPT_PAGER_ARGS+=(-p "$PATTERN") SCRIPT_PAGER_ARGS+=(-p "$PATTERN")
fi fi
else
print_error "Unsupported pager '$(get_pager)' for option "\
"-p/--less-search-pattern"
exit 1
fi
fi fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------