Skip to content

Commit

Permalink
Adding more movement keys (#153)
Browse files Browse the repository at this point in the history
* Adding more movement keys

Implemented a min function
Added actions for Home, End, PageUp and PageDown
Fixes #125

* Scroll by pane height-1

* Home and End should control cursor position
  • Loading branch information
matheusfillipe committed Jun 13, 2022
1 parent df4cd55 commit 76fb6bd
Showing 1 changed file with 30 additions and 5 deletions.
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

0 comments on commit 76fb6bd

Please sign in to comment.