mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
Remove unused stuff
This commit is contained in:
parent
497029fbcc
commit
a78e298fc6
113
buffer.go
113
buffer.go
|
|
@ -23,32 +23,6 @@ func (sp simplePipeline) Pipeline() (chan struct{}, chan Line) {
|
|||
return sp.cancelCh, sp.outputCh
|
||||
}
|
||||
|
||||
func acceptPipeline(cancel chan struct{}, in chan Line, out chan Line, pc *pipelineCtx) {
|
||||
trace("acceptPipeline: START")
|
||||
defer trace("acceptPipeline: END")
|
||||
defer close(out)
|
||||
for {
|
||||
select {
|
||||
case <-cancel:
|
||||
trace("acceptPipeline: detected cancel request. Bailing out")
|
||||
return
|
||||
case l, ok := <-in:
|
||||
if l == nil && !ok {
|
||||
trace("acceptPipeline: detected end of input. Bailing out")
|
||||
if pc.onEnd != nil {
|
||||
pc.onEnd()
|
||||
}
|
||||
return
|
||||
}
|
||||
trace("acceptPipeline: forwarding to callback")
|
||||
if ll, err := pc.onIncomingLine(l); err == nil {
|
||||
trace("acceptPipeline: forwarding to out channel")
|
||||
out <- ll
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (buffers *dependentBuffers) Register(lb LineBuffer) {
|
||||
*buffers = append(*buffers, lb)
|
||||
}
|
||||
|
|
@ -75,93 +49,6 @@ func (buffers dependentBuffers) InvalidateUpTo(i int) {
|
|||
}
|
||||
}
|
||||
|
||||
func NewRawLineBuffer() *RawLineBuffer {
|
||||
return &RawLineBuffer{
|
||||
simplePipeline: simplePipeline{},
|
||||
lines: []Line{},
|
||||
capacity: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) Replay() error {
|
||||
rlb.outputCh = make(chan Line)
|
||||
go func() {
|
||||
replayed := 0
|
||||
trace("RawLineBuffer.Replay (goroutine): START")
|
||||
defer func() { trace("RawLineBuffer.Replay (goroutine): END (Replayed %d lines)", replayed) }()
|
||||
|
||||
defer func() { recover() }() // It's okay if we fail to replay
|
||||
defer close(rlb.outputCh)
|
||||
for _, l := range rlb.lines {
|
||||
select {
|
||||
case rlb.outputCh <- l:
|
||||
replayed++
|
||||
case <-rlb.cancelCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) Accept(p Pipeliner) {
|
||||
cancelCh, incomingCh := p.Pipeline()
|
||||
rlb.cancelCh = cancelCh
|
||||
rlb.outputCh = make(chan Line)
|
||||
go acceptPipeline(cancelCh, incomingCh, rlb.outputCh,
|
||||
&pipelineCtx{rlb.Append, rlb.onEnd})
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) Append(l Line) (Line, error) {
|
||||
trace("RawLineBuffer.Append: %s", l.DisplayString())
|
||||
if rlb.capacity > 0 && len(rlb.lines) > rlb.capacity {
|
||||
diff := len(rlb.lines) - rlb.capacity
|
||||
|
||||
// Golang's version of array realloc
|
||||
rlb.lines = rlb.lines[diff:rlb.capacity:rlb.capacity]
|
||||
} else {
|
||||
rlb.lines = append(rlb.lines, l)
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) Register(lb LineBuffer) {
|
||||
rlb.buffers.Register(lb)
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) Unregister(lb LineBuffer) {
|
||||
rlb.buffers.Unregister(lb)
|
||||
}
|
||||
|
||||
// LineAt returns the line at index `i`
|
||||
func (rlb RawLineBuffer) LineAt(i int) (Line, error) {
|
||||
if i < 0 || len(rlb.lines) <= i {
|
||||
return nil, ErrBufferOutOfRange
|
||||
}
|
||||
return rlb.lines[i], nil
|
||||
}
|
||||
|
||||
// Size returns the number of lines in the buffer
|
||||
func (rlb RawLineBuffer) Size() int {
|
||||
return len(rlb.lines)
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) SetCapacity(capacity int) {
|
||||
if capacity < 0 {
|
||||
capacity = 0
|
||||
}
|
||||
rlb.capacity = capacity
|
||||
}
|
||||
|
||||
func (rlb RawLineBuffer) InvalidateUpTo(_ int) {
|
||||
// no op
|
||||
}
|
||||
|
||||
func (rlb *RawLineBuffer) AppendLine(l Line) (Line, error) {
|
||||
return rlb.Append(l)
|
||||
}
|
||||
|
||||
func NewFilteredBuffer(src Buffer, page, perPage int) *FilteredBuffer {
|
||||
fb := FilteredBuffer{
|
||||
src: src,
|
||||
|
|
|
|||
111
buffer_test.go
111
buffer_test.go
|
|
@ -1,111 +0,0 @@
|
|||
package peco
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBuffer(t *testing.T) {
|
||||
rawbuf := NewRawLineBuffer()
|
||||
for _, l := range []string{"Alice", "Bob", "Charlie"} {
|
||||
rawbuf.AppendLine(NewRawLine(l, false))
|
||||
}
|
||||
|
||||
if rawbuf.Size() != 3 {
|
||||
t.Errorf("Expected to read 3 lines, got %d", rawbuf.Size())
|
||||
}
|
||||
|
||||
f := RegexpFilter{
|
||||
flags: regexpFlagList(ignoreCaseFlags),
|
||||
query: `c`,
|
||||
}
|
||||
|
||||
rawbuf.Replay()
|
||||
done := make(chan struct{})
|
||||
f.Accept(rawbuf)
|
||||
|
||||
buf := NewRawLineBuffer()
|
||||
buf.onEnd = func() { done <- struct{}{} }
|
||||
buf.Accept(f)
|
||||
|
||||
for loop := true; loop; {
|
||||
select {
|
||||
case <-done:
|
||||
loop = false
|
||||
case <-buf.outputCh:
|
||||
}
|
||||
}
|
||||
|
||||
if buf.Size() != 2 {
|
||||
t.Errorf("Expected to match 2 lines, got %d", buf.Size())
|
||||
}
|
||||
|
||||
for i, v := range []string{"Alice", "Charlie"} {
|
||||
l, err := buf.LineAt(i)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get line at %d: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if l.DisplayString() != v {
|
||||
t.Errorf("Expected filtered output at %d to be '%s', got '%s'", i, v, l.DisplayString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferPaging(t *testing.T) {
|
||||
rawbuf := NewRawLineBuffer()
|
||||
for _, l := range []string{"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "George", "Hugh"} {
|
||||
rawbuf.AppendLine(NewRawLine(l, false))
|
||||
}
|
||||
|
||||
pc := PageCrop{perPage: 4, currentPage: 2}
|
||||
pagebuf := pc.Crop(rawbuf)
|
||||
|
||||
for i, v := range []string{"Eve", "Frank", "George", "Hugh"} {
|
||||
l, err := pagebuf.LineAt(i)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get line at %d: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if l.DisplayString() != v {
|
||||
t.Errorf("Expected filtered output at %d to be '%s', got '%s'", i, v, l.DisplayString())
|
||||
}
|
||||
}
|
||||
|
||||
rawbuf.Replay()
|
||||
|
||||
// Also test regexp filter + paging
|
||||
rf := RegexpFilter{
|
||||
flags: regexpFlagList(ignoreCaseFlags),
|
||||
query: `a`,
|
||||
}
|
||||
pc.perPage = 2
|
||||
|
||||
rf.Accept(rawbuf)
|
||||
|
||||
done := make(chan struct{})
|
||||
buf := NewRawLineBuffer()
|
||||
buf.onEnd = func() { done <- struct{}{} }
|
||||
buf.Accept(rf)
|
||||
|
||||
for loop := true; loop; {
|
||||
select {
|
||||
case <-done:
|
||||
loop = false
|
||||
case <-buf.outputCh:
|
||||
}
|
||||
}
|
||||
|
||||
pagebuf = pc.Crop(buf)
|
||||
|
||||
for i, v := range []string{"David", "Frank"} {
|
||||
l, err := pagebuf.LineAt(i)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get line at %d: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if l.DisplayString() != v {
|
||||
t.Errorf("Expected filtered output at %d to be '%s', got '%s'", i, v, l.DisplayString())
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue