Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 24, 2023
1 parent d589f7c commit 0ec8ec8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions error.go
Expand Up @@ -3,5 +3,6 @@ package fzf
import "errors"

var (
// ErrAbort is returned when a Fuzzy Finder is aborted.
ErrAbort = errors.New("abort")
)
3 changes: 3 additions & 0 deletions fzf.go
Expand Up @@ -2,10 +2,12 @@ package fzf

import tea "github.com/charmbracelet/bubbletea"

// Fuzzy Finder.
type FZF struct {
option *option
}

// New returns a new Fuzzy Finder.
func New(opts ...Option) *FZF {
o := defaultOption
for _, opt := range opts {
Expand All @@ -17,6 +19,7 @@ func New(opts ...Option) *FZF {
}
}

// Find launches the Fuzzy Finder and returns a list of indexes of the selected items.
func (fzf *FZF) Find(items Items) ([]int, error) {
m := newModel(fzf, newItems(items))

Expand Down
6 changes: 5 additions & 1 deletion item.go
@@ -1,7 +1,11 @@
package fzf

// Items is a list of items to be searched by the Fuzzy Finder.
type Items interface {
ItemString(int) string
// ItemString returns the string of the i-th item in Items.
ItemString(i int) string

// Len returns the number of items in Items.
Len() int
}

Expand Down
1 change: 1 addition & 0 deletions keymap.go
Expand Up @@ -2,6 +2,7 @@ package fzf

import "github.com/charmbracelet/bubbles/key"

// KeyMap is the key mapping for the Fuzzy Finder.
type KeyMap struct {
Up []string
Down []string
Expand Down
10 changes: 10 additions & 0 deletions option.go
Expand Up @@ -34,50 +34,59 @@ type option struct {
keymap *keymap
}

// Option represents a option for the Fuzzy Finder.
type Option func(o *option)

// WithLimit sets the number of items that can be selected.
func WithLimit(l int) Option {
return func(o *option) {
o.limit = l
}
}

// WithNoLimit can be set to `true` to allow unlimited item selection.
func WithNoLimit(n bool) Option {
return func(o *option) {
o.noLimit = n
}
}

// WithPrompt sets the prompt text.
func WithPrompt(p string) Option {
return func(o *option) {
o.prompt = p
}
}

// WithCursor sets the cursor.
func WithCursor(c string) Option {
return func(o *option) {
o.cursor = c
}
}

// WithSelectedPrefix sets the prefix of the selected item.
func WithSelectedPrefix(p string) Option {
return func(o *option) {
o.selectedPrefix = p
}
}

// WithUnselectedPrefix sets the prefix of the unselected item.
func WithUnselectedPrefix(p string) Option {
return func(o *option) {
o.unselectedPrefix = p
}
}

// WithStyles sets the various styles.
func WithStyles(opts ...StylesOption) Option {
return func(o *option) {
o.styles = NewStyles(opts...)
}
}

// WithKeyMap sets the key mapping.
func WithKeyMap(km KeyMap) Option {
return func(o *option) {
if len(km.Up) > 0 {
Expand All @@ -98,6 +107,7 @@ func WithKeyMap(km KeyMap) Option {
}
}

// WithInputPlaceholder sets the placeholder for input.
func WithInputPlaceholder(p string) Option {
return func(o *option) {
o.inputPlaceholder = p
Expand Down
9 changes: 9 additions & 0 deletions styles.go
Expand Up @@ -12,6 +12,7 @@ var (
}
)

// Style represents a style.
type Style struct {
ForegroundColor string
BackgroundColor string
Expand All @@ -23,6 +24,7 @@ type Style struct {
Faint bool
}

// Styles is the styles for each component.
type Styles struct {
option *stylesOption
}
Expand Down Expand Up @@ -58,6 +60,7 @@ func (s *Style) lipgloss() lipgloss.Style {
return style
}

// Option represents a option for the styles.
type StylesOption func(o *stylesOption)

type stylesOption struct {
Expand All @@ -68,6 +71,7 @@ type stylesOption struct {
matches lipgloss.Style
}

// NewStyles returns a new styles.
func NewStyles(opts ...StylesOption) *Styles {
o := defaultStylesOption
for _, opt := range opts {
Expand All @@ -76,30 +80,35 @@ func NewStyles(opts ...StylesOption) *Styles {
return &Styles{option: o}
}

// WithStyleCursor sets the style of cursor.
func WithStyleCursor(s Style) StylesOption {
return func(o *stylesOption) {
o.cursor = s.lipgloss()
}
}

// WithStyleCursorLine sets the style of cursor line.
func WithStyleCursorLine(s Style) StylesOption {
return func(o *stylesOption) {
o.cursorLine = s.lipgloss()
}
}

// WithStyleSelectedPrefix sets the style of prefix of the selected item.
func WithStyleSelectedPrefix(s Style) StylesOption {
return func(o *stylesOption) {
o.selectedPrefix = s.lipgloss()
}
}

// WithStyleUnselectedPrefix sets the style of prefix of the unselected item.
func WithStyleUnselectedPrefix(s Style) StylesOption {
return func(o *stylesOption) {
o.unselectedPrefix = s.lipgloss()
}
}

// WithStyleMatches sets the style of the matched characters.
func WithStyleMatches(s Style) StylesOption {
return func(o *stylesOption) {
o.matches = s.lipgloss()
Expand Down

0 comments on commit 0ec8ec8

Please sign in to comment.