diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2016-09-18 04:52:47 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2016-09-19 01:15:38 +0900 |
commit | 37f43fbb35b819501fe4db7844d17231789c55cd (patch) | |
tree | 3ee39365325b942d703fd024c33f3d813748f4a0 /src | |
parent | 401a5fd5ff0e509a4ea56c9dba567e17be6c494f (diff) |
Add --print0 option0.15.0
Related: #660
Diffstat (limited to 'src')
-rw-r--r-- | src/core.go | 12 | ||||
-rw-r--r-- | src/options.go | 6 | ||||
-rw-r--r-- | src/terminal.go | 12 |
3 files changed, 19 insertions, 11 deletions
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() |