add support to nil-byte separated input strings, closes #121

This commit is contained in:
Giulio Iotti 2015-06-08 06:36:21 +00:00
parent 443a80f254
commit f6dd32046e
3 changed files with 22 additions and 23 deletions

View File

@ -113,7 +113,7 @@ func Run(opts *Options) {
// Reader // Reader
streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync
if !streamingFilter { if !streamingFilter {
reader := Reader{func(str string) { chunkList.Push(str) }, eventBox} reader := Reader{func(str string) { chunkList.Push(str) }, eventBox, opts.ReadZero}
go reader.ReadSource() go reader.ReadSource()
} }
@ -139,7 +139,7 @@ func Run(opts *Options) {
if pattern.MatchItem(item) { if pattern.MatchItem(item) {
fmt.Println(*item.text) fmt.Println(*item.text)
} }
}, eventBox} }, eventBox, opts.ReadZero}
reader.ReadSource() reader.ReadSource()
} else { } else {
eventBox.Unwatch(EvtReadNew) eventBox.Unwatch(EvtReadNew)

View File

@ -50,6 +50,7 @@ const usage = `usage: fzf [options]
-1, --select-1 Automatically select the only match -1, --select-1 Automatically select the only match
-0, --exit-0 Exit immediately when there's no match -0, --exit-0 Exit immediately when there's no match
-f, --filter=STR Filter mode. Do not start interactive finder. -f, --filter=STR Filter mode. Do not start interactive finder.
--null Read null-byte separated strings from input
--print-query Print query as the first line --print-query Print query as the first line
--expect=KEYS Comma-separated list of keys to complete fzf --expect=KEYS Comma-separated list of keys to complete fzf
--sync Synchronous search for multi-staged filtering --sync Synchronous search for multi-staged filtering
@ -117,6 +118,7 @@ type Options struct {
Expect []int Expect []int
Keymap map[int]actionType Keymap map[int]actionType
PrintQuery bool PrintQuery bool
ReadZero bool
Sync bool Sync bool
Version bool Version bool
} }
@ -155,6 +157,7 @@ func defaultOptions() *Options {
Expect: []int{}, Expect: []int{},
Keymap: defaultKeymap(), Keymap: defaultKeymap(),
PrintQuery: false, PrintQuery: false,
ReadZero: false,
Sync: false, Sync: false,
Version: false} Version: false}
} }
@ -525,6 +528,8 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Exit0 = true opts.Exit0 = true
case "+0", "--no-exit-0": case "+0", "--no-exit-0":
opts.Exit0 = false opts.Exit0 = false
case "--null":
opts.ReadZero = true
case "--print-query": case "--print-query":
opts.PrintQuery = true opts.PrintQuery = true
case "--no-print-query": case "--no-print-query":

View File

@ -13,6 +13,7 @@ import (
type Reader struct { type Reader struct {
pusher func(string) pusher func(string)
eventBox *util.EventBox eventBox *util.EventBox
delimNil bool
} }
// ReadSource reads data from the default command or from standard input // ReadSource reads data from the default command or from standard input
@ -30,31 +31,24 @@ func (r *Reader) ReadSource() {
} }
func (r *Reader) feed(src io.Reader) { func (r *Reader) feed(src io.Reader) {
delim := byte('\n')
if r.delimNil {
delim = '\000'
}
reader := bufio.NewReader(src) reader := bufio.NewReader(src)
eof := false for {
Loop: line, err := reader.ReadString(delim)
for !eof { if line != "" {
buf := []byte{} // "ReadString returns err != nil if and only if the returned data does not end in delim."
iter := 0 // TODO: max size? if err == nil {
for { line = line[:len(line)-1]
// "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++ r.pusher(line)
buf = append(buf, line...)
if !isPrefix {
break
}
}
if iter > 0 {
r.pusher(string(buf))
r.eventBox.Set(EvtReadNew, nil) r.eventBox.Set(EvtReadNew, nil)
} }
if err != nil {
break
}
} }
} }