Fix a few low hanging fruits

This commit is contained in:
Daisuke Maki 2020-12-22 20:30:20 +09:00
parent 0ec5ad84d5
commit 9c2bbf92ce
9 changed files with 20 additions and 39 deletions

View file

@ -438,9 +438,9 @@ func doScrollLastItem(ctx context.Context, state *Peco, e Event) {
}
func doToggleSelectionAndSelectNext(ctx context.Context, state *Peco, e Event) {
toplevel, _ := ctx.Value(isTopLevelActionCall).(bool)
toplevel, _ := ctx.Value(isTopLevelActionCall{}).(bool)
state.Hub().Batch(ctx, func(ctx context.Context) {
ctx = context.WithValue(ctx, isTopLevelActionCall, false)
ctx = context.WithValue(ctx, isTopLevelActionCall{}, false)
doToggleSelection(ctx, state, e)
// XXX This is sucky. Fix later
if state.LayoutType() == "top-down" {
@ -911,9 +911,9 @@ func doSingleKeyJump(ctx context.Context, state *Peco, e Event) {
return
}
toplevel, _ := ctx.Value(isTopLevelActionCall).(bool)
toplevel, _ := ctx.Value(isTopLevelActionCall{}).(bool)
state.Hub().Batch(ctx, func(ctx context.Context) {
ctx = context.WithValue(ctx, isTopLevelActionCall, false)
ctx = context.WithValue(ctx, isTopLevelActionCall{}, false)
state.Hub().SendPaging(ctx, JumpToLineRequest(index))
doFinish(ctx, state, e)
}, toplevel)
@ -921,9 +921,9 @@ func doSingleKeyJump(ctx context.Context, state *Peco, e Event) {
func makeCombinedAction(actions ...Action) ActionFunc {
return ActionFunc(func(ctx context.Context, state *Peco, e Event) {
toplevel, _ := ctx.Value(isTopLevelActionCall).(bool)
toplevel, _ := ctx.Value(isTopLevelActionCall{}).(bool)
state.Hub().Batch(ctx, func(ctx context.Context) {
ctx = context.WithValue(ctx, isTopLevelActionCall, false)
ctx = context.WithValue(ctx, isTopLevelActionCall{}, false)
for _, a := range actions {
a.Execute(ctx, state, e)
}

View file

@ -16,7 +16,7 @@ func TestActionFunc(t *testing.T) {
af := ActionFunc(func(_ context.Context, _ *Peco, _ Event) {
called++
})
af.Execute(nil, nil, nil)
af.Execute(context.TODO(), nil, nil)
if !assert.Equal(t, called, 1, "Expected ActionFunc to be called once, but it got called %d times", called) {
return
}

View file

@ -122,9 +122,9 @@ func (mb *MemoryBuffer) Accept(ctx context.Context, in chan interface{}, _ pipel
}
return
case v := <-in:
switch v.(type) {
switch v := v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pipeline.IsEndMark(v) {
if pdebug.Enabled {
pdebug.Printf("MemoryBuffer received end mark (read %d lines, %s since starting accept loop)", len(mb.lines), time.Since(start).String())
}
@ -132,7 +132,7 @@ func (mb *MemoryBuffer) Accept(ctx context.Context, in chan interface{}, _ pipel
}
case line.Line:
mb.mutex.Lock()
mb.lines = append(mb.lines, v.(line.Line))
mb.lines = append(mb.lines, v)
mb.mutex.Unlock()
}
}

View file

@ -82,9 +82,9 @@ func acceptAndFilter(ctx context.Context, f filter.Filter, in chan interface{},
buf = buffer.GetLineListBuf()
}
case v := <-in:
switch v.(type) {
switch v := v.(type) {
case error:
if pipeline.IsEndMark(v.(error)) {
if pipeline.IsEndMark(v) {
if pdebug.Enabled {
pdebug.Printf("filter received end mark (read %d lines, %s since starting accept loop)", lines+len(buf), time.Since(start).String())
}
@ -103,7 +103,7 @@ func acceptAndFilter(ctx context.Context, f filter.Filter, in chan interface{},
// process while we filter what we already have. The buffer
// size is fairly big, because this really only makes a
// difference if we have a lot of lines to process.
buf = append(buf, v.(line.Line))
buf = append(buf, v)
if len(buf) >= bufsiz {
flush <- buf
buf = buffer.GetLineListBuf()

2
go.mod
View file

@ -8,7 +8,7 @@ require (
github.com/gdamore/tcell/v2 v2.1.0
github.com/google/btree v0.0.0-20161213163243-0c3044bc8bad
github.com/jessevdk/go-flags v1.1.0
github.com/lestrrat-go/codegen v0.0.0-20201219095749-85e244da3358 // indirect
github.com/lestrrat-go/codegen v0.0.0-20201219095749-85e244da3358
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc
github.com/lestrrat-go/pdebug v0.0.0-20180220043849-39f9a71bcabe
github.com/mattn/go-runewidth v0.0.9

View file

@ -24,7 +24,7 @@ func (km Keymap) Sequence() Keyseq {
return km.seq
}
const isTopLevelActionCall = "peco.isTopLevelActionCall"
type isTopLevelActionCall struct{}
func (km Keymap) ExecuteAction(ctx context.Context, state *Peco, ev Event) (err error) {
if pdebug.Enabled {
@ -37,7 +37,7 @@ func (km Keymap) ExecuteAction(ctx context.Context, state *Peco, ev Event) (err
return errors.New("action not found")
}
ctx = context.WithValue(ctx, isTopLevelActionCall, true)
ctx = context.WithValue(ctx, isTopLevelActionCall{}, true)
a.Execute(ctx, state, ev)
return nil
}
@ -183,8 +183,3 @@ func (km *Keymap) ApplyKeybinding() error {
return errors.Wrap(k.Compile(), "failed to compile key binding patterns")
}
// TODO: this needs to be fixed.
func (km Keymap) hasModifierMaps() bool {
return false
}

View file

@ -538,7 +538,7 @@ func (l *ListArea) Draw(state *Peco, parent Layout, perPage int, options *DrawOp
Fill(true).
Do()
} else if len(line) > m[1] {
l.screen.Print(line[m[1]:len(line)]).
l.screen.Print(line[m[1]:]).
X(prev).
Y(y).
XOffset(xOffset).

View file

@ -45,13 +45,6 @@ func newInterceptor() *interceptor {
}
}
func (i *interceptor) reset() {
i.m.Lock()
defer i.m.Unlock()
i.events = make(map[string][]interceptorArgs)
}
func (i *interceptor) record(name string, args []interface{}) {
i.m.Lock()
defer i.m.Unlock()
@ -209,13 +202,6 @@ func TestPeco(t *testing.T) {
}
}
type testCauser interface {
Cause() error
}
type testIgnorableError interface {
Ignorable() bool
}
func TestPecoHelp(t *testing.T) {
p := newPeco()
p.Argv = []string{"peco", "-h"}

View file

@ -52,16 +52,16 @@ func (v *View) Loop(ctx context.Context, cancel func()) error {
v.movePage(r, r.Data().(PagingRequest))
case r := <-h.DrawCh():
tmp := r.Data()
switch tmp.(type) {
switch tmp := tmp.(type) {
case string:
switch tmp.(string) {
switch tmp {
case "prompt":
v.drawPrompt(r)
case "purgeCache":
v.purgeDisplayCache(r)
}
case *DrawOptions:
v.drawScreen(r, tmp.(*DrawOptions))
v.drawScreen(r, tmp)
default:
v.drawScreen(r, nil)
}