Skip to content

Commit

Permalink
Merge pull request #6 from SydneyOwl/main
Browse files Browse the repository at this point in the history
feat(spinner): Add last frame and message for spinner
  • Loading branch information
Kei-K23 authored Nov 25, 2024
2 parents fd0dc28 + 9ac51d9 commit 947b8b5
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions spinix.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,20 @@ type ProgressBar struct {
type progressBarStyle string

type Spinner struct {
theme []string // Spinner animation frames
spinnerColor string // Color for loader
speed time.Duration // Interval to update the loader
message string // Message to show next to loader
messageColor string // Color for message text
showMessage bool // Indicate to show message or not
active bool // Status for loading indicator
mutex sync.Mutex // For thread-safe operations
stopCh chan struct{} // Channel to send stop signal
callback func() // Optional callback to run after spinner stops
theme []string // Spinner animation frames
spinnerColor string // Color for loader
speed time.Duration // Interval to update the loader
message string // Message to show next to loader
messageColor string // Color for message text
showMessage bool // Indicate to show message or not
active bool // Status for loading indicator
mutex sync.Mutex // For thread-safe operations
stopCh chan struct{} // Channel to send stop signal
callback func() // Optional callback to run after spinner stops
lastFrame string // Optional frame to show after spinner stops
lastFrameColor string // Color for stop frame
lastMessage string // Optional message to display after spinner stops
lastMessageColor string // Color for last message
}

// SpinnerThemes defines several spinner animation styles.
Expand Down Expand Up @@ -174,7 +178,11 @@ func (s *Spinner) Stop() {
s.stopCh = make(chan struct{})
s.active = false
fmt.Print(clearLine) // Clear line
fmt.Print(showCursor) // Hide the cursor in terminal
fmt.Print(showCursor) // Show the cursor in terminal

if len(s.lastFrame) != 0 || len(s.lastMessage) != 0 {
fmt.Printf("\r%s%s\033[0m %s%s\u001B[0m\n", s.lastFrameColor, s.lastFrame, s.lastMessageColor, s.lastMessage)
}

// Execute callback if set
if s.callback != nil {
Expand Down Expand Up @@ -219,6 +227,30 @@ func (s *Spinner) SetSpeed(speed time.Duration) *Spinner {
return s
}

// SetLastFrame allows setting last frame to display after spinner stops.
func (s *Spinner) SetLastFrame(frame string) *Spinner {
s.lastFrame = frame
return s
}

// SetLastFrameColor allows setting color of last frame to display after spinner stops.
func (s *Spinner) SetLastFrameColor(colorCode string) *Spinner {
s.lastFrameColor = colorCode
return s
}

// SetLastMessage sets a message to display after the spinner stops.
func (s *Spinner) SetLastMessage(message string) *Spinner {
s.lastMessage = message
return s
}

// SetLastMessageColor sets color of last message.
func (s *Spinner) SetLastMessageColor(colorCode string) *Spinner {
s.lastMessageColor = colorCode
return s
}

// animate is an internal function that continuously updates the spinner display
// until it receives a stop signal.
func (s *Spinner) animate() {
Expand Down

0 comments on commit 947b8b5

Please sign in to comment.