From 37f43fbb35b819501fe4db7844d17231789c55cd Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 18 Sep 2016 04:52:47 +0900 Subject: Add --print0 option Related: #660 --- CHANGELOG.md | 2 ++ man/man1/fzf.1 | 6 ++++++ src/core.go | 12 ++++++------ src/options.go | 6 ++++++ src/terminal.go | 12 +++++++----- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dee72641..0a8ea8b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ------ diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index 93882e3d..13feb37c 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -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. diff --git a/src/core.go b/src/core.go index e707ad5f..f7dae1a3 100644 --- a/src/core.go +++ b/src/core.go @@ -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) diff --git a/src/options.go b/src/options.go index 2629c288..6ff71c0f 100644 --- a/src/options.go +++ b/src/options.go @@ -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": diff --git a/src/terminal.go b/src/terminal.go index 2a02ff88..c036117e 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -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() -- cgit v1.2.3