Skip to content

Awkward Spinner Behavior for Fast Actions #159

Closed
@screwage

Description

Describe the bug

When running a Spinner with a quick action, any output following the spinner's lifecycle is consistently pushed to the side.

To Reproduce

Here's a simple file that shows this weird behavior:

package main

import (
	"time"

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

func main() {
	spinner.New().Title("I'm not so quick..").Action(func() {
		time.Sleep(2 * time.Second)
	}).Run()
	huh.NewNote().Title("I'm right over here! Hit Enter to see the next spinner!").Run()

	spinner.New().Title("I'm too fast!").Action(func() {
		// No-op
	}).Run()
	huh.NewNote().Title("<- I swear there was a spinner here somewhere!").Run()
}

out

In this case, I've used a no-op action to showcase the behavior, but I've been able to replicate this in other projects where the action is occasionally faster than expected.

Expected behavior

I expected this to behave similarly to its equivalent counterpart, using context:

package main

import (
	"context"
	"time"

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

func main() {
	ctxSlow, cancelSlow := context.WithCancel(context.Background())

	go func() {
		time.Sleep(2 * time.Second)
		cancelSlow()
	}()
	spinner.New().Title("I'm not so quick..").Context(ctxSlow).Run()
	huh.NewNote().Title("I'm right over here! Hit Enter to see the next spinner!").Run()

	ctxFast, cancelFast := context.WithCancel(context.Background())

	go func() {
		// No-op
		cancelFast()
	}()

	spinner.New().Title("I'm too fast!").Context(ctxFast).Run()
	huh.NewNote().Title("There was a fast spinner here, but at least I'm not over there ->").Run()
}

out

(Unrelated to this issue, but I've only been using Go for ~3 weeks, if my examples are weird or I'm misusing contexts, please let me know :) )

Environment

My example gif was recorded using vhs on an M2 Macbook Air running Sonoma 14.2.1, but I've also been able to replicate this on Windows 10 machines.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingspinner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions