Skip to content

Commit

Permalink
Add an option to begin at the top of the list (fix #126) (#130)
Browse files Browse the repository at this point in the history
* Add an option to begin at the top of the list (fix #126)

* Update option.go

Co-authored-by: ktr <ktr@syfm.me>

* Fix

* fix

Co-authored-by: ktr <ktr@syfm.me>
  • Loading branch information
r-darwish and ktr0731 authored Nov 23, 2021
1 parent 30689be commit 35bbbaa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fuzzyfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func (f *finder) initFinder(items []string, matched []matching.Matched, opt opt)
f.state.items = items
f.state.matched = matched
f.state.allMatched = matched

if opt.beginAtTop {
f.state.cursorY = len(f.state.matched) - 1
f.state.y = len(f.state.matched) - 1
}

if !isInTesting() {
f.drawTimer = time.AfterFunc(0, func() {
f._draw()
Expand Down
20 changes: 20 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type opt struct {
hotReload bool
promptString string
header string
beginAtTop bool
}

type mode int
Expand Down Expand Up @@ -58,6 +59,25 @@ func WithHotReload() Option {
}
}

type cursorPosition int

const (
CursorPositionBottom cursorPosition = iota
CursorPositionTop
)

// WithCursorPosition sets the initial position of the cursor
func WithCursorPosition(position cursorPosition) Option {
return func(o *opt) {
switch position {
case CursorPositionTop:
o.beginAtTop = true
case CursorPositionBottom:
o.beginAtTop = false
}
}
}

// WithPromptString changes the prompt string. The default value is "> ".
func WithPromptString(s string) Option {
return func(o *opt) {
Expand Down

0 comments on commit 35bbbaa

Please sign in to comment.