-
Notifications
You must be signed in to change notification settings - Fork 132
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 action #292
Open
caarlos0
wants to merge
11
commits into
main
Choose a base branch
from
spinerr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9700db5
feat(spinner): make action return an error
caarlos0 3dd5b75
fix: improvements
caarlos0 2730597
fix: remove default action
caarlos0 529eadf
chore: ref
caarlos0 3e5350e
docs: fix example
caarlos0 c664aaa
Merge remote-tracking branch 'origin/main' into spinerr
caarlos0 29633a0
context
caarlos0 7dd7b8d
docs: fix example
caarlos0 d8d0704
fix: the spinner can actually always have a context
caarlos0 94bdd03
feat: allow a writer too
caarlos0 c3f64db
fix: example
caarlos0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"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, io.Writer) error { | ||
time.Sleep(time.Minute) | ||
return nil | ||
}). | ||
Accessible(false). | ||
Run() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
fmt.Println("Done!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"github.com/charmbracelet/huh/spinner" | ||
) | ||
|
||
func main() { | ||
action := func(_ context.Context, w io.Writer) error { | ||
fmt.Fprintln(w, "Added bottom bun") | ||
time.Sleep(time.Second) | ||
fmt.Fprintln(w, "Added patty") | ||
time.Sleep(time.Second) | ||
fmt.Fprintln(w, "Added condiments") | ||
time.Sleep(time.Second) | ||
fmt.Fprintln(w, "Added top bun") | ||
time.Sleep(time.Second) | ||
return nil | ||
} | ||
_ = spinner.New(). | ||
Title("Preparing your burger"). | ||
ActionErr(action). | ||
// Accessible(true). | ||
Run() | ||
fmt.Println("Order up!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package spinner | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
@@ -23,12 +24,15 @@ import ( | |
// ⣾ Loading... | ||
type Spinner struct { | ||
spinner spinner.Model | ||
action func() | ||
action func(ctx context.Context, w io.Writer) error | ||
caarlos0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx context.Context | ||
accessible bool | ||
output *termenv.Output | ||
title string | ||
titleStyle lipgloss.Style | ||
|
||
err error | ||
buf bytes.Buffer | ||
} | ||
|
||
type Type spinner.Spinner | ||
|
@@ -62,6 +66,15 @@ 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, io.Writer) error { | ||
action() | ||
return nil | ||
} | ||
return s | ||
} | ||
|
||
// ActionErr sets the action of the spinner. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should specify what the difference is between |
||
func (s *Spinner) ActionErr(action func(ctx context.Context, w io.Writer) error) *Spinner { | ||
s.action = action | ||
return s | ||
} | ||
|
@@ -98,24 +111,31 @@ 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: "#FFFDF5", Dark: "#FFFDF5"}), | ||
output: termenv.NewOutput(os.Stdout), | ||
ctx: nil, | ||
buf: bytes.Buffer{}, | ||
} | ||
} | ||
|
||
// 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, &s.buf)} | ||
} | ||
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": | ||
|
@@ -132,41 +152,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 | ||
return s.buf.String() + 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). | ||
|
@@ -176,30 +185,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, os.Stdout) | ||
}() | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even it's an example, I won't use a time. Minute here
You can use a 5s delay it would be enough.
I know this timeout won't be reached but three is a problem with a time.Sleep that will block everything. It's somehow OK for an example. But as it's an example, it should be written in a production-ready code
So here is what I'm suggesting