diff --git a/go.mod b/go.mod index 25c5bee3df6a4..a39120258638a 100644 --- a/go.mod +++ b/go.mod @@ -133,7 +133,7 @@ require ( github.com/prometheus/alertmanager v0.27.0 github.com/prometheus/common/sigv4 v0.1.0 github.com/richardartoul/molecule v1.0.0 - github.com/schollz/progressbar/v3 v3.17.1 + github.com/schollz/progressbar/v3 v3.18.0 github.com/shirou/gopsutil/v4 v4.24.12 github.com/thanos-io/objstore v0.0.0-20241217111833-063ea3806b2f github.com/twmb/franz-go v1.18.0 diff --git a/go.sum b/go.sum index 16462dedb4006..9b41e9471f490 100644 --- a/go.sum +++ b/go.sum @@ -1055,8 +1055,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30 h1:yoKAVkEVwAqbGbR8n87rHQ1dulL25rKloGadb3vm770= github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30/go.mod h1:sH0u6fq6x4R5M7WxkoQFY/o7UaiItec0o1LinLCJNq8= -github.com/schollz/progressbar/v3 v3.17.1 h1:bI1MTaoQO+v5kzklBjYNRQLoVpe0zbyRZNK6DFkVC5U= -github.com/schollz/progressbar/v3 v3.17.1/go.mod h1:RzqpnsPQNjUyIgdglUjRLgD7sVnxN1wpmBMV+UiEbL4= +github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA= +github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM= diff --git a/vendor/github.com/schollz/progressbar/v3/progressbar.go b/vendor/github.com/schollz/progressbar/v3/progressbar.go index b994d6d0c1c7b..0ccec7d37094b 100644 --- a/vendor/github.com/schollz/progressbar/v3/progressbar.go +++ b/vendor/github.com/schollz/progressbar/v3/progressbar.go @@ -2,10 +2,13 @@ package progressbar import ( "bytes" + "encoding/json" "errors" "fmt" "io" + "log" "math" + "net/http" "os" "regexp" "strings" @@ -859,6 +862,37 @@ func (p *ProgressBar) ChangeMax64(newMax int64) { p.Add(0) // re-render } +// AddMax takes in a int +// and adds it to the max +// value of the progress bar +func (p *ProgressBar) AddMax(added int) { + p.AddMax64(int64(added)) +} + +// AddMax64 is basically +// the same as AddMax, +// but takes in a int64 +// to avoid casting +func (p *ProgressBar) AddMax64(added int64) { + p.lock.Lock() + + p.config.max += added + + if p.config.showBytes { + p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max), + p.config.useIECUnits) + } + + if p.config.max == -1 { + p.lengthUnknown() + } else { + p.lengthKnown(p.config.max) + } + p.lock.Unlock() // so p.Add can lock + + p.Add(0) // re-render +} + // IsFinished returns true if progress bar is completed func (p *ProgressBar) IsFinished() bool { p.lock.Lock() @@ -978,6 +1012,31 @@ func (p *ProgressBar) State() State { return s } +// StartHTTPServer starts an HTTP server dedicated to serving progress bar updates. This allows you to +// display the status in various UI elements, such as an OS status bar with an `xbar` extension. +// It is recommended to run this function in a separate goroutine to avoid blocking the main thread. +// +// hostPort specifies the address and port to bind the server to, for example, "0.0.0.0:19999". +func (p *ProgressBar) StartHTTPServer(hostPort string) { + // for advanced users, we can return the data as json + http.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/json") + // since the state is a simple struct, we can just ignore the error + bs, _ := json.Marshal(p.State()) + w.Write(bs) + }) + // for others, we just return the description in a plain text format + http.HandleFunc("/desc", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintf(w, + "%d/%d, %.2f%%, %s left", + p.State().CurrentNum, p.State().Max, p.State().CurrentPercent*100, + (time.Second * time.Duration(p.State().SecondsLeft)).String(), + ) + }) + log.Fatal(http.ListenAndServe(hostPort, nil)) +} + // regex matching ansi escape codes var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) diff --git a/vendor/modules.txt b/vendor/modules.txt index 46aa8ed3ce6f3..fe83d65ad6796 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1569,7 +1569,7 @@ github.com/rivo/uniseg # github.com/rs/xid v1.6.0 ## explicit; go 1.16 github.com/rs/xid -# github.com/schollz/progressbar/v3 v3.17.1 +# github.com/schollz/progressbar/v3 v3.18.0 ## explicit; go 1.22 github.com/schollz/progressbar/v3 # github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529