batgrep: add flag to disable separators between files

This commit is contained in:
Konstantin Mikhailov 2021-08-29 19:03:09 +10:00 committed by Ethan P
parent 7803ecaba1
commit 8ad00bd978
5 changed files with 27 additions and 2 deletions

View File

@ -31,6 +31,7 @@ Search through files or directories looking for matching regular expressions (or
| | `--paging=["never"/"always"]`| Enable/disable paging. |
| | `--pager=[PAGER]` | Specify the pager to use. |
| | `--terminal-width=[COLS]` | Generate output for the specified terminal width. |
| | `--no-separator` | Disable printing separator between files |
The following options are passed directly to ripgrep, and are not handled by this script.

View File

@ -38,6 +38,7 @@ OPT_SNIP=""
OPT_HIGHLIGHT=true
OPT_SEARCH_PATTERN=false
OPT_FIXED_STRINGS=false
OPT_NO_SEPARATOR=false
BAT_STYLE="${BAT_STYLE:-header,numbers}"
# Set options based on the bat version.
@ -117,6 +118,7 @@ while shiftopt; do
--no-highlight) OPT_HIGHLIGHT=false ;;
-p | --search-pattern) OPT_SEARCH_PATTERN=true ;;
--no-search-pattern) OPT_SEARCH_PATTERN=false ;;
--no-separator) OPT_NO_SEPARATOR=true ;;
# Option forwarding
--rg:*) {
@ -214,6 +216,12 @@ main() {
LAST_LH=()
LAST_FILE=''
READ_FROM_STDIN=false
NO_SEPARATOR="$OPT_NO_SEPARATOR"
if [[ "$BAT_STYLE" = *grid* ]]; then
NO_SEPARATOR=true
fi
# If we found no files being provided and STDIN to not be attached to a tty,
# we capture STDIN to a variable. This variable will later be written to
@ -248,7 +256,7 @@ main() {
[[ -z "$LAST_FILE" ]] && return 0
# Print the separator.
if ! [[ "$BAT_STYLE" = *grid* ]]; then
if ! "$NO_SEPARATOR"; then
"$FIRST_PRINT" && echo "$SEP"
fi
FIRST_PRINT=false
@ -263,7 +271,7 @@ main() {
"$LAST_FILE"
# Print the separator.
if ! [[ "$BAT_STYLE" = *grid* ]]; then
if ! "$NO_SEPARATOR"; then
echo "$SEP"
fi
}

View File

@ -0,0 +1,6 @@
File: file.txt
 1 cat 
 2 dog
 3 car 
 4 frog
 5 fox

View File

@ -114,3 +114,13 @@ test:respects_bat_style() {
BAT_STYLE="grid" batgrep "ca" file.txt --color=always
}
test:output_without_separator() {
description "Snapshot test for output without separator"
snapshot stdout
snapshot stderr
require_rg
batgrep "ca" file.txt --no-separator --color=always
}