#16: Refactor to use UI utils for printing msg

This commit is contained in:
abhisek 2023-02-16 16:18:26 +05:30
parent 504598de65
commit e895f8a0ec
No known key found for this signature in database
GPG Key ID: CB92A4990C02A88F
4 changed files with 110 additions and 91 deletions

66
internal/ui/progress.go Normal file
View File

@ -0,0 +1,66 @@
package ui
import (
"os"
"time"
"github.com/jedib0t/go-pretty/v6/progress"
)
var progressWriter progress.Writer
func StartProgressWriter() {
pw := progress.NewWriter()
pw.SetTrackerLength(25)
pw.SetMessageWidth(20)
pw.SetSortBy(progress.SortByPercentDsc)
pw.SetStyle(progress.StyleDefault)
pw.SetOutputWriter(os.Stderr)
pw.SetTrackerPosition(progress.PositionRight)
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.Style().Options.PercentFormat = "%4.1f%%"
pw.Style().Visibility.Pinned = true
pw.Style().Visibility.ETA = true
pw.Style().Visibility.Value = true
progressWriter = pw
go progressWriter.Render()
}
func StopProgressWriter() {
if progressWriter != nil {
progressWriter.Stop()
time.Sleep(1 * time.Second)
}
}
func TrackProgress(message string, total int) any {
tracker := progress.Tracker{Message: message, Total: int64(total),
Units: progress.UnitsDefault}
if progressWriter != nil {
progressWriter.AppendTracker(&tracker)
}
return &tracker
}
func MarkTrackerAsDone(i any) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.MarkAsDone()
}
}
func IncrementTrackerTotal(i any, count int) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.UpdateTotal(tracker.Total + int64(count))
}
}
func IncrementProgress(i any, count int) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.Increment(int64(count))
}
}

39
internal/ui/spinner.go Normal file
View File

@ -0,0 +1,39 @@
package ui
import (
"fmt"
"time"
)
var spinnerChan chan bool
func StartSpinner(msg string) {
style := `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`
frames := []rune(style)
length := len(frames)
spinnerChan = make(chan bool)
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
pos := 0
for {
select {
case <-spinnerChan:
ticker.Stop()
return
case <-ticker.C:
fmt.Printf("\r%s ... %s", msg, string(frames[pos%length]))
pos += 1
}
}
}()
}
func StopSpinner() {
spinnerChan <- true
fmt.Printf("\r")
fmt.Println()
}

View File

@ -2,96 +2,9 @@ package ui
import (
"fmt"
"time"
"github.com/jedib0t/go-pretty/v6/progress"
"os"
)
var progressWriter progress.Writer
var spinnerChan chan bool
func StartSpinner(msg string) {
style := `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`
frames := []rune(style)
length := len(frames)
spinnerChan = make(chan bool)
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
pos := 0
for {
select {
case <-spinnerChan:
ticker.Stop()
return
case <-ticker.C:
fmt.Printf("\r%s ... %s", msg, string(frames[pos%length]))
pos += 1
}
}
}()
}
func StopSpinner() {
spinnerChan <- true
fmt.Printf("\r")
fmt.Println()
}
func StartProgressWriter() {
pw := progress.NewWriter()
pw.SetTrackerLength(25)
pw.SetMessageWidth(20)
pw.SetSortBy(progress.SortByPercentDsc)
pw.SetStyle(progress.StyleDefault)
pw.SetTrackerPosition(progress.PositionRight)
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.Style().Options.PercentFormat = "%4.1f%%"
pw.Style().Visibility.Pinned = true
pw.Style().Visibility.ETA = true
pw.Style().Visibility.Value = true
progressWriter = pw
go progressWriter.Render()
}
func StopProgressWriter() {
if progressWriter != nil {
progressWriter.Stop()
time.Sleep(1 * time.Second)
}
}
func TrackProgress(message string, total int) any {
tracker := progress.Tracker{Message: message, Total: int64(total),
Units: progress.UnitsDefault}
if progressWriter != nil {
progressWriter.AppendTracker(&tracker)
}
return &tracker
}
func MarkTrackerAsDone(i any) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.MarkAsDone()
}
}
func IncrementTrackerTotal(i any, count int) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.UpdateTotal(tracker.Total + int64(count))
}
}
func IncrementProgress(i any, count int) {
if tracker, ok := i.(*progress.Tracker); ok {
tracker.Increment(int64(count))
}
func PrintBanner(s string) {
fmt.Fprintf(os.Stderr, s)
}

View File

@ -7,6 +7,7 @@ import (
"strconv"
"github.com/safedep/dry/utils"
"github.com/safedep/vet/internal/ui"
"github.com/safedep/vet/pkg/common/logger"
"github.com/spf13/cobra"
)
@ -68,7 +69,7 @@ func main() {
func printBanner() {
bRet, err := strconv.ParseBool(os.Getenv("VET_DISABLE_BANNER"))
if (err != nil) || (!bRet) {
fmt.Print(banner)
ui.PrintBanner(banner)
}
}