-
Notifications
You must be signed in to change notification settings - Fork 32
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
Created a spinner for let users wait that the test is running #148
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43221c6
Created a spinner for let users wait that the test is running
Bharadwajshivam28 7ef5f87
Removed unused methods
Bharadwajshivam28 4f299d5
used PrintfAPI
Bharadwajshivam28 514d711
created seperate file for spinner
Bharadwajshivam28 e40bf7d
Merge branch 'kubernetes-sigs:main' into Spinner
Bharadwajshivam28 dd76f56
formatted newly made files
Bharadwajshivam28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var spinnerFrames = []string{ | ||
"⠈⠁", "⠈⠑", "⠈⠱", "⠈⡱", "⢀⡱", "⢄⡱", "⢄⡱", "⢆⡱", "⢎⡱", "⢎⡰", | ||
"⢎⡠", "⢎⡀", "⢎⠁", "⠎⠁", "⠊⠁", | ||
} | ||
|
||
type Spinner struct { | ||
stop chan struct{} | ||
stopped chan struct{} | ||
mu *sync.Mutex | ||
running bool | ||
writer io.Writer | ||
ticker *time.Ticker | ||
prefix string | ||
suffix string | ||
frameFormat string | ||
} | ||
|
||
func NewSpinner(w io.Writer) *Spinner { | ||
frameFormat := "\x1b[?7l\r%s%s%s\x1b[?7h" | ||
if runtime.GOOS == "windows" { | ||
frameFormat = "\r%s%s%s" | ||
} | ||
return &Spinner{ | ||
stop: make(chan struct{}, 1), | ||
stopped: make(chan struct{}), | ||
mu: &sync.Mutex{}, | ||
writer: w, | ||
frameFormat: frameFormat, | ||
} | ||
} | ||
|
||
func (s *Spinner) Start() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
if s.running { | ||
return | ||
} | ||
s.running = true | ||
s.ticker = time.NewTicker(time.Millisecond * 100) | ||
go func() { | ||
for { | ||
for _, frame := range spinnerFrames { | ||
select { | ||
case <-s.stop: | ||
func() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.ticker.Stop() | ||
s.running = false | ||
s.stopped <- struct{}{} | ||
}() | ||
return | ||
case <-s.ticker.C: | ||
func() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
fmt.Fprintf(s.writer, s.frameFormat, s.prefix, frame, s.suffix) | ||
}() | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (s *Spinner) Stop() { | ||
s.mu.Lock() | ||
if !s.running { | ||
s.mu.Unlock() | ||
return | ||
} | ||
s.stop <- struct{}{} | ||
s.mu.Unlock() | ||
<-s.stopped | ||
} |
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
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.
By moving the spinner we can revert this to a normal printf and remove the PrintAPI method.
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.
Hey @rjsadow I made a new method
PrintAPI
because I wanted the Spinner to be at top most and below this the function to be running and when usingPrintf
the output is-and when i create separate method for API line the output is-
My end goal is to keep the spinner at top most and everything below it and if i use the common
Printf
and add a new line in that then all the lines will have a new line..Please correct me if i am wrong