Add --print0 option

Related: #660
pull/670/head 0.15.0
Junegunn Choi 8 years ago
parent 401a5fd5ff
commit 37f43fbb35
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -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
------

@ -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.

@ -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)

@ -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":

@ -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()

Loading…
Cancel
Save