Skip to content

Commit

Permalink
feat: better loading
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed May 9, 2024
1 parent 81c646c commit 1b9f805
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions examples/dynamic/dynamic-country/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ func main() {
time.Sleep(1000 * time.Millisecond)
return huh.NewOptions(s...)
}, &country /* only this function when `country` changes */),
huh.NewSelect[string]().
Height(8).
TitleFunc(func() string {
switch country {
case "United States":
return "State"
case "Canada":
return "Province"
default:
return "Territory"
}
}, &country).
OptionsFunc(func() []huh.Option[string] {
s := states[country]
// simulate API call
time.Sleep(1000 * time.Millisecond)
return huh.NewOptions(s...)
}, &country /* only this function when `country` changes */),
huh.NewNote().
TitleFunc(func() string {
return fmt.Sprintf("You selected: %s", country)
Expand Down
23 changes: 22 additions & 1 deletion field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -40,6 +42,7 @@ type Select[T comparable] struct {
focused bool
filtering bool
filter textinput.Model
spinner spinner.Model

// options
inline bool
Expand All @@ -54,6 +57,8 @@ func NewSelect[T comparable]() *Select[T] {
filter := textinput.New()
filter.Prompt = "/"

s := spinner.New(spinner.WithSpinner(spinner.Line))

return &Select[T]{
value: new(T),
validate: func(T) error { return nil },
Expand All @@ -63,6 +68,7 @@ func NewSelect[T comparable]() *Select[T] {
options: Eval[[]Option[T]]{cache: make(map[uint64][]Option[T])},
title: Eval[string]{cache: make(map[uint64]string)},
description: Eval[string]{cache: make(map[uint64]string)},
spinner: s,
}
}

Expand Down Expand Up @@ -292,12 +298,21 @@ func (s *Select[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
s.selected = clamp(s.selected, 0, len(s.options.val)-1)
} else {
s.options.loading = true
s.options.loadingStart = time.Now()
cmds = append(cmds, func() tea.Msg {
return updateOptionsMsg[T]{id: s.id, hash: hash, options: s.options.fn()}
})
}, s.spinner.Tick)
}
}
return s, tea.Batch(cmds...)

case spinner.TickMsg:
if !s.options.loading {
break
}
s.spinner, cmd = s.spinner.Update(msg)
return s, cmd

case updateTitleMsg:
if msg.id == s.id && msg.hash == s.title.bindingsHash {
s.title.update(msg.title)
Expand Down Expand Up @@ -481,6 +496,12 @@ func (s *Select[T]) optionsView() string {
sb strings.Builder
)

if s.options.loading && time.Since(s.options.loadingStart) > spinnerShowThreshold {
s.spinner.Style = s.activeStyles().MultiSelectSelector.UnsetString()
sb.WriteString(s.spinner.View() + " Loading...")
return sb.String()
}

if s.inline {
sb.WriteString(styles.PrevIndicator.Faint(s.selected <= 0).String())
if len(s.filteredOptions) > 0 {
Expand Down

0 comments on commit 1b9f805

Please sign in to comment.