-
Notifications
You must be signed in to change notification settings - Fork 6
Increase polling frequency #63
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4388508
Reduce polling interval to 1 second, initial working spinner implemen…
nathanjcochran aef1859
Clear spinner line when finished, only render to non-tty when message…
nathanjcochran 2e859ba
Revert some messages
nathanjcochran 8d88833
Fix race condition in spinner.go
nathanjcochran 3c94b3a
Fix go vet error
nathanjcochran 94e165c
Switch to using bubbletea to redraw terminal status message
nathanjcochran e2b792e
Fix duplicate error output
nathanjcochran 462678c
Cleanup
nathanjcochran 1221c03
Merge branch 'main' into nathan/increase-polling-frequency
nathanjcochran ac42720
Consolidate IsTerminal functions
nathanjcochran 6b5ffa8
Separate animated and manual spinners into separate types implementin…
nathanjcochran 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 hidden or 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 hidden or 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,126 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "time" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| "golang.org/x/term" | ||
| ) | ||
|
|
||
| // spinnerFrames defines the animation frames for the spinner | ||
| var spinnerFrames = []string{"⢎ ", "⠎⠁", "⠊⠑", "⠈⠱", " ⡱", "⢀⡰", "⢄⡠", "⢆⡀"} | ||
|
|
||
| type Spinner struct { | ||
| // Populated when output is a TTY | ||
| program *tea.Program | ||
|
|
||
| // Populated when output is not a TTY | ||
| output io.Writer | ||
| model *spinnerModel | ||
| } | ||
|
|
||
| func NewSpinner(output io.Writer, message string, args ...any) *Spinner { | ||
| model := spinnerModel{ | ||
| message: fmt.Sprintf(message, args...), | ||
| } | ||
|
|
||
| // If output is not a TTY, print each message on a new line | ||
| if !isTerminal(output) { | ||
| s := &Spinner{ | ||
| output: output, | ||
| model: &model, | ||
| } | ||
| s.println() | ||
| return s | ||
| } | ||
|
|
||
| // If output is a TTY, use bubbletea to dynamically update the message | ||
| program := tea.NewProgram( | ||
| model, | ||
| tea.WithOutput(output), | ||
| ) | ||
|
|
||
| // Start the program in a goroutine | ||
| go func() { | ||
| if _, err := program.Run(); err != nil { | ||
| fmt.Fprintf(output, "Error displaying output: %s\n", err) | ||
| } | ||
| }() | ||
|
|
||
| return &Spinner{ | ||
| program: program, | ||
| } | ||
| } | ||
|
|
||
| func (s *Spinner) Update(message string, args ...any) { | ||
| message = fmt.Sprintf(message, args...) | ||
| if s.program != nil { | ||
| s.program.Send(updateMsg(message)) | ||
| } else if message != s.model.message { | ||
| s.model.message = message | ||
| s.model.incFrame() | ||
| s.println() | ||
| } | ||
| } | ||
|
|
||
| func (s *Spinner) Stop() { | ||
| if s.program == nil { | ||
| return | ||
| } | ||
|
|
||
| s.program.Quit() | ||
| s.program.Wait() | ||
| } | ||
|
|
||
| func (s *Spinner) println() { | ||
| fmt.Fprintln(s.output, s.model.View()) | ||
| } | ||
|
|
||
| func isTerminal(w io.Writer) bool { | ||
| if f, ok := w.(*os.File); ok { | ||
| return term.IsTerminal(int(f.Fd())) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Message types for the bubbletea model | ||
| type tickMsg struct{} | ||
| type updateMsg string | ||
|
|
||
| // spinnerModel is the bubbletea model for the spinner | ||
| type spinnerModel struct { | ||
| message string | ||
| frame int | ||
| } | ||
|
|
||
| func (m spinnerModel) Init() tea.Cmd { | ||
| return m.tick() | ||
| } | ||
|
|
||
| func (m spinnerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| switch msg := msg.(type) { | ||
| case tickMsg: | ||
| m.incFrame() | ||
| return m, m.tick() | ||
| case updateMsg: | ||
| m.message = string(msg) | ||
| } | ||
| return m, nil | ||
| } | ||
|
|
||
| func (m spinnerModel) View() string { | ||
| return fmt.Sprintf("%s %s", spinnerFrames[m.frame], m.message) | ||
| } | ||
|
|
||
| func (m *spinnerModel) incFrame() { | ||
| m.frame = (m.frame + 1) % len(spinnerFrames) | ||
| } | ||
|
|
||
| func (m *spinnerModel) tick() tea.Cmd { | ||
| return tea.Tick(100*time.Millisecond, func(time.Time) tea.Msg { | ||
| return tickMsg{} | ||
| }) | ||
| } | ||
This file contains hidden or 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
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.
I wrote a similar function for the version check stuff. Should consolidate these.
Reviewing on mobile, too hard to provide a link, sorry.
Uh oh!
There was an error while loading. Please reload this page.
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.
Good call! Done here: ac42720.
I decided to rely on the official golang.org/x/term package rather than the github.com/mattn/go-isatty package you were using. I think the functionality should be essentially the same, but I trust the official package slightly more.