Skip to content
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

Manager updates #17

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"os/signal"
"syscall"
"time"

"github.com/chelnak/ysmrr/pkg/charmap"
Expand Down Expand Up @@ -69,8 +68,8 @@ func (sm *spinnerManager) Start() {
// terminal is properly reset.
// Unsure if this is the right place for this especially given
// that it calls os.Exit.
signals := make(chan os.Signal, 2)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)

go func() {
<-signals
Expand All @@ -82,7 +81,11 @@ func (sm *spinnerManager) Start() {
go sm.render()
}

// Stop signals that all spinners should complete.
// Stop sends a signal to the render goroutine to stop
// rendering. We then stop the ticker and persist the final
// frame for each spinner.
// Finally the deferred tput command will ensure tat the cursor
// is no longer hidden.
func (sm *spinnerManager) Stop() {
sm.done <- true
sm.ticks.Stop()
Expand Down Expand Up @@ -129,20 +132,16 @@ func (sm *spinnerManager) GetMessageColor() colors.Color {
return sm.messageColor
}

func (sm *spinnerManager) setNextFrame() {
sm.frame += 1
if sm.frame >= len(sm.chars) {
sm.frame = 0
}
}

func (sm *spinnerManager) renderFrame() {
for _, s := range sm.spinners {
s.Print(sm.writer, sm.chars[sm.frame])
}
sm.setNextFrame()
}

// This is the code that actually renders the spinners.
// Rendering is done in a separate goroutine so that the main
// goroutine can continue to handle signals.
// The render goroutine is called by Start().
//
// Each tick signal calls renderFrame which in turn will print the current
// frame to the writer provided by the manager.
//
// The render method also emits tput strings to the terminal to set the
// correct location of the cursor.
func (sm *spinnerManager) render() {
tput.Sc(sm.writer)
tput.Civis(sm.writer)
Expand All @@ -159,6 +158,20 @@ func (sm *spinnerManager) render() {
}
}

func (sm *spinnerManager) renderFrame() {
for _, s := range sm.spinners {
s.Print(sm.writer, sm.chars[sm.frame])
}
sm.setNextFrame()
}

func (sm *spinnerManager) setNextFrame() {
sm.frame += 1
if sm.frame >= len(sm.chars) {
sm.frame = 0
}
}

// Option represents a spinner manager option.
type Option func(*spinnerManager)

Expand Down