| 1 |
package huh |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/charmbracelet/bubbles/key" |
| 12 |
"github.com/charmbracelet/bubbles/spinner" |
| 13 |
"github.com/charmbracelet/bubbles/textinput" |
| 14 |
"github.com/charmbracelet/bubbles/viewport" |
| 15 |
tea "github.com/charmbracelet/bubbletea" |
| 16 |
"github.com/charmbracelet/huh/internal/accessibility" |
| 17 |
"github.com/charmbracelet/lipgloss" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
minHeight = 1 |
| 22 |
defaultHeight = 10 |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type Select[T comparable] struct { |
| 32 |
id int |
| 33 |
accessor Accessor[T] |
| 34 |
key string |
| 35 |
|
| 36 |
viewport viewport.Model |
| 37 |
|
| 38 |
title Eval[string] |
| 39 |
description Eval[string] |
| 40 |
options Eval[[]Option[T]] |
| 41 |
filteredOptions []Option[T] |
| 42 |
|
| 43 |
validate func(T) error |
| 44 |
err error |
| 45 |
|
| 46 |
selected int |
| 47 |
focused bool |
| 48 |
filtering bool |
| 49 |
filter textinput.Model |
| 50 |
spinner spinner.Model |
| 51 |
|
| 52 |
inline bool |
| 53 |
width int |
| 54 |
height int |
| 55 |
accessible bool |
| 56 |
theme *Theme |
| 57 |
keymap SelectKeyMap |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
func NewSelect[T comparable]() *Select[T] { |
| 67 |
filter := textinput.New() |
| 68 |
filter.Prompt = "/" |
| 69 |
|
| 70 |
s := spinner.New(spinner.WithSpinner(spinner.Line)) |
| 71 |
|
| 72 |
return &Select[T]{ |
| 73 |
accessor: &EmbeddedAccessor[T]{}, |
| 74 |
validate: func(T) error { return nil }, |
| 75 |
filtering: false, |
| 76 |
filter: filter, |
| 77 |
id: nextID(), |
| 78 |
options: Eval[[]Option[T]]{cache: make(map[uint64][]Option[T])}, |
| 79 |
title: Eval[string]{cache: make(map[uint64]string)}, |
| 80 |
description: Eval[string]{cache: make(map[uint64]string)}, |
| 81 |
spinner: s, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (s *Select[T]) Value(value *T) *Select[T] { |
| 87 |
return s.Accessor(NewPointerAccessor(value)) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (s *Select[T]) Accessor(accessor Accessor[T]) *Select[T] { |
| 92 |
s.accessor = accessor |
| 93 |
s.selectValue(s.accessor.Get()) |
| 94 |
s.updateValue() |
| 95 |
return s |
| 96 |
} |
| 97 |
|
| 98 |
func (s *Select[T]) selectValue(value T) { |
| 99 |
for i, o := range s.options.val { |
| 100 |
if o.Value == value { |
| 101 |
s.selected = i |
| 102 |
break |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func (s *Select[T]) Key(key string) *Select[T] { |
| 110 |
s.key = key |
| 111 |
return s |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func (s *Select[T]) Title(title string) *Select[T] { |
| 118 |
s.title.val = title |
| 119 |
s.title.fn = nil |
| 120 |
return s |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
func (s *Select[T]) TitleFunc(f func() string, bindings any) *Select[T] { |
| 131 |
s.title.fn = f |
| 132 |
s.title.bindings = bindings |
| 133 |
return s |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func (s *Select[T]) Filtering(filtering bool) *Select[T] { |
| 138 |
s.filtering = filtering |
| 139 |
s.filter.Focus() |
| 140 |
return s |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func (s *Select[T]) Description(description string) *Select[T] { |
| 147 |
s.description.val = description |
| 148 |
return s |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func (s *Select[T]) DescriptionFunc(f func() string, bindings any) *Select[T] { |
| 159 |
s.description.fn = f |
| 160 |
s.description.bindings = bindings |
| 161 |
return s |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func (s *Select[T]) Options(options ...Option[T]) *Select[T] { |
| 177 |
if len(options) <= 0 { |
| 178 |
return s |
| 179 |
} |
| 180 |
s.options.val = options |
| 181 |
s.filteredOptions = options |
| 182 |
|
| 183 |
s.selectOption() |
| 184 |
|
| 185 |
s.updateViewportHeight() |
| 186 |
s.updateValue() |
| 187 |
|
| 188 |
return s |
| 189 |
} |
| 190 |
|
| 191 |
func (s *Select[T]) selectOption() { |
| 192 |
|
| 193 |
for i, option := range s.options.val { |
| 194 |
if option.Value == s.accessor.Get() { |
| 195 |
s.selected = i |
| 196 |
break |
| 197 |
} |
| 198 |
if option.selected { |
| 199 |
s.selected = i |
| 200 |
break |
| 201 |
} |
| 202 |
} |
| 203 |
s.viewport.YOffset = s.selected |
| 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 |
func (s *Select[T]) OptionsFunc(f func() []Option[T], bindings any) *Select[T] { |
| 230 |
s.options.fn = f |
| 231 |
s.options.bindings = bindings |
| 232 |
|
| 233 |
|
| 234 |
if s.height <= 0 { |
| 235 |
s.height = defaultHeight |
| 236 |
s.updateViewportHeight() |
| 237 |
} |
| 238 |
return s |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
func (s *Select[T]) Inline(v bool) *Select[T] { |
| 243 |
s.inline = v |
| 244 |
if v { |
| 245 |
s.Height(1) |
| 246 |
} |
| 247 |
s.keymap.Left.SetEnabled(v) |
| 248 |
s.keymap.Right.SetEnabled(v) |
| 249 |
s.keymap.Up.SetEnabled(!v) |
| 250 |
s.keymap.Down.SetEnabled(!v) |
| 251 |
return s |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
func (s *Select[T]) Height(height int) *Select[T] { |
| 257 |
s.height = height |
| 258 |
s.updateViewportHeight() |
| 259 |
return s |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
func (s *Select[T]) Validate(validate func(T) error) *Select[T] { |
| 264 |
s.validate = validate |
| 265 |
return s |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
func (s *Select[T]) Error() error { return s.err } |
| 270 |
|
| 271 |
|
| 272 |
func (*Select[T]) Skip() bool { return false } |
| 273 |
|
| 274 |
|
| 275 |
func (*Select[T]) Zoom() bool { return false } |
| 276 |
|
| 277 |
|
| 278 |
func (s *Select[T]) Focus() tea.Cmd { |
| 279 |
s.focused = true |
| 280 |
return nil |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
func (s *Select[T]) Blur() tea.Cmd { |
| 285 |
value := s.accessor.Get() |
| 286 |
if s.inline { |
| 287 |
s.clearFilter() |
| 288 |
s.selectValue(value) |
| 289 |
} |
| 290 |
s.focused = false |
| 291 |
s.err = s.validate(value) |
| 292 |
return nil |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
func (s *Select[T]) Hovered() (T, bool) { |
| 299 |
if len(s.filteredOptions) == 0 || s.selected >= len(s.filteredOptions) { |
| 300 |
var zero T |
| 301 |
return zero, false |
| 302 |
} |
| 303 |
return s.filteredOptions[s.selected].Value, true |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
func (s *Select[T]) KeyBinds() []key.Binding { |
| 308 |
return []key.Binding{ |
| 309 |
s.keymap.Up, |
| 310 |
s.keymap.Down, |
| 311 |
s.keymap.Left, |
| 312 |
s.keymap.Right, |
| 313 |
s.keymap.Filter, |
| 314 |
s.keymap.SetFilter, |
| 315 |
s.keymap.ClearFilter, |
| 316 |
s.keymap.Prev, |
| 317 |
s.keymap.Next, |
| 318 |
s.keymap.Submit, |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
func (s *Select[T]) Init() tea.Cmd { |
| 324 |
return nil |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
func (s *Select[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 329 |
s.updateViewportHeight() |
| 330 |
|
| 331 |
var cmd tea.Cmd |
| 332 |
if s.filtering { |
| 333 |
s.filter, cmd = s.filter.Update(msg) |
| 334 |
} |
| 335 |
|
| 336 |
switch msg := msg.(type) { |
| 337 |
case updateFieldMsg: |
| 338 |
var cmds []tea.Cmd |
| 339 |
if ok, hash := s.title.shouldUpdate(); ok { |
| 340 |
s.title.bindingsHash = hash |
| 341 |
if !s.title.loadFromCache() { |
| 342 |
s.title.loading = true |
| 343 |
cmds = append(cmds, func() tea.Msg { |
| 344 |
return updateTitleMsg{id: s.id, title: s.title.fn(), hash: hash} |
| 345 |
}) |
| 346 |
} |
| 347 |
} |
| 348 |
if ok, hash := s.description.shouldUpdate(); ok { |
| 349 |
s.description.bindingsHash = hash |
| 350 |
if !s.description.loadFromCache() { |
| 351 |
s.description.loading = true |
| 352 |
cmds = append(cmds, func() tea.Msg { |
| 353 |
return updateDescriptionMsg{id: s.id, description: s.description.fn(), hash: hash} |
| 354 |
}) |
| 355 |
} |
| 356 |
} |
| 357 |
if ok, hash := s.options.shouldUpdate(); ok { |
| 358 |
s.clearFilter() |
| 359 |
s.options.bindingsHash = hash |
| 360 |
if s.options.loadFromCache() { |
| 361 |
s.filteredOptions = s.options.val |
| 362 |
s.selected = clamp(s.selected, 0, len(s.options.val)-1) |
| 363 |
} else { |
| 364 |
s.options.loading = true |
| 365 |
s.options.loadingStart = time.Now() |
| 366 |
cmds = append(cmds, func() tea.Msg { |
| 367 |
return updateOptionsMsg[T]{id: s.id, hash: hash, options: s.options.fn()} |
| 368 |
}, s.spinner.Tick) |
| 369 |
} |
| 370 |
} |
| 371 |
return s, tea.Batch(cmds...) |
| 372 |
|
| 373 |
case spinner.TickMsg: |
| 374 |
if !s.options.loading { |
| 375 |
break |
| 376 |
} |
| 377 |
s.spinner, cmd = s.spinner.Update(msg) |
| 378 |
return s, cmd |
| 379 |
|
| 380 |
case updateTitleMsg: |
| 381 |
if msg.id == s.id && msg.hash == s.title.bindingsHash { |
| 382 |
s.title.update(msg.title) |
| 383 |
} |
| 384 |
case updateDescriptionMsg: |
| 385 |
if msg.id == s.id && msg.hash == s.description.bindingsHash { |
| 386 |
s.description.update(msg.description) |
| 387 |
} |
| 388 |
case updateOptionsMsg[T]: |
| 389 |
if msg.id == s.id && msg.hash == s.options.bindingsHash { |
| 390 |
s.options.update(msg.options) |
| 391 |
s.selectOption() |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
s.selected = clamp(s.selected, 0, len(msg.options)-1) |
| 396 |
s.filteredOptions = msg.options |
| 397 |
s.updateValue() |
| 398 |
} |
| 399 |
case tea.KeyMsg: |
| 400 |
s.err = nil |
| 401 |
switch { |
| 402 |
case key.Matches(msg, s.keymap.Filter): |
| 403 |
s.setFiltering(true) |
| 404 |
return s, s.filter.Focus() |
| 405 |
case key.Matches(msg, s.keymap.SetFilter): |
| 406 |
if len(s.filteredOptions) <= 0 { |
| 407 |
s.filter.SetValue("") |
| 408 |
s.filteredOptions = s.options.val |
| 409 |
} |
| 410 |
s.setFiltering(false) |
| 411 |
case key.Matches(msg, s.keymap.ClearFilter): |
| 412 |
s.clearFilter() |
| 413 |
case key.Matches(msg, s.keymap.Up, s.keymap.Left): |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if s.filtering && (msg.String() == "k" || msg.String() == "h") { |
| 421 |
break |
| 422 |
} |
| 423 |
s.selected = s.selected - 1 |
| 424 |
if s.selected < 0 { |
| 425 |
s.selected = len(s.filteredOptions) - 1 |
| 426 |
s.viewport.GotoBottom() |
| 427 |
} |
| 428 |
if s.selected < s.viewport.YOffset { |
| 429 |
s.viewport.SetYOffset(s.selected) |
| 430 |
} |
| 431 |
s.updateValue() |
| 432 |
case key.Matches(msg, s.keymap.GotoTop): |
| 433 |
if s.filtering { |
| 434 |
break |
| 435 |
} |
| 436 |
s.selected = 0 |
| 437 |
s.viewport.GotoTop() |
| 438 |
s.updateValue() |
| 439 |
case key.Matches(msg, s.keymap.GotoBottom): |
| 440 |
if s.filtering { |
| 441 |
break |
| 442 |
} |
| 443 |
s.selected = len(s.filteredOptions) - 1 |
| 444 |
s.viewport.GotoBottom() |
| 445 |
case key.Matches(msg, s.keymap.HalfPageUp): |
| 446 |
s.selected = max(s.selected-s.viewport.Height/2, 0) |
| 447 |
s.viewport.HalfPageUp() |
| 448 |
s.updateValue() |
| 449 |
case key.Matches(msg, s.keymap.HalfPageDown): |
| 450 |
s.selected = min(s.selected+s.viewport.Height/2, len(s.filteredOptions)-1) |
| 451 |
s.viewport.HalfPageDown() |
| 452 |
s.updateValue() |
| 453 |
case key.Matches(msg, s.keymap.Down, s.keymap.Right): |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
if s.filtering && (msg.String() == "j" || msg.String() == "l") { |
| 458 |
break |
| 459 |
} |
| 460 |
s.selected = s.selected + 1 |
| 461 |
if s.selected > len(s.filteredOptions)-1 { |
| 462 |
s.selected = 0 |
| 463 |
s.viewport.GotoTop() |
| 464 |
} |
| 465 |
if s.selected >= s.viewport.YOffset+s.viewport.Height { |
| 466 |
s.viewport.ScrollDown(1) |
| 467 |
} |
| 468 |
s.updateValue() |
| 469 |
case key.Matches(msg, s.keymap.Prev): |
| 470 |
if s.selected >= len(s.filteredOptions) { |
| 471 |
break |
| 472 |
} |
| 473 |
s.updateValue() |
| 474 |
s.err = s.validate(s.accessor.Get()) |
| 475 |
if s.err != nil { |
| 476 |
return s, nil |
| 477 |
} |
| 478 |
s.updateValue() |
| 479 |
return s, PrevField |
| 480 |
case key.Matches(msg, s.keymap.Next, s.keymap.Submit): |
| 481 |
if s.selected >= len(s.filteredOptions) { |
| 482 |
break |
| 483 |
} |
| 484 |
s.setFiltering(false) |
| 485 |
s.updateValue() |
| 486 |
s.err = s.validate(s.accessor.Get()) |
| 487 |
if s.err != nil { |
| 488 |
return s, nil |
| 489 |
} |
| 490 |
s.updateValue() |
| 491 |
return s, NextField |
| 492 |
} |
| 493 |
|
| 494 |
if s.filtering { |
| 495 |
s.filteredOptions = s.options.val |
| 496 |
if s.filter.Value() != "" { |
| 497 |
s.filteredOptions = nil |
| 498 |
for _, option := range s.options.val { |
| 499 |
if s.filterFunc(option.Key) { |
| 500 |
s.filteredOptions = append(s.filteredOptions, option) |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
if len(s.filteredOptions) > 0 { |
| 505 |
s.selected = min(s.selected, len(s.filteredOptions)-1) |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
_, offset, height := s.optionsView() |
| 510 |
if offset > -1 && height > 0 && (offset < s.viewport.YOffset || height+offset >= s.viewport.YOffset+s.viewport.Height) { |
| 511 |
s.viewport.SetYOffset(offset) |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
return s, cmd |
| 516 |
} |
| 517 |
|
| 518 |
func (s *Select[T]) updateValue() { |
| 519 |
if s.selected < len(s.filteredOptions) && s.selected >= 0 { |
| 520 |
s.accessor.Set(s.filteredOptions[s.selected].Value) |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
func (s *Select[T]) updateViewportHeight() { |
| 527 |
|
| 528 |
if s.height <= 0 { |
| 529 |
v, _, _ := s.optionsView() |
| 530 |
s.viewport.Height = lipgloss.Height(v) |
| 531 |
return |
| 532 |
} |
| 533 |
|
| 534 |
offset := 0 |
| 535 |
if ss := s.titleView(); ss != "" { |
| 536 |
offset += lipgloss.Height(ss) |
| 537 |
} |
| 538 |
if ss := s.descriptionView(); ss != "" { |
| 539 |
offset += lipgloss.Height(ss) |
| 540 |
} |
| 541 |
|
| 542 |
s.viewport.Height = max(minHeight, s.height-offset) |
| 543 |
s.viewport.YOffset = s.selected |
| 544 |
} |
| 545 |
|
| 546 |
func (s *Select[T]) activeStyles() *FieldStyles { |
| 547 |
theme := s.theme |
| 548 |
if theme == nil { |
| 549 |
theme = ThemeCharm() |
| 550 |
} |
| 551 |
if s.focused { |
| 552 |
return &theme.Focused |
| 553 |
} |
| 554 |
return &theme.Blurred |
| 555 |
} |
| 556 |
|
| 557 |
func (s *Select[T]) titleView() string { |
| 558 |
var ( |
| 559 |
styles = s.activeStyles() |
| 560 |
sb = strings.Builder{} |
| 561 |
maxWidth = s.width - styles.Base.GetHorizontalFrameSize() |
| 562 |
) |
| 563 |
if s.filtering { |
| 564 |
sb.WriteString(s.filter.View()) |
| 565 |
} else if s.filter.Value() != "" && !s.inline { |
| 566 |
sb.WriteString(styles.Description.Render("/" + s.filter.Value())) |
| 567 |
} else { |
| 568 |
sb.WriteString(styles.Title.Render(wrap(s.title.val, maxWidth))) |
| 569 |
} |
| 570 |
if s.err != nil { |
| 571 |
sb.WriteString(styles.ErrorIndicator.String()) |
| 572 |
} |
| 573 |
return sb.String() |
| 574 |
} |
| 575 |
|
| 576 |
func (s *Select[T]) descriptionView() string { |
| 577 |
if s.description.val == "" { |
| 578 |
return "" |
| 579 |
} |
| 580 |
maxWidth := s.width - s.activeStyles().Base.GetHorizontalFrameSize() |
| 581 |
return s.activeStyles().Description.Render(wrap(s.description.val, maxWidth)) |
| 582 |
} |
| 583 |
|
| 584 |
func (s *Select[T]) optionsView() (string, int, int) { |
| 585 |
var ( |
| 586 |
styles = s.activeStyles() |
| 587 |
sb strings.Builder |
| 588 |
) |
| 589 |
|
| 590 |
if s.options.loading && time.Since(s.options.loadingStart) > spinnerShowThreshold { |
| 591 |
s.spinner.Style = s.activeStyles().MultiSelectSelector.UnsetString() |
| 592 |
sb.WriteString(s.spinner.View() + " Loading...") |
| 593 |
return sb.String(), -1, 1 |
| 594 |
} |
| 595 |
|
| 596 |
if s.inline { |
| 597 |
option := styles.TextInput.Placeholder.Render("No matches") |
| 598 |
if len(s.filteredOptions) > 0 { |
| 599 |
option = styles.SelectedOption.Render(s.filteredOptions[s.selected].Key) |
| 600 |
} |
| 601 |
return lipgloss.NewStyle(). |
| 602 |
Width(s.width). |
| 603 |
Render(lipgloss.JoinHorizontal( |
| 604 |
lipgloss.Left, |
| 605 |
styles.PrevIndicator.Faint(s.selected <= 0).String(), |
| 606 |
option, |
| 607 |
styles.NextIndicator.Faint(s.selected == len(s.filteredOptions)-1).String(), |
| 608 |
)), |
| 609 |
-1, 1 |
| 610 |
} |
| 611 |
|
| 612 |
var cursorOffset int |
| 613 |
var cursorHeight int |
| 614 |
for i, option := range s.filteredOptions { |
| 615 |
selected := s.selected == i |
| 616 |
line := s.renderOption(option, selected) |
| 617 |
if i < s.selected { |
| 618 |
cursorOffset += lipgloss.Height(line) |
| 619 |
} |
| 620 |
if selected { |
| 621 |
cursorHeight = lipgloss.Height(line) |
| 622 |
} |
| 623 |
|
| 624 |
sb.WriteString(line) |
| 625 |
if i < len(s.options.val)-1 { |
| 626 |
sb.WriteString("\n") |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
for i := len(s.filteredOptions); i < len(s.options.val)-1; i++ { |
| 631 |
sb.WriteString("\n") |
| 632 |
} |
| 633 |
|
| 634 |
return sb.String(), cursorOffset, cursorHeight |
| 635 |
} |
| 636 |
|
| 637 |
func (s *Select[T]) renderOption(option Option[T], selected bool) string { |
| 638 |
var ( |
| 639 |
styles = s.activeStyles() |
| 640 |
cursor = styles.SelectSelector.String() |
| 641 |
cursorW = lipgloss.Width(cursor) |
| 642 |
maxWidth = s.width - s.activeStyles().Base.GetHorizontalFrameSize() - cursorW |
| 643 |
) |
| 644 |
|
| 645 |
key := wrap(option.Key, maxWidth) |
| 646 |
|
| 647 |
if selected { |
| 648 |
return lipgloss.JoinHorizontal( |
| 649 |
lipgloss.Left, |
| 650 |
cursor, |
| 651 |
styles.SelectedOption.Render(key), |
| 652 |
) |
| 653 |
} |
| 654 |
return lipgloss.JoinHorizontal( |
| 655 |
lipgloss.Left, |
| 656 |
strings.Repeat(" ", cursorW), |
| 657 |
styles.UnselectedOption.Render(key), |
| 658 |
) |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
func (s *Select[T]) View() string { |
| 663 |
styles := s.activeStyles() |
| 664 |
vpc, _, _ := s.optionsView() |
| 665 |
s.viewport.SetContent(vpc) |
| 666 |
|
| 667 |
var parts []string |
| 668 |
if s.title.val != "" || s.title.fn != nil { |
| 669 |
parts = append(parts, s.titleView()) |
| 670 |
} |
| 671 |
if s.description.val != "" || s.description.fn != nil { |
| 672 |
parts = append(parts, s.descriptionView()) |
| 673 |
} |
| 674 |
parts = append(parts, s.viewport.View()) |
| 675 |
return styles.Base.Width(s.width).Height(s.height). |
| 676 |
Render(strings.Join(parts, "\n")) |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
func (s *Select[T]) clearFilter() { |
| 681 |
s.filter.SetValue("") |
| 682 |
s.filteredOptions = s.options.val |
| 683 |
s.setFiltering(false) |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
func (s *Select[T]) setFiltering(filtering bool) { |
| 688 |
if s.inline && filtering { |
| 689 |
s.filter.Width = lipgloss.Width(s.titleView()) - 1 - 1 |
| 690 |
} |
| 691 |
s.filtering = filtering |
| 692 |
s.keymap.SetFilter.SetEnabled(filtering) |
| 693 |
s.keymap.Filter.SetEnabled(!filtering) |
| 694 |
s.keymap.ClearFilter.SetEnabled(!filtering && s.filter.Value() != "") |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
func (s *Select[T]) filterFunc(option string) bool { |
| 699 |
|
| 700 |
return strings.Contains(strings.ToLower(option), strings.ToLower(s.filter.Value())) |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
func (s *Select[T]) Run() error { |
| 705 |
if s.accessible { |
| 706 |
return s.RunAccessible(os.Stdout, os.Stdin) |
| 707 |
} |
| 708 |
return Run(s) |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
func (s *Select[T]) RunAccessible(w io.Writer, r io.Reader) error { |
| 713 |
styles := s.activeStyles() |
| 714 |
_, _ = fmt.Fprintln(w, styles.Title. |
| 715 |
PaddingRight(1). |
| 716 |
Render(cmp.Or(s.title.val, "Select:"))) |
| 717 |
|
| 718 |
for i, option := range s.options.val { |
| 719 |
_, _ = fmt.Fprintf(w, "%d. %s\n", i+1, option.Key) |
| 720 |
} |
| 721 |
|
| 722 |
var defaultValue *int |
| 723 |
switch s.accessor.(type) { |
| 724 |
case *PointerAccessor[T]: |
| 725 |
s.selectOption() |
| 726 |
idx := s.selected + 1 |
| 727 |
defaultValue = &idx |
| 728 |
} |
| 729 |
prompt := fmt.Sprintf("Enter a number between %d and %d: ", 1, len(s.options.val)) |
| 730 |
if len(s.options.val) == 1 { |
| 731 |
prompt = "There is only one option available; enter the number 1:" |
| 732 |
} |
| 733 |
for { |
| 734 |
choice := accessibility.PromptInt(w, r, prompt, 1, len(s.options.val), defaultValue) |
| 735 |
option := s.options.val[choice-1] |
| 736 |
if err := s.validate(option.Value); err != nil { |
| 737 |
_, _ = fmt.Fprintln(w, err.Error()) |
| 738 |
_, _ = fmt.Fprintln(w) |
| 739 |
continue |
| 740 |
} |
| 741 |
s.accessor.Set(option.Value) |
| 742 |
return nil |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
func (s *Select[T]) WithTheme(theme *Theme) Field { |
| 748 |
if s.theme != nil { |
| 749 |
return s |
| 750 |
} |
| 751 |
s.theme = theme |
| 752 |
s.filter.Cursor.Style = theme.Focused.TextInput.Cursor |
| 753 |
s.filter.Cursor.TextStyle = theme.Focused.TextInput.CursorText |
| 754 |
s.filter.PromptStyle = theme.Focused.TextInput.Prompt |
| 755 |
s.filter.TextStyle = theme.Focused.TextInput.Text |
| 756 |
s.filter.PlaceholderStyle = theme.Focused.TextInput.Placeholder |
| 757 |
s.updateViewportHeight() |
| 758 |
return s |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
func (s *Select[T]) WithKeyMap(k *KeyMap) Field { |
| 763 |
s.keymap = k.Select |
| 764 |
s.keymap.Left.SetEnabled(s.inline) |
| 765 |
s.keymap.Right.SetEnabled(s.inline) |
| 766 |
s.keymap.Up.SetEnabled(!s.inline) |
| 767 |
s.keymap.Down.SetEnabled(!s.inline) |
| 768 |
return s |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
func (s *Select[T]) WithAccessible(accessible bool) Field { |
| 776 |
s.accessible = accessible |
| 777 |
return s |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
func (s *Select[T]) WithWidth(width int) Field { |
| 782 |
s.width = width |
| 783 |
return s |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
func (s *Select[T]) WithHeight(height int) Field { |
| 788 |
return s.Height(height) |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
func (s *Select[T]) WithPosition(p FieldPosition) Field { |
| 793 |
if s.filtering { |
| 794 |
return s |
| 795 |
} |
| 796 |
s.keymap.Prev.SetEnabled(!p.IsFirst()) |
| 797 |
s.keymap.Next.SetEnabled(!p.IsLast()) |
| 798 |
s.keymap.Submit.SetEnabled(p.IsLast()) |
| 799 |
return s |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
func (s *Select[T]) GetKey() string { return s.key } |
| 804 |
|
| 805 |
|
| 806 |
func (s *Select[T]) GetValue() any { |
| 807 |
return s.accessor.Get() |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
func (s *Select[T]) GetFiltering() bool { |
| 812 |
return s.filtering |
| 813 |
} |