New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context line printing wrong when reading from a pipe #347

Closed
jjhart opened this Issue Mar 5, 2014 · 7 comments

Comments

Projects
None yet
7 participants
@jjhart
Copy link

jjhart commented Mar 5, 2014

Tested with both ag 0.14 and ag 0.19.2 on OS X Mountain Lion & Mavericks

It's a somewhat understandable behavior, but other related tools (grep) do this correctly.

# test setup
$ for f in a b c; do echo $f; done > test.txt
$ cat test.txt
a
b
c

# correct:
$ ag -B1 b test.txt
1-a
2:b

# correct:
$ ag -C1 b test.txt
1-a
2:b
3-c


# bug in -B handling:
$ cat test.txt | ag -B1 b
b

# bug in -C handling:
$ cat test.txt | ag -C1 b
b
:

(yes, that's a weird ":" printed at the end)

@mlitbk

This comment has been minimized.

Copy link

mlitbk commented Mar 5, 2014

This issue is related to #343

@jjhart

This comment has been minimized.

Copy link

jjhart commented Mar 5, 2014

Yes, it's related & I imagine your proposal on #343 would fix this issue too. But the proposal (reading the entire stream into a buffer & then searching that as if it were a file) is overkill for both situations, with serious performance/runtime implications when searching a large stream. Issue #343 could be fixed with a simple counter, and my issue could be fixed by buffering only the requested number of previous input lines rather than the entire stream. Multi-line regexes are still a problem, though, but that's a universal truth.

(note I haven't read the ag code, so I don't know the complexity involved with these "simple fixes". But, naively, both seem like fairly minor tweaks to the state being carted around by the line-matching code.)

@joachim-n

This comment has been minimized.

Copy link

joachim-n commented May 16, 2015

This is still an issue in the latest version, 0.29.1.

@grekko

This comment has been minimized.

Copy link

grekko commented Aug 18, 2015

I can reproduce this too w/ version 0.30.0. Installed via homebrew from: https://homebrew.bintray.com/bottles/the_silver_searcher-0.30.0.yosemite.bottle.tar.gz

$ echo "A\nB\nC" > test.txt
$ cat test.txt
A
B
C

Using grep (works as expected):

$ cat test.txt|grep -C1 B
A
B
C

Using ag (0.30.0, does not work):

$ cat test.txt|ag -C1 B
B

Any updates on this?

@DXist

This comment has been minimized.

Copy link

DXist commented Nov 20, 2015

+1. Warning about unsupported before/after flags for stdin stream will work for me.

@cemeyer

This comment has been minimized.

Copy link
Contributor

cemeyer commented May 7, 2016

@jjhart Reading the entire stream into a buffer and then searching that as a file is exactly what is done for zipped files, for example. Gzip/bzip/xz files are essentially streams. I've created issue #896 to track this improvement.

@premek

This comment has been minimized.

Copy link

premek commented Jul 28, 2016

+1 Related: #869

@ggreer ggreer closed this in 08b7b0a Nov 25, 2016

ggreer added a commit that referenced this issue Nov 25, 2016

Merge pull request #895 from cemeyer/347-context
Fix #347 - Keep enough context around to print in stream mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment