From daa54ba4cfeeb04e1280dd1f347ee231dd6fbf57 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 18 Sep 2019 14:20:04 -0700 Subject: [PATCH] Update batgrep to allow snip to be disabled --- doc/batgrep.md | 15 ++++++++------- src/batgrep.sh | 10 ++++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/doc/batgrep.md b/doc/batgrep.md index 852ba8d..ff11274 100644 --- a/doc/batgrep.md +++ b/doc/batgrep.md @@ -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.

This is disabled when `--context=0` or when `bat --version` is less than `0.12.x`. | diff --git a/src/batgrep.sh b/src/batgrep.sh index 72af150..137b02d 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -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"