-
Notifications
You must be signed in to change notification settings - Fork 5
/
option.go
242 lines (204 loc) Β· 5.12 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package fzf
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"github.com/charmbracelet/bubbles/key"
)
var defaultOption = option{
limit: 1,
noLimit: false,
caseSensitive: false,
prompt: "> ",
cursor: "> ",
selectedPrefix: "β ",
unselectedPrefix: "β― ",
inputPlaceholder: "Filter...",
styles: NewStyles(),
keymap: &keymap{
Up: key.NewBinding(key.WithKeys("up", "ctrl+p")),
Down: key.NewBinding(key.WithKeys("down", "ctrl+n")),
Toggle: key.NewBinding(key.WithKeys("tab")),
Choose: key.NewBinding(key.WithKeys("enter")),
Abort: key.NewBinding(key.WithKeys("ctrl+c", "esc")),
},
inputPosition: InputPositionTop,
countViewEnabled: true,
countViewFunc: func(meta CountViewMeta) string {
var v strings.Builder
_, _ = v.WriteString(strconv.Itoa(meta.MatchesCount))
_, _ = v.WriteRune('/')
_, _ = v.WriteString(strconv.Itoa(meta.ItemsCount))
_, _ = v.WriteRune(' ')
if meta.Limit > 1 || meta.NoLimit {
_, _ = v.WriteString("(")
_, _ = v.WriteString(strconv.Itoa(meta.SelectedCount))
if !meta.NoLimit {
_, _ = v.WriteRune('/')
_, _ = v.WriteString(strconv.Itoa(meta.Limit))
}
_, _ = v.WriteString(") ")
}
borderw := meta.Width - v.Len()
if borderw < 0 {
borderw = 0
}
_, _ = v.WriteString(strings.Repeat("β", borderw))
return v.String()
},
hotReloadLocker: nil,
}
// CountViewMeta provides information used in count view.
type CountViewMeta struct {
ItemsCount int
MatchesCount int
SelectedCount int
Width int
Limit int
NoLimit bool
}
// InputPosition represents the position of input.
type InputPosition string
const (
InputPositionTop InputPosition = "top"
InputPositionBottom InputPosition = "bottom"
)
// Valid validates the value of InputPosition.
func (p InputPosition) Valid() error {
switch p {
case InputPositionTop, InputPositionBottom:
return nil
default:
return fmt.Errorf("invalid input position: %s", p)
}
}
type option struct {
limit int
noLimit bool
caseSensitive bool
prompt string
cursor string
selectedPrefix string
unselectedPrefix string
inputPlaceholder string
styles *Styles
keymap *keymap
inputPosition InputPosition
countViewEnabled bool
countViewFunc func(meta CountViewMeta) string
hotReloadLocker sync.Locker
}
func (o *option) valid() error {
if o.limit < 1 {
return errors.New("limit must be at least 1")
}
if err := o.inputPosition.Valid(); err != nil {
return err
}
return nil
}
func (o *option) multiple() bool {
return o.noLimit || o.limit > 1
}
// 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 {
o.keymap.Up = key.NewBinding(key.WithKeys(km.Up...))
}
if len(km.Down) > 0 {
o.keymap.Down = key.NewBinding(key.WithKeys(km.Down...))
}
if len(km.Toggle) > 0 {
o.keymap.Toggle = key.NewBinding(key.WithKeys(km.Toggle...))
}
if len(km.Choose) > 0 {
o.keymap.Choose = key.NewBinding(key.WithKeys(km.Choose...))
}
if len(km.Abort) > 0 {
o.keymap.Abort = key.NewBinding(key.WithKeys(append(km.Abort, "ctrl+c")...))
}
}
}
// WithInputPlaceholder sets the placeholder for input.
func WithInputPlaceholder(p string) Option {
return func(o *option) {
o.inputPlaceholder = p
}
}
// WithCountViewEnabled enables or disables count view.
func WithCountViewEnabled(b bool) Option {
return func(o *option) {
o.countViewEnabled = b
}
}
// WithCountView sets the function to create the count view.
func WithCountView(f func(meta CountViewMeta) string) Option {
return func(o *option) {
o.countViewFunc = f
}
}
// WithHotReload sets the locker for read items.
func WithHotReload(locker sync.Locker) Option {
return func(o *option) {
o.hotReloadLocker = locker
}
}
// WithCaseSensitive sets the case sensitivity.
func WithCaseSensitive(s bool) Option {
return func(o *option) {
o.caseSensitive = s
}
}
// WithInputPosition sets the position of input.
func WithInputPosition(p InputPosition) Option {
return func(o *option) {
o.inputPosition = p
}
}