Skip to content

Commit

Permalink
Merge 450fd69 into 96f097c
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshba committed Oct 23, 2020
2 parents 96f097c + 450fd69 commit 0533f2d
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions detector/chain.go
Expand Up @@ -55,11 +55,58 @@ func (dc *Chain) Test(currentAdditions []gitrepo.Addition, talismanRC *talismanr
log.Printf("Number of files to scan: %d\n", len(currentAdditions))
log.Printf("Number of detectors: %d\n", len(dc.detectors))
total := len(currentAdditions) * len(dc.detectors)
bar := pb.StartNew(total)
var progressBar = getProgressBar()
progressBar.Start(total)
for _, v := range dc.detectors {
v.Test(cc, currentAdditions, talismanRC, result, func() {
bar.Increment()
progressBar.Increment()
})
}
bar.Finish()
progressBar.Finish()
}

func getProgressBar() progressBar {
if isTerminal() {
return &defaultProgressBar{}
} else {
return &noOpProgressBar{}
}
}

func isTerminal() bool {
fileInfo, _ := os.Stdout.Stat()
return (fileInfo.Mode() & os.ModeCharDevice) != 0
}

type progressBar interface {
Start(int)
Increment()
Finish()
}

type noOpProgressBar struct {
}

func (d *noOpProgressBar) Start(int) {}

func (d *noOpProgressBar) Increment() {}

func (d *noOpProgressBar) Finish() {}

type defaultProgressBar struct {
bar *pb.ProgressBar
}

func (d *defaultProgressBar) Start(total int) {
bar := pb.ProgressBarTemplate(`{{ red "Talisman Scan:" }} {{counters .}} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{percent . | rndcolor }} {{green}} {{blue}}`).New(total)
bar.Set(pb.Terminal, true)
d.bar = bar.Start()
}

func (d *defaultProgressBar) Increment() {
d.bar.Increment()
}

func (d *defaultProgressBar) Finish() {
d.bar.Finish()
}

0 comments on commit 0533f2d

Please sign in to comment.