create ReturnsLineID switch

This commit is contained in:
Daisuke Maki 2016-06-20 09:53:26 -04:00
parent fd4a1f4e8f
commit 8c435afe89
3 changed files with 38 additions and 20 deletions

View file

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

View file

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

View file

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