batgrep: Respect the BAT_STYLE environment variable (#54)

This commit is contained in:
Ethan P 2021-04-09 01:12:36 -07:00
parent a526c7927b
commit cae57be87b
No known key found for this signature in database
GPG Key ID: 6963FD04F6CF35EA
4 changed files with 26 additions and 3 deletions

View File

@ -38,7 +38,7 @@ OPT_SNIP=""
OPT_HIGHLIGHT=true OPT_HIGHLIGHT=true
OPT_SEARCH_PATTERN=false OPT_SEARCH_PATTERN=false
OPT_FIXED_STRINGS=false OPT_FIXED_STRINGS=false
BAT_STYLE="header,numbers" BAT_STYLE="${BAT_STYLE:-header,numbers}"
# Set options based on the bat version. # Set options based on the bat version.
if version_compare "$(bat_version)" -gt "0.12"; then if version_compare "$(bat_version)" -gt "0.12"; then
@ -246,7 +246,9 @@ main() {
[[ -z "$LAST_FILE" ]] && return 0 [[ -z "$LAST_FILE" ]] && return 0
# Print the separator. # Print the separator.
"$FIRST_PRINT" && echo "$SEP" if ! [[ "$BAT_STYLE" = *grid* ]]; then
"$FIRST_PRINT" && echo "$SEP"
fi
FIRST_PRINT=false FIRST_PRINT=false
# Print the file. # Print the file.
@ -259,7 +261,9 @@ main() {
"$LAST_FILE" "$LAST_FILE"
# Print the separator. # Print the separator.
echo "$SEP" if ! [[ "$BAT_STYLE" = *grid* ]]; then
echo "$SEP"
fi
} }
do_print_from_file_or_stdin() { do_print_from_file_or_stdin() {

View File

@ -0,0 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
cat 
dog
car 
frog
fox
────────────────────────────────────────────────────────────────────────────────

View File

@ -3,6 +3,8 @@ HAS_RIPGREP=false
setup() { setup() {
use_shim 'batgrep' use_shim 'batgrep'
unset BAT_STYLE
if command -v rg &>/dev/null; then if command -v rg &>/dev/null; then
HAS_RIPGREP=true HAS_RIPGREP=true
fi fi
@ -102,3 +104,13 @@ test:search_from_stdin() {
cat file.txt | batgrep "^ca" 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
}