Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spinner): improve context, action, output, tests #292

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/burger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Burger struct {

func main() {
var burger Burger
var order = Order{Burger: burger}
order := Order{Burger: burger}

// Should we run in accessible mode?
accessible, _ := strconv.ParseBool(os.Getenv("ACCESSIBLE"))
Expand Down Expand Up @@ -152,7 +152,6 @@ func main() {
).WithAccessible(accessible)

err := form.Run()

if err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
Expand Down
28 changes: 28 additions & 0 deletions spinner/examples/context-and-action-and-error/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/charmbracelet/huh/spinner"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

err := spinner.New().
Context(ctx).
ActionErr(func(context.Context) error {
time.Sleep(time.Second * 5)
return nil
}).
Accessible(false).
Run()
if err != nil {
log.Fatalln(err)
}
fmt.Println("Done!")
}
28 changes: 28 additions & 0 deletions spinner/examples/context-and-action/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"fmt"
"log"
"math/rand"
"time"

"github.com/charmbracelet/huh/spinner"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

err := spinner.New().
Context(ctx).
Action(func() {
time.Sleep(time.Minute)
}).
Accessible(rand.Int()%2 == 0).
Run()
if err != nil {
log.Fatalln(err)
}
fmt.Println("Done!")
}
8 changes: 4 additions & 4 deletions spinner/examples/loading/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package main

import (
"fmt"
"os"
"time"

"github.com/charmbracelet/huh/spinner"
)

func main() {
action := func() {
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
}
if err := spinner.New().Title("Preparing your burger...").Action(action).Run(); err != nil {
fmt.Println(err)
os.Exit(1)
fmt.Println("Failed:", err)
return
}
fmt.Println("Order up!")
}

87 changes: 47 additions & 40 deletions spinner/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -23,12 +22,13 @@ import (
// ⣾ Loading...
type Spinner struct {
spinner spinner.Model
action func()
action func(ctx context.Context) error
ctx context.Context
accessible bool
output *termenv.Output
title string
titleStyle lipgloss.Style
err error
}

type Type spinner.Spinner
Expand Down Expand Up @@ -62,6 +62,18 @@ func (s *Spinner) Title(title string) *Spinner {

// Action sets the action of the spinner.
func (s *Spinner) Action(action func()) *Spinner {
s.action = func(context.Context) error {
action()
return nil
}
return s
}

// ActionErr sets the action of the spinner.
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
//
// This is just like [Action], but allows the action to use a `context.Context`
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
// and to return an error.
func (s *Spinner) ActionErr(action func(context.Context) error) *Spinner {
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
s.action = action
return s
}
Expand Down Expand Up @@ -98,24 +110,30 @@ func New() *Spinner {
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#F780E2"))

return &Spinner{
action: func() { time.Sleep(time.Second) },
spinner: s,
ctx: context.Background(),
title: "Loading...",
titleStyle: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#00020A", Dark: "#FFFDF5"}),
output: termenv.NewOutput(os.Stdout),
ctx: nil,
}
}

// Init initializes the spinner.
func (s *Spinner) Init() tea.Cmd {
return s.spinner.Tick
return tea.Batch(s.spinner.Tick, func() tea.Msg {
if s.action != nil {
return doneMsg{err: s.action(s.ctx)}
}
return nil
})
}

// Update updates the spinner.
func (s *Spinner) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case spinner.TickMsg:
case doneMsg:
s.err = msg.err
return s, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
Expand All @@ -132,41 +150,30 @@ func (s *Spinner) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (s *Spinner) View() string {
var title string
if s.title != "" {
title = s.titleStyle.Render(s.title) + " "
title = s.titleStyle.Render(s.title)
}
return s.spinner.View() + title
}

// Run runs the spinner.
func (s *Spinner) Run() error {
if s.accessible {
return s.runAccessible()
}

hasCtx := s.ctx != nil
hasCtxErr := hasCtx && s.ctx.Err() != nil

if hasCtxErr {
if s.ctx.Err() != nil {
if errors.Is(s.ctx.Err(), context.Canceled) {
return nil
}
return s.ctx.Err()
}

p := tea.NewProgram(s, tea.WithContext(s.ctx), tea.WithOutput(os.Stderr))
if s.ctx == nil {
go func() {
s.action()
p.Quit()
}()
if s.accessible {
return s.runAccessible()
}

_, err := p.Run()
if errors.Is(err, tea.ErrProgramKilled) {
return nil
} else {
return err
m, err := tea.NewProgram(s, tea.WithContext(s.ctx), tea.WithOutput(os.Stderr)).Run()
mm := m.(*Spinner)
if mm.err != nil {
return mm.err
}
return err
}

// runAccessible runs the spinner in an accessible mode (statically).
Expand All @@ -176,30 +183,30 @@ func (s *Spinner) runAccessible() error {
title := s.titleStyle.Render(strings.TrimSuffix(s.title, "..."))
fmt.Println(title + frame)

if s.ctx == nil {
s.action()
s.output.ShowCursor()
s.output.CursorBack(len(frame) + len(title))
return nil
actionDone := make(chan error)
if s.action != nil {
go func() {
actionDone <- s.action(s.ctx)
}()
}

actionDone := make(chan struct{})

go func() {
s.action()
actionDone <- struct{}{}
}()

for {
select {
case <-s.ctx.Done():
s.output.ShowCursor()
s.output.CursorBack(len(frame) + len(title))
if errors.Is(s.ctx.Err(), context.Canceled) {
return nil
}
return s.ctx.Err()
case <-actionDone:
case err := <-actionDone:
s.output.ShowCursor()
s.output.CursorBack(len(frame) + len(title))
return nil
return err
}
}
}

type doneMsg struct {
err error
}
Loading