Update batgrep to allow snip to be disabled

This commit is contained in:
Ethan P 2019-09-18 14:20:04 -07:00
parent 7537354fdf
commit daa54ba4cf
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
2 changed files with 16 additions and 9 deletions

View File

@ -14,13 +14,14 @@ A script that combines [ripgrep](https://github.com/burntsushi/ripgrep) with bat
**Options:**
| Short | Long | Description |
| ----- | -------------------------- | ---------------------------------------------------------- |
| `-i` | `--ignore-case` | Use case insensitive searching. |
| `-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`. |
| | `--no-follow` | Do not follow symlinks. |
| Short | Long | Description |
| ----- | -------------------------- | ------------------------------------------------------------ |
| `-i` | `--ignore-case` | Use case insensitive searching. |
| `-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`. |
| | `--no-follow` | Do not follow symlinks. |
| | `--no-snip` | Do not show the `snip` decoration.<br /><br />This is disabled when `--context=0` or when `bat --version` is less than `0.12.x`. |

View File

@ -19,11 +19,12 @@ FILES=()
OPT_CONTEXT_BEFORE=2
OPT_CONTEXT_AFTER=2
OPT_FOLLOW=true
OPT_SNIP=""
BAT_STYLE="header,numbers"
# Set options based on the bat version.
if version_compare "$(bat_version)" -gt "0.12"; then
BAT_STYLE="${BAT_STYLE},snip"
OPT_SNIP=",snip"
fi
# Parse arguments.
@ -61,6 +62,7 @@ while shiftopt; do
# Script Options
--no-follow) OPT_FOLLOW=false;;
--no-snip) OPT_SNIP="";;
# ???
-*) {
@ -88,6 +90,10 @@ if "$OPT_FOLLOW"; then
RG_ARGS+=("--follow")
fi
if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then
OPT_SNIP=""
fi
# Invoke ripgrep.
FOUND_FILES=()
FOUND=0
@ -107,7 +113,7 @@ do_print() {
"$BAT" "${BAT_ARGS[@]}" \
"${LAST_LR[@]}" \
"${LAST_LH[@]}" \
--style="${BAT_STYLE}" \
--style="${BAT_STYLE}${OPT_SNIP}" \
--paging=never \
"$LAST_FILE"