From cae57be87b68cb54415a0a9b5acc02d3584c0c54 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 9 Apr 2021 01:12:36 -0700 Subject: [PATCH] batgrep: Respect the BAT_STYLE environment variable (#54) --- src/batgrep.sh | 10 +++++++--- .../batgrep/test_respects_bat_style.stderr.snapshot | 0 .../batgrep/test_respects_bat_style.stdout.snapshot | 7 +++++++ test/suite/batgrep.sh | 12 ++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 test/snapshot/batgrep/test_respects_bat_style.stderr.snapshot create mode 100644 test/snapshot/batgrep/test_respects_bat_style.stdout.snapshot diff --git a/src/batgrep.sh b/src/batgrep.sh index 9df8a26..527815f 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -38,7 +38,7 @@ OPT_SNIP="" OPT_HIGHLIGHT=true OPT_SEARCH_PATTERN=false OPT_FIXED_STRINGS=false -BAT_STYLE="header,numbers" +BAT_STYLE="${BAT_STYLE:-header,numbers}" # Set options based on the bat version. if version_compare "$(bat_version)" -gt "0.12"; then @@ -246,7 +246,9 @@ main() { [[ -z "$LAST_FILE" ]] && return 0 # Print the separator. - "$FIRST_PRINT" && echo "$SEP" + if ! [[ "$BAT_STYLE" = *grid* ]]; then + "$FIRST_PRINT" && echo "$SEP" + fi FIRST_PRINT=false # Print the file. @@ -259,7 +261,9 @@ main() { "$LAST_FILE" # Print the separator. - echo "$SEP" + if ! [[ "$BAT_STYLE" = *grid* ]]; then + echo "$SEP" + fi } do_print_from_file_or_stdin() { diff --git a/test/snapshot/batgrep/test_respects_bat_style.stderr.snapshot b/test/snapshot/batgrep/test_respects_bat_style.stderr.snapshot new file mode 100644 index 0000000..e69de29 diff --git a/test/snapshot/batgrep/test_respects_bat_style.stdout.snapshot b/test/snapshot/batgrep/test_respects_bat_style.stdout.snapshot new file mode 100644 index 0000000..0463a83 --- /dev/null +++ b/test/snapshot/batgrep/test_respects_bat_style.stdout.snapshot @@ -0,0 +1,7 @@ +──────────────────────────────────────────────────────────────────────────────── +cat  +dog +car  +frog +fox +──────────────────────────────────────────────────────────────────────────────── diff --git a/test/suite/batgrep.sh b/test/suite/batgrep.sh index e5c5932..e1911a1 100644 --- a/test/suite/batgrep.sh +++ b/test/suite/batgrep.sh @@ -2,6 +2,8 @@ HAS_RIPGREP=false setup() { use_shim 'batgrep' + + unset BAT_STYLE if command -v rg &>/dev/null; then HAS_RIPGREP=true @@ -102,3 +104,13 @@ test:search_from_stdin() { cat file.txt | batgrep "^ca" } + +test:respects_bat_style() { + description "Should respect the BAT_STYLE variable." + snapshot stdout + snapshot stderr + + require_rg + + BAT_STYLE="grid" batgrep "ca" file.txt --color=always +}