mirror of
https://github.com/safedep/vet.git
synced 2025-12-11 17:44:20 -06:00
#16: Refactor to use UI utils for printing msg
This commit is contained in:
parent
504598de65
commit
e895f8a0ec
66
internal/ui/progress.go
Normal file
66
internal/ui/progress.go
Normal 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
39
internal/ui/spinner.go
Normal 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()
|
||||||
|
}
|
||||||
@ -2,96 +2,9 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"os"
|
||||||
|
|
||||||
"github.com/jedib0t/go-pretty/v6/progress"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var progressWriter progress.Writer
|
func PrintBanner(s string) {
|
||||||
var spinnerChan chan bool
|
fmt.Fprintf(os.Stderr, s)
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/safedep/dry/utils"
|
"github.com/safedep/dry/utils"
|
||||||
|
"github.com/safedep/vet/internal/ui"
|
||||||
"github.com/safedep/vet/pkg/common/logger"
|
"github.com/safedep/vet/pkg/common/logger"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -68,7 +69,7 @@ func main() {
|
|||||||
func printBanner() {
|
func printBanner() {
|
||||||
bRet, err := strconv.ParseBool(os.Getenv("VET_DISABLE_BANNER"))
|
bRet, err := strconv.ParseBool(os.Getenv("VET_DISABLE_BANNER"))
|
||||||
if (err != nil) || (!bRet) {
|
if (err != nil) || (!bRet) {
|
||||||
fmt.Print(banner)
|
ui.PrintBanner(banner)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user