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 1 commit
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
33 changes: 32 additions & 1 deletion 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,9 @@ func (f *finder) readKey() error {
f.stateMu.Lock()
defer f.stateMu.Unlock()

// Max number of lines to scroll by using PgUp and PgDn
const pageScrollBy = 15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you scroll the list based on the actual screen size?


switch e := e.(type) {
case *tcell.EventKey:
switch e.Key() {
Expand Down Expand Up @@ -480,7 +496,7 @@ func (f *finder) readKey() error {
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(len(f.state.matched), height-2) {
f.state.cursorY++
}
case tcell.KeyDown, tcell.KeyCtrlJ, tcell.KeyCtrlN:
Expand All @@ -490,6 +506,21 @@ func (f *finder) readKey() error {
if f.state.cursorY-1 >= 0 {
f.state.cursorY--
}
case tcell.KeyPgUp:
f.state.y += min(pageScrollBy, len(f.state.matched)-1-f.state.y)
_, height := f.term.Size()
maxCursorY := min(height-3, len(f.state.matched)-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.KeyHome:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the action of Home in fzf is beginning-of-line, go-fuzzyfinder's one should also be the same behavior.

f.state.y = len(f.state.matched) - 1
_, height := f.term.Size()
f.state.cursorY = min(height-3, len(f.state.matched)-1)
case tcell.KeyEnd:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as the above comment. This should be the same behavior of end-of-line.

f.state.y = 0
f.state.cursorY = 0
case tcell.KeyTab:
if !f.opt.multi {
return nil
Expand Down