From 9c2bbf92cefe4f9b753f4176d0aba337bb0dceb1 Mon Sep 17 00:00:00 2001 From: Daisuke Maki Date: Tue, 22 Dec 2020 20:30:20 +0900 Subject: [PATCH] Fix a few low hanging fruits --- action.go | 12 ++++++------ action_test.go | 2 +- buffer.go | 6 +++--- filter.go | 6 +++--- go.mod | 2 +- keymap.go | 9 ++------- layout.go | 2 +- peco_test.go | 14 -------------- view.go | 6 +++--- 9 files changed, 20 insertions(+), 39 deletions(-) diff --git a/action.go b/action.go index 5e83f4e..ad57ff2 100644 --- a/action.go +++ b/action.go @@ -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) } diff --git a/action_test.go b/action_test.go index df3340c..0ab6e65 100644 --- a/action_test.go +++ b/action_test.go @@ -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 } diff --git a/buffer.go b/buffer.go index a94b90b..da5301c 100644 --- a/buffer.go +++ b/buffer.go @@ -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() } } diff --git a/filter.go b/filter.go index 735f5e5..2126f94 100644 --- a/filter.go +++ b/filter.go @@ -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() diff --git a/go.mod b/go.mod index 2ca7b5d..6c02ef5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/keymap.go b/keymap.go index 2a93ab9..c7476f3 100644 --- a/keymap.go +++ b/keymap.go @@ -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 -} diff --git a/layout.go b/layout.go index a874be3..a7a19eb 100644 --- a/layout.go +++ b/layout.go @@ -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). diff --git a/peco_test.go b/peco_test.go index 83784fa..1f7e440 100644 --- a/peco_test.go +++ b/peco_test.go @@ -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"} diff --git a/view.go b/view.go index 3436aab..0d10f39 100644 --- a/view.go +++ b/view.go @@ -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) }