Add --print0 option

Related: #660
This commit is contained in:
Junegunn Choi 2016-09-18 04:52:47 +09:00
parent 401a5fd5ff
commit 37f43fbb35
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
5 changed files with 27 additions and 11 deletions

View File

@ -7,6 +7,8 @@ CHANGELOG
- Added `--algo=[v1|v2]` option so one can still choose the old algorithm
which values the search performance over the quality of the result
- Advanced scoring criteria
- `--read0` to read input delimited by ASCII NUL character
- `--print0` to print output delimited by ASCII NUL character
0.13.5
------

View File

@ -285,6 +285,12 @@ with the default enter key.
e.g. \fBfzf --expect=ctrl-v,ctrl-t,alt-s,f1,f2,~,@\fR
.RE
.TP
.B "--read0"
Read input delimited by ASCII NUL character instead of newline character
.TP
.B "--print0"
Print output delimited by ASCII NUL character instead of newline character
.TP
.B "--sync"
Synchronous search for multi-staged filtering. If specified, fzf will launch
ncurses finder only after the input stream is complete.

View File

@ -151,7 +151,7 @@ func Run(opts *Options) {
// Filtering mode
if opts.Filter != nil {
if opts.PrintQuery {
fmt.Println(*opts.Filter)
opts.Printer(*opts.Filter)
}
pattern := patternBuilder([]rune(*opts.Filter))
@ -164,7 +164,7 @@ func Run(opts *Options) {
item := chunkList.trans(runes, 0)
if item != nil {
if result, _, _ := pattern.MatchItem(item, false, slab); result != nil {
fmt.Println(item.text.ToString())
opts.Printer(item.text.ToString())
found = true
}
}
@ -180,7 +180,7 @@ func Run(opts *Options) {
chunks: snapshot,
pattern: pattern})
for i := 0; i < merger.Length(); i++ {
fmt.Println(merger.Get(i).item.AsString(opts.Ansi))
opts.Printer(merger.Get(i).item.AsString(opts.Ansi))
found = true
}
}
@ -254,13 +254,13 @@ func Run(opts *Options) {
} else if val.final {
if opts.Exit0 && count == 0 || opts.Select1 && count == 1 {
if opts.PrintQuery {
fmt.Println(opts.Query)
opts.Printer(opts.Query)
}
if len(opts.Expect) > 0 {
fmt.Println()
opts.Printer("")
}
for i := 0; i < count; i++ {
fmt.Println(val.Get(i).item.AsString(opts.Ansi))
opts.Printer(val.Get(i).item.AsString(opts.Ansi))
}
if count > 0 {
os.Exit(exitOk)

View File

@ -162,6 +162,7 @@ type Options struct {
Preview previewOpts
PrintQuery bool
ReadZero bool
Printer func(string)
Sync bool
History *History
Header []string
@ -206,6 +207,7 @@ func defaultOptions() *Options {
Preview: previewOpts{"", posRight, sizeSpec{50, true}, false},
PrintQuery: false,
ReadZero: false,
Printer: func(str string) { fmt.Println(str) },
Sync: false,
History: nil,
Header: make([]string, 0),
@ -935,6 +937,10 @@ func parseOptions(opts *Options, allArgs []string) {
opts.ReadZero = true
case "--no-read0":
opts.ReadZero = false
case "--print0":
opts.Printer = func(str string) { fmt.Print(str, "\x00") }
case "--no-print0":
opts.Printer = func(str string) { fmt.Println(str) }
case "--print-query":
opts.PrintQuery = true
case "--no-print-query":

View File

@ -63,6 +63,7 @@ type Terminal struct {
reading bool
jumping jumpMode
jumpLabels string
printer func(string)
merger *Merger
selected map[int32]selectedItem
reqBox *util.EventBox
@ -269,6 +270,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
reading: true,
jumping: jumpDisabled,
jumpLabels: opts.JumpLabels,
printer: opts.Printer,
merger: EmptyMerger,
selected: make(map[int32]selectedItem),
reqBox: util.NewEventBox(),
@ -347,21 +349,21 @@ func (t *Terminal) UpdateList(merger *Merger) {
func (t *Terminal) output() bool {
if t.printQuery {
fmt.Println(string(t.input))
t.printer(string(t.input))
}
if len(t.expect) > 0 {
fmt.Println(t.pressed)
t.printer(t.pressed)
}
found := len(t.selected) > 0
if !found {
cnt := t.merger.Length()
if cnt > 0 && cnt > t.cy {
fmt.Println(t.current())
t.printer(t.current())
found = true
}
} else {
for _, sel := range t.sortSelected() {
fmt.Println(sel.text)
t.printer(sel.text)
}
}
return found
@ -1028,7 +1030,7 @@ func (t *Terminal) Loop() {
t.printPreview()
case reqPrintQuery:
C.Close()
fmt.Println(string(t.input))
t.printer(string(t.input))
exit(exitOk)
case reqQuit:
C.Close()