Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more movement keys #153

Merged
merged 3 commits into from Jun 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions fuzzyfinder.go
Expand Up @@ -27,6 +27,19 @@ var (
errEntered = errors.New("entered")
)

// Finds the minimum value among the arguments
func min(vars ...int) int {
min := vars[0]

for _, i := range vars {
if min > i {
min = i
}
}

return min
}

type state struct {
items []string // All item names.
allMatched []matching.Matched // All items.
Expand Down Expand Up @@ -415,6 +428,12 @@ func (f *finder) readKey() error {
f.stateMu.Lock()
defer f.stateMu.Unlock()

_, screenHeight := f.term.Size()
matchedLinesCount := len(f.state.matched)

// Max number of lines to scroll by using PgUp and PgDn
var pageScrollBy = screenHeight - 3

switch e := e.(type) {
case *tcell.EventKey:
switch e.Key() {
Expand Down Expand Up @@ -450,10 +469,10 @@ func (f *finder) readKey() error {
f.state.cursorX += runewidth.RuneWidth(f.state.input[f.state.x])
f.state.x++
}
case tcell.KeyCtrlA:
case tcell.KeyCtrlA, tcell.KeyHome:
f.state.cursorX = 0
f.state.x = 0
case tcell.KeyCtrlE:
case tcell.KeyCtrlE, tcell.KeyEnd:
f.state.cursorX = runewidth.StringWidth(string(f.state.input))
f.state.x = len(f.state.input)
case tcell.KeyCtrlW:
Expand All @@ -476,11 +495,10 @@ func (f *finder) readKey() error {
f.state.cursorX = 0
f.state.x = 0
case tcell.KeyUp, tcell.KeyCtrlK, tcell.KeyCtrlP:
if f.state.y+1 < len(f.state.matched) {
if f.state.y+1 < matchedLinesCount {
f.state.y++
}
_, height := f.term.Size()
if f.state.cursorY+1 < height-2 && f.state.cursorY+1 < len(f.state.matched) {
if f.state.cursorY+1 < min(matchedLinesCount, screenHeight-2) {
f.state.cursorY++
}
case tcell.KeyDown, tcell.KeyCtrlJ, tcell.KeyCtrlN:
Expand All @@ -490,6 +508,13 @@ func (f *finder) readKey() error {
if f.state.cursorY-1 >= 0 {
f.state.cursorY--
}
case tcell.KeyPgUp:
f.state.y += min(pageScrollBy, matchedLinesCount-1-f.state.y)
maxCursorY := min(screenHeight-3, matchedLinesCount-1)
f.state.cursorY += min(pageScrollBy, maxCursorY-f.state.cursorY)
case tcell.KeyPgDn:
f.state.y -= min(pageScrollBy, f.state.y)
f.state.cursorY -= min(pageScrollBy, f.state.cursorY)
case tcell.KeyTab:
if !f.opt.multi {
return nil
Expand Down