mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
change applyFn to interface
This commit is contained in:
parent
88b5b7a39c
commit
8a9dfefb3a
|
|
@ -41,11 +41,17 @@ func (c *LineCollector) Lines() []line.Line {
|
|||
return c.lines
|
||||
}
|
||||
|
||||
// lineApplier is implemented by filter types that embed baseFilter to provide
|
||||
// their type-specific matching logic.
|
||||
type lineApplier interface {
|
||||
applyInternal(ctx context.Context, lines []line.Line, em LineEmitter) error
|
||||
}
|
||||
|
||||
// baseFilter provides shared implementations of Apply, ApplyCollect,
|
||||
// NewContext, and BufSize for filters that follow the applyInternal pattern.
|
||||
// Filters embed this type and set applyFn to their type-specific matching logic.
|
||||
// Filters embed this type and set impl to their concrete filter value.
|
||||
type baseFilter struct {
|
||||
applyFn func(ctx context.Context, lines []line.Line, em LineEmitter) error
|
||||
impl lineApplier
|
||||
}
|
||||
|
||||
// NewContext returns a context initialized with the given query for pipeline use.
|
||||
|
|
@ -59,13 +65,13 @@ func (b *baseFilter) BufSize() int {
|
|||
|
||||
// Apply runs the filter's matching logic on lines, sending matches to out.
|
||||
func (b *baseFilter) Apply(ctx context.Context, lines []line.Line, out pipeline.ChanOutput) error {
|
||||
return b.applyFn(ctx, lines, &chanEmitter{out: out})
|
||||
return b.impl.applyInternal(ctx, lines, &chanEmitter{out: out})
|
||||
}
|
||||
|
||||
// ApplyCollect runs the filter and returns matched lines directly as a slice,
|
||||
// bypassing channel-based output for better performance in parallel paths.
|
||||
func (b *baseFilter) ApplyCollect(ctx context.Context, lines []line.Line) ([]line.Line, error) {
|
||||
c := NewLineCollector(len(lines) / 2)
|
||||
err := b.applyFn(ctx, lines, c)
|
||||
err := b.impl.applyInternal(ctx, lines, c)
|
||||
return c.Lines(), err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ func NewFuzzy(sortLongest bool) *Fuzzy {
|
|||
ff := &Fuzzy{
|
||||
sortLongest: sortLongest,
|
||||
}
|
||||
ff.applyFn = ff.applyInternal
|
||||
ff.impl = ff
|
||||
return ff
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ func newRegexpFilter(name string, flags regexpFlags, quotemeta bool) *Regexp {
|
|||
quotemeta: quotemeta,
|
||||
name: name,
|
||||
}
|
||||
rf.applyFn = rf.applyInternal
|
||||
rf.impl = rf
|
||||
return rf
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue