mirror of
https://github.com/eth-p/bat-extras.git
synced 2025-06-20 11:47:43 +02:00
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:
parent
67f1ff9c0e
commit
0cda564175
@ -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.<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`. |
|
||||
|
51
lib/pager.sh
51
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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user