From 8c435afe89d4cfa08d75e18211229be74337ac2e Mon Sep 17 00:00:00 2001 From: Daisuke Maki Date: Mon, 20 Jun 2016 09:53:26 -0400 Subject: [PATCH] create ReturnsLineID switch --- filter.go | 42 +++++++++++++++++++++++++++--------------- interface.go | 14 ++++++++++---- peco.go | 2 +- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/filter.go b/filter.go index a4d8046..3dbdcb2 100644 --- a/filter.go +++ b/filter.go @@ -374,7 +374,7 @@ func NewSmartCaseFilter() *RegexpFilter { return rf } -func NewExternalCmdFilter(name string, cmd string, args []string, threshold int, enableSep bool) *ExternalCmdFilter { +func NewExternalCmdFilter(name string, cmd string, args []string, threshold int, enableSep bool, returnsLineID bool) *ExternalCmdFilter { if len(args) == 0 { args = []string{"$QUERY"} } @@ -389,6 +389,7 @@ func NewExternalCmdFilter(name string, cmd string, args []string, threshold int, enableSep: enableSep, name: name, outCh: pipeline.OutputChannel(make(chan interface{})), + returnsLineID: returnsLineID, thresholdBufsiz: threshold, } } @@ -400,6 +401,7 @@ func (ecf ExternalCmdFilter) Clone() LineFilter { enableSep: ecf.enableSep, name: ecf.name, outCh: pipeline.OutputChannel(make(chan interface{})), + returnsLineID: ecf.returnsLineID, thresholdBufsiz: ecf.thresholdBufsiz, } } @@ -492,11 +494,12 @@ func (ecf *ExternalCmdFilter) launchExternalCmd(ctx context.Context, buf []Line) inbuf := &bytes.Buffer{} for _, l := range buf { inbuf.WriteString(l.DisplayString()) - inbuf.WriteByte(0) - inbuf.WriteString(strconv.FormatUint(l.ID(), 10)) + if ecf.returnsLineID { + inbuf.WriteByte(0) + inbuf.WriteString(strconv.FormatUint(l.ID(), 10)) + lines[l.ID()] = l + } inbuf.WriteByte('\n') - - lines[l.ID()] = l } cmd.Stdin = inbuf @@ -519,18 +522,27 @@ func (ecf *ExternalCmdFilter) launchExternalCmd(ctx context.Context, buf []Line) for { b, _, err := rdr.ReadLine() if len(b) > 0 { - // Lookup the Line from its ID - id, err := strconv.ParseUint(string(b), 10, 64) - if err == nil { - continue - } + switch { + case ecf.returnsLineID: + // Lookup the Line from its ID +pdebug.Printf("------------------> %s\n", b) + id, err := strconv.ParseUint(string(b), 10, 64) + if err != nil { + continue + } - l, ok := lines[id] - if !ok { - continue - } + l, ok := lines[id] + if !ok { + continue + } - cmdCh <- NewMatchedLine(l, nil) + cmdCh <- NewMatchedLine(l, nil) + default: + // This is the ONLY location where we need to actually + // RECREATE a RawLine, and thus the only place where + // ctx.enableSep is required. + cmdCh <- NewMatchedLine(NewRawLine(string(b), ecf.enableSep), nil) + } } if err != nil { break diff --git a/interface.go b/interface.go index 63c2ad7..84bd835 100644 --- a/interface.go +++ b/interface.go @@ -370,6 +370,11 @@ type CustomFilterConfig struct { // more often, but you pay the penalty of invoking that command // more times. BufferThreshold int + + // Set this to true if your tool can receive lines containing + // "DATA\0LINE_ID". You should only return the LINE_ID if this + // is set to true + ReturnsLineID bool } // StyleSet holds styles for various sections @@ -503,11 +508,12 @@ type RegexpFilter struct { } type ExternalCmdFilter struct { - enableSep bool - cmd string args []string + cmd string + enableSep bool name string - query string - thresholdBufsiz int outCh pipeline.OutputChannel + query string + returnsLineID bool + thresholdBufsiz int } diff --git a/peco.go b/peco.go index 83083b2..717a854 100644 --- a/peco.go +++ b/peco.go @@ -468,7 +468,7 @@ func (p *Peco) populateFilters() error { p.filters.Add(NewRegexpFilter()) for name, c := range p.config.CustomFilter { - f := NewExternalCmdFilter(name, c.Cmd, c.Args, c.BufferThreshold, p.enableSep) + f := NewExternalCmdFilter(name, c.Cmd, c.Args, c.BufferThreshold, p.enableSep, c.ReturnsLineID) p.filters.Add(f) }