Merge pull request #457 from taynaud/master

Add a few commands to use peco more like a pager
This commit is contained in:
lestrrat 2018-10-25 16:56:57 +09:00 committed by GitHub
commit 6762b04883
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 131 additions and 7 deletions

125
action.go
View file

@ -3,6 +3,7 @@ package peco
import (
"bytes"
"fmt"
"math"
"os"
"strconv"
"unicode"
@ -127,6 +128,11 @@ func init() {
ActionFunc(doRefreshScreen).Register("RefreshScreen", termbox.KeyCtrlL)
ActionFunc(doToggleSingleKeyJump).Register("ToggleSingleKeyJump")
ActionFunc(doToggleViewArround).Register("ViewArround", termbox.KeyCtrlV)
ActionFunc(doGoToNextSelection).Register("GoToNextSelection", termbox.KeyCtrlK)
ActionFunc(doGoToPreviousSelection).Register(" doGoToPreviousSelection", termbox.KeyCtrlJ)
ActionFunc(doKonamiCommand).RegisterKeySequence(
"KonamiCommand",
keyseq.KeyList{
@ -771,6 +777,125 @@ func doToggleSingleKeyJump(ctx context.Context, state *Peco, e termbox.Event) {
state.ToggleSingleKeyJumpMode()
}
func doToggleViewArround(ctx context.Context, state *Peco, e termbox.Event) {
if pdebug.Enabled {
g := pdebug.Marker("doToggleViewArround")
defer g.End()
}
q := state.Query()
if q.Len() > 0 {
l, err := state.CurrentLineBuffer().LineAt(state.Location().LineNumber())
if err != nil {
return
}
currentLine := l.ID()
doDeleteAll(ctx, state, e)
state.Hub().SendPaging(JumpToLineRequest(currentLine))
}
}
func doGoToNextSelection(_ context.Context, state *Peco, _ termbox.Event) {
if pdebug.Enabled {
g := pdebug.Marker("doGoToNextSelection")
defer g.End()
}
selection := state.Selection()
if selection.Len() == 0 {
state.Hub().SendStatusMsg("No Selection")
return
}
b := state.CurrentLineBuffer()
l, err := b.LineAt(state.Location().LineNumber())
if err != nil {
return
}
currentLine := l.ID()
nextLine := uint64(math.MaxUint64)
firstLine := uint64(math.MaxUint64)
found := false
selection.Ascend(func(it btree.Item) bool {
selectedLine := it.(line.Line)
if selectedLine.ID() > currentLine {
if selectedLine.ID() < nextLine {
nextLine = selectedLine.ID()
found = true
}
}
if selectedLine.ID() <= firstLine {
firstLine = selectedLine.ID()
}
return true
})
if found {
state.Hub().SendStatusMsg("Next Selection")
state.Hub().SendPaging(ToScrollFirstItem)
state.Hub().SendPaging(JumpToLineRequest(nextLine))
} else {
state.Hub().SendStatusMsg("Next Selection (first)")
state.Hub().SendPaging(ToScrollFirstItem)
state.Hub().SendPaging(JumpToLineRequest(firstLine))
}
}
func doGoToPreviousSelection(_ context.Context, state *Peco, _ termbox.Event) {
if pdebug.Enabled {
g := pdebug.Marker("doGoToPreviousSelection")
defer g.End()
}
selection := state.Selection()
if selection.Len() == 0 {
state.Hub().SendStatusMsg("No Selection")
return
}
b := state.CurrentLineBuffer()
l, err := b.LineAt(state.Location().LineNumber())
if err != nil {
return
}
currentLine := l.ID()
previousLine := uint64(0)
lastLine := uint64(math.MaxUint64)
found := false
selection.Ascend(func(it btree.Item) bool {
selectedLine := it.(line.Line)
if selectedLine.ID() < currentLine {
if selectedLine.ID() > previousLine {
previousLine = selectedLine.ID()
found = true
}
}
if selectedLine.ID() >= lastLine {
lastLine = selectedLine.ID()
}
return true
})
if found {
state.Hub().SendStatusMsg("Previous Selection")
state.Hub().SendPaging(ToScrollFirstItem)
state.Hub().SendPaging(JumpToLineRequest(previousLine))
} else {
state.Hub().SendStatusMsg("Previous Selection (first)")
state.Hub().SendPaging(ToScrollFirstItem)
state.Hub().SendPaging(JumpToLineRequest(lastLine))
}
}
func doSingleKeyJump(ctx context.Context, state *Peco, e termbox.Event) {
if pdebug.Enabled {
g := pdebug.Marker("doSingleKeyJump %c", e.Ch)

View file

@ -3,8 +3,8 @@ package peco
import (
"time"
"github.com/nsf/termbox-go"
"context"
"github.com/nsf/termbox-go"
)
func NewInput(state *Peco, am ActionMap, src chan termbox.Event) *Input {

View file

@ -7,9 +7,9 @@ import (
"testing"
"time"
"context"
termbox "github.com/nsf/termbox-go"
"github.com/stretchr/testify/assert"
"context"
)
func TestIssue212_SanityCheck(t *testing.T) {

View file

@ -1,15 +1,14 @@
package peco
import (
"context"
"sort"
"strings"
"time"
"github.com/lestrrat-go/pdebug"
"github.com/nsf/termbox-go"
"github.com/peco/peco/internal/keyseq"
"github.com/pkg/errors"
"context"
)
// NewKeymap creates a new Keymap struct

View file

@ -74,7 +74,7 @@ func (q *Query) Runes() <-chan rune {
defer q.mutex.Unlock()
for _, r := range q.query {
c<-r
c <- r
}
}()

View file

@ -18,7 +18,7 @@ import (
// call Setup()
func NewSource(name string, in io.Reader, idgen line.IDGenerator, capacity int, enableSep bool) *Source {
s := &Source{
name: name,
name: name,
in: in, // Note that this may be closed, so do not rely on it
capacity: capacity,
enableSep: enableSep,

View file

@ -3,8 +3,8 @@ package peco
import (
"time"
"github.com/peco/peco/hub"
"context"
"github.com/peco/peco/hub"
)
type statusMsgReq interface {