Skip to content

Commit

Permalink
Update spinner manager to support multiple actions in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed May 10, 2023
1 parent bc093ce commit 0edbbb7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/callgraph/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import error "github.com/debricked/cli/pkg/io/err"

type IJob interface {
GetFiles() []string
GetDir() string
Errors() error.IErrors
Run()
ReceiveStatus() chan string
Expand Down
10 changes: 6 additions & 4 deletions pkg/callgraph/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Scheduler struct {
spinnerManager tui.ISpinnerManager
}

const callgraph = "Callgraph"

func NewScheduler(workers int) *Scheduler {
return &Scheduler{workers: workers, waitGroup: sync.WaitGroup{}}
}
Expand All @@ -38,7 +40,7 @@ func (scheduler *Scheduler) Schedule(jobs []job.IJob) (IGeneration, error) {
fmt.Println("Done with spinner start", len(jobs))

for _, j := range jobs {
spinner := scheduler.spinnerManager.AddSpinner("Test")
spinner := scheduler.spinnerManager.AddSpinner(callgraph, j.GetDir())
scheduler.queue <- queueItem{
job: j,
spinner: spinner,
Expand All @@ -63,16 +65,16 @@ func (scheduler *Scheduler) Schedule(jobs []job.IJob) (IGeneration, error) {
func (scheduler *Scheduler) updateStatus(item queueItem) {
for {
msg := <-item.job.ReceiveStatus()
tui.SetSpinnerMessage(item.spinner, "file", msg)
tui.SetSpinnerMessage(item.spinner, callgraph, item.job.GetDir(), msg)
}
}

func (scheduler *Scheduler) finish(item queueItem) {
if item.job.Errors().HasError() {
tui.SetSpinnerMessage(item.spinner, "file", "failed")
tui.SetSpinnerMessage(item.spinner, callgraph, item.job.GetDir(), "failed")
item.spinner.Error()
} else {
tui.SetSpinnerMessage(item.spinner, "file", "done")
tui.SetSpinnerMessage(item.spinner, callgraph, item.job.GetDir(), "done")
item.spinner.Complete()
}
}
10 changes: 6 additions & 4 deletions pkg/resolution/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Scheduler struct {
spinnerManager tui.ISpinnerManager
}

const resolving = "Resolving"

func NewScheduler(workers int) *Scheduler {
return &Scheduler{workers: workers, waitGroup: sync.WaitGroup{}}
}
Expand All @@ -44,7 +46,7 @@ func (scheduler *Scheduler) Schedule(jobs []job.IJob) (IResolution, error) {
})

for _, j := range jobs {
spinner := scheduler.spinnerManager.AddSpinner(j.GetFile())
spinner := scheduler.spinnerManager.AddSpinner(resolving, j.GetFile())
scheduler.queue <- queueItem{
job: j,
spinner: spinner,
Expand Down Expand Up @@ -75,16 +77,16 @@ func (scheduler *Scheduler) worker() {
func (scheduler *Scheduler) updateStatus(item queueItem) {
for {
msg := <-item.job.ReceiveStatus()
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), msg)
tui.SetSpinnerMessage(item.spinner, resolving, item.job.GetFile(), msg)
}
}

func (scheduler *Scheduler) finish(item queueItem) {
if item.job.Errors().HasError() {
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), "failed")
tui.SetSpinnerMessage(item.spinner, resolving, item.job.GetFile(), "failed")
item.spinner.Error()
} else {
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), "done")
tui.SetSpinnerMessage(item.spinner, resolving, item.job.GetFile(), "done")
item.spinner.Complete()
}
}
10 changes: 5 additions & 5 deletions pkg/tui/spinner_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type ISpinnerManager interface {
AddSpinner(file string) *ysmrr.Spinner
AddSpinner(action string, file string) *ysmrr.Spinner
Start()
Stop()
}
Expand All @@ -25,9 +25,9 @@ func NewSpinnerManager() SpinnerManager {
return SpinnerManager{ysmrr.NewSpinnerManager(ysmrr.WithSpinnerColor(colors.FgHiBlue))}
}

func (sm SpinnerManager) AddSpinner(file string) *ysmrr.Spinner {
func (sm SpinnerManager) AddSpinner(action string, file string) *ysmrr.Spinner {
spinner := sm.spinnerManager.AddSpinner("")
SetSpinnerMessage(spinner, file, "waiting for worker")
SetSpinnerMessage(spinner, action, file, "waiting for worker")

return spinner
}
Expand All @@ -40,7 +40,7 @@ func (sm SpinnerManager) Stop() {
sm.spinnerManager.Stop()
}

func SetSpinnerMessage(spinner *ysmrr.Spinner, filename string, message string) {
func SetSpinnerMessage(spinner *ysmrr.Spinner, action string, filename string, message string) {
const maxNumberOfChars = 50
truncatedFilename := filename
if len(truncatedFilename) > maxNumberOfChars {
Expand All @@ -60,5 +60,5 @@ func SetSpinnerMessage(spinner *ysmrr.Spinner, filename string, message string)

}
file := color.YellowString(truncatedFilename)
spinner.UpdateMessage(fmt.Sprintf("Resolving %s: %s", file, message))
spinner.UpdateMessage(fmt.Sprintf("%s %s: %s", action, file, message))
}
24 changes: 20 additions & 4 deletions pkg/tui/spinner_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert"
)

const resolving = "Resolving"

func TestNewSpinnerManager(t *testing.T) {
spinnerManager := NewSpinnerManager()
assert.NotNil(t, spinnerManager)
Expand All @@ -17,13 +19,27 @@ func TestNewSpinnerManager(t *testing.T) {
func TestSetSpinnerMessage(t *testing.T) {
spinnerManager := NewSpinnerManager()
message := "test"
spinner := spinnerManager.AddSpinner(message)
spinner := spinnerManager.AddSpinner(resolving, message)
assert.Contains(t, spinner.GetMessage(), fmt.Sprintf("Resolving %s: waiting for worker", color.YellowString(message)))

fileName := "file-name"
message = "new test message"

SetSpinnerMessage(spinner, fileName, message)
SetSpinnerMessage(spinner, resolving, fileName, message)
assert.Contains(t, spinner.GetMessage(), fmt.Sprintf("Resolving %s: %s", color.YellowString(fileName), message))
}

func TestSetDifferentActionSpinnerMessage(t *testing.T) {
spinnerManager := NewSpinnerManager()
message := "test"
action := "Callgraph"
spinner := spinnerManager.AddSpinner(action, message)
assert.Contains(t, spinner.GetMessage(), fmt.Sprintf("Callgraph %s: waiting for worker", color.YellowString(message)))

fileName := "file-name"
message = "new test message"

SetSpinnerMessage(spinner, resolving, fileName, message)
assert.Contains(t, spinner.GetMessage(), fmt.Sprintf("Resolving %s: %s", color.YellowString(fileName), message))
}

Expand All @@ -36,7 +52,7 @@ func TestSetSpinnerMessageLongFilenameParts(t *testing.T) {
}
longFileName := filepath.Join(longFilenameParts...)

spinner := spinnerManager.AddSpinner(longFileName)
spinner := spinnerManager.AddSpinner(resolving, longFileName)
message := spinner.GetMessage()

assert.Contains(t, message, longFileName)
Expand All @@ -63,7 +79,7 @@ func TestSetSpinnerMessageLongFilenameManyDirs(t *testing.T) {
longFilenameParts[len(longFilenameParts)-1],
}
truncatedFilename := filepath.Join(truncatedFilenameParts...)
spinner := spinnerManager.AddSpinner(longFileName)
spinner := spinnerManager.AddSpinner(resolving, longFileName)
message := spinner.GetMessage()

assert.Contains(t, message, truncatedFilename)
Expand Down

0 comments on commit 0edbbb7

Please sign in to comment.