Fix #248 - Premature termination of Reader on long input

This commit is contained in:
Junegunn Choi 2015-06-03 01:48:02 +09:00
parent fdbfe36c0b
commit b00bcf506e
2 changed files with 34 additions and 3 deletions

View File

@ -30,9 +30,29 @@ func (r *Reader) ReadSource() {
}
func (r *Reader) feed(src io.Reader) {
if scanner := bufio.NewScanner(src); scanner != nil {
for scanner.Scan() {
r.pusher(scanner.Text())
reader := bufio.NewReader(src)
eof := false
Loop:
for !eof {
buf := []byte{}
iter := 0 // TODO: max size?
for {
// "ReadLine either returns a non-nil line or it returns an error, never both"
line, isPrefix, err := reader.ReadLine()
eof = err == io.EOF
if eof {
break
} else if err != nil {
break Loop
}
iter++
buf = append(buf, line...)
if !isPrefix {
break
}
}
if iter > 0 {
r.pusher(string(buf))
r.eventBox.Set(EvtReadNew, nil)
}
}

View File

@ -525,6 +525,17 @@ class TestGoFZF < TestBase
tmux.send_keys 'uuu', 'TTT', 'tt', 'uu', 'ttt', 'C-j'
assert_equal %w[4 5 6 9], readonce.split($/)
end
def test_long_line
tempname = TEMPNAME + Time.now.to_f.to_s
data = '.' * 256 * 1024
File.open(tempname, 'w') do |f|
f << data
end
assert_equal data, `cat #{tempname} | #{FZF} -f .`.chomp
ensure
File.unlink tempname
end
private
def writelines path, lines
File.unlink path while File.exists? path