Skip to content

Commit

Permalink
make spinner more general and use it in fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 9, 2023
1 parent 6a394d6 commit e39017d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
29 changes: 28 additions & 1 deletion internal/file/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/debricked/cli/internal/tui"
)

var EXCLUDED_EXT = []string{
Expand Down Expand Up @@ -86,10 +88,13 @@ type IFingerprint interface {
}

type Fingerprinter struct {
spinnerManager tui.ISpinnerManager
}

func NewFingerprinter() *Fingerprinter {
return &Fingerprinter{}
return &Fingerprinter{
spinnerManager: tui.NewSpinnerManager("Fingerprinting", "0"),
}
}

type FileFingerprint struct {
Expand All @@ -112,7 +117,15 @@ func (f *Fingerprinter) FingerprintFiles(rootPath string, exclusions []string) (

fingerprints := Fingerprints{}

f.spinnerManager.Start()
spinnerMessage := "files processed"
spinner := f.spinnerManager.AddSpinner(spinnerMessage)

nbFiles := 0

err := filepath.Walk(rootPath, func(path string, fileInfo os.FileInfo, err error) error {
nbFiles++

if err != nil {
return err
}
Expand All @@ -128,9 +141,23 @@ func (f *Fingerprinter) FingerprintFiles(rootPath string, exclusions []string) (

fingerprints.Append(fingerprint)

if nbFiles%100 == 0 {
f.spinnerManager.SetSpinnerMessage(spinner, spinnerMessage, fmt.Sprintf("%d", nbFiles))
}

return nil
})

f.spinnerManager.SetSpinnerMessage(spinner, spinnerMessage, fmt.Sprintf("%d", nbFiles))

if err != nil {
spinner.Error()
} else {
spinner.Complete()
}

f.spinnerManager.Stop()

return fingerprints, err
}

Expand Down
8 changes: 4 additions & 4 deletions internal/resolution/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (scheduler *Scheduler) Schedule(jobs []job.IJob) (IResolution, error) {
scheduler.queue = make(chan queueItem, len(jobs))
scheduler.waitGroup.Add(len(jobs))

scheduler.spinnerManager = tui.NewSpinnerManager()
scheduler.spinnerManager = tui.NewSpinnerManager("Resolving", "waiting for worker")

for w := 1; w <= scheduler.workers; w++ {
go scheduler.worker()
Expand Down Expand Up @@ -75,16 +75,16 @@ func (scheduler *Scheduler) worker() {
func (scheduler *Scheduler) updateStatus(item queueItem) {
for {
msg := <-item.job.ReceiveStatus()
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), msg)
scheduler.spinnerManager.SetSpinnerMessage(item.spinner, item.job.GetFile(), msg)
}
}

func (scheduler *Scheduler) finish(item queueItem) {
if item.job.Errors().HasError() {
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), "failed")
scheduler.spinnerManager.SetSpinnerMessage(item.spinner, item.job.GetFile(), "failed")
item.spinner.Error()
} else {
tui.SetSpinnerMessage(item.spinner, item.job.GetFile(), "done")
scheduler.spinnerManager.SetSpinnerMessage(item.spinner, item.job.GetFile(), "done")
item.spinner.Complete()
}
}
15 changes: 9 additions & 6 deletions internal/tui/spinner_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ type ISpinnerManager interface {
AddSpinner(file string) *ysmrr.Spinner
Start()
Stop()
SetSpinnerMessage(spinner *ysmrr.Spinner, filename string, message string)
}

type SpinnerManager struct {
spinnerManager ysmrr.SpinnerManager
spinnerManager ysmrr.SpinnerManager
baseString string
spinnerStartMessage string
}

func NewSpinnerManager() SpinnerManager {
return SpinnerManager{ysmrr.NewSpinnerManager(ysmrr.WithSpinnerColor(colors.FgHiBlue))}
func NewSpinnerManager(baseString string, spinnerStartMessage string) SpinnerManager {
return SpinnerManager{ysmrr.NewSpinnerManager(ysmrr.WithSpinnerColor(colors.FgHiBlue)), baseString, spinnerStartMessage}
}

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

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

func SetSpinnerMessage(spinner *ysmrr.Spinner, filename string, message string) {
func (sm SpinnerManager) SetSpinnerMessage(spinner *ysmrr.Spinner, filename string, message string) {
const maxNumberOfChars = 50
truncatedFilename := filename
if len(truncatedFilename) > maxNumberOfChars {
Expand All @@ -60,5 +63,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", sm.baseString, file, message))
}
29 changes: 23 additions & 6 deletions internal/tui/spinner_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@ import (
)

func TestNewSpinnerManager(t *testing.T) {
spinnerManager := NewSpinnerManager()
spinnerManager := NewSpinnerManager(
"Resolving",
"waiting for worker",
)

assert.NotNil(t, spinnerManager)
}

func TestSetSpinnerMessage(t *testing.T) {
spinnerManager := NewSpinnerManager()
spinnerManager := NewSpinnerManager(
"Resolving",
"waiting for worker",
)
message := "test"
spinner := spinnerManager.AddSpinner(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)
spinnerManager.SetSpinnerMessage(spinner, fileName, message)
assert.Contains(t, spinner.GetMessage(), fmt.Sprintf("Resolving %s: %s", color.YellowString(fileName), message))
}

func TestSetSpinnerMessageLongFilenameParts(t *testing.T) {
spinnerManager := NewSpinnerManager()
spinnerManager := NewSpinnerManager(
"Resolving",
"waiting for worker",
)
longFilenameParts := []string{
"directory",
"sub-directory################################################################",
Expand All @@ -43,7 +53,10 @@ func TestSetSpinnerMessageLongFilenameParts(t *testing.T) {
}

func TestSetSpinnerMessageLongFilenameManyDirs(t *testing.T) {
spinnerManager := NewSpinnerManager()
spinnerManager := NewSpinnerManager(
"Resolving",
"waiting for worker",
)
longFilenameParts := []string{
"directory",
"sub-directory",
Expand All @@ -70,7 +83,11 @@ func TestSetSpinnerMessageLongFilenameManyDirs(t *testing.T) {
}

func TestStartStop(t *testing.T) {
spinnerManager := NewSpinnerManager()
spinnerManager := NewSpinnerManager(
"Resolving",
"waiting for worker",
)

spinnerManager.Start()
spinnerManager.Stop()
}

0 comments on commit e39017d

Please sign in to comment.