Genivia / ugrep

NEW ugrep 6.5: a more powerful, ultra fast, user-friendly, compatible grep. Includes a TUI, Google-like Boolean search with AND/OR/NOT, fuzzy search, hexdumps, searches (nested) archives (zip, 7z, tar, pax, cpio), compressed files (gz, Z, bz2, lzma, xz, lz4, zstd, brotli), pdfs, docs, and more
https://ugrep.com
BSD 3-Clause "New" or "Revised" License
2.56k stars 109 forks source link

-A / --after-context and -B / --before-context with NUM set to zero (0) does not show group separators #409

Closed AndydeCleyre closed 1 month ago

AndydeCleyre commented 1 month ago

Here's a difference from GNU grep I noticed while trying to visually separate matches -- abusing -A to force group separators on.

It looks like busybox grep doesn't show the separators in this case either, so this may not be a goal for ugrep. But reading the --help descriptions suggests that separators would be shown in this case.

$ ug --version
ugrep 6.1.0 x86_64-pc-linux-gnu +avx2; -P:pcre2jit; -z:zlib,bzip2,lzma,zstd,brotli,7z,tar/pax/cpio/zip
License: BSD-3-Clause; ugrep user manual: <https://ugrep.com>
Written by Robert van Engelen and others: <https://github.com/Genivia/ugrep>
Ugrep utilizes the RE/flex regex library: <https://github.com/Genivia/RE-flex>

$ printf '%s\n' '# one' '# two' 'two and a half' 'two and three quarters' '# three' >tmp.txt

$ grep -A 0 '^#' tmp.txt
# one
# two
--
# three

$ busybox grep -A 0 '^#' tmp.txt
# one
# two
# three

$ busybox grep -A 1 '^#' tmp.txt
# one
# two
two and a half
--
# three

$ ugrep -A 0 '^#' tmp.txt
# one
# two
# three

Minor aside: It looks like the Zsh completion does not suggest the short form -A for some reason.

genivia-inc commented 1 month ago

Zero values for -ABC turn the context option off. It's a way (the only way) to turn off when set e.g. in an alias or in a .ugrep config.

That is the same behavior as BSD grep, which does not show separators for zero context either. GNU grep may differ, but it is not standard and so there is no reason to require a separator when the context length is zero.

AndydeCleyre commented 1 month ago

OK, thanks. So:

genivia-inc commented 1 month ago

Testing zsh -A completion: it is the same as other short options. I don't see any difference. Short options don't really "complete". It's already complete except for the NUM value. As far as I can tell, zsh short option completion specifications can't indicate or suggest a NUM value. Long options do suggest that a value should be specified, but keeps it unspecified. The zsh completion specification is {-A,--after-context=-}'[Output NUM lines of trailing context after matching lines]', same as for -B, -C and similar to other options that accept arguments.

Showing group separators when there isn't any context to show is up to the grep tool to decide to support or not. It's not compatible across grep tools and should not be relied upon. BSD grep (the default macOS grep) and pcregrep/pcre2grep don't show separators for zero context.

genivia-inc commented 1 month ago

I can change this to:

{-A,--after-context=-}'[Output NUM lines of trailing context after matching lines]:NUM:( )'

so -A completion will just advance a space (to enter a number).

AndydeCleyre commented 1 month ago

I meant that if I only provide the dash and hit tab, the A is not suggested or listed. I can provide a screenshot when I get back to a computer.

AndydeCleyre commented 1 month ago

image

genivia-inc commented 1 month ago

Yes, I understood your questions that -A did not show up and does/may not complete, so I did some tests. But with the current zsh completions (not the updated version that I mentioned) everything works fine as it should on my end:

image

I'm using zsh 5.8.1 (x86_64-apple-darwin21.0).

Perhaps a bug in your zsh version?

AndydeCleyre commented 1 month ago

Ok, that's resolved, I just had a outdated completion files from an earlier git checkout of ugrep, but the current master works fine. Thanks!