Documentation
¶
Index ¶
- func MaxSpinners() int
- func MaxSteppers() int
- type BarT
- type DownloadTask
- type DownloadTasks
- type DownloadTasksOpt
- type GroupedPB
- type MOpt
- type MultiPB
- type OnCompleted
- type OnDataPrepared
- type OnDone
- type OnStart
- type OnStartCB
- type Opt
- func WithBarAppendText(str string) Opt
- func WithBarExtraTailSpaces(howMany int) Opt
- func WithBarIndentChars(str string) Opt
- func WithBarInitialValue(v int64) Opt
- func WithBarOnCompleted(cb OnCompleted) Opt
- func WithBarOnDataPrepared(cb OnDataPrepared) Opt
- func WithBarOnStart(cb OnStart) Opt
- func WithBarPrependText(str string) Opt
- func WithBarResumeable(b bool) Opt
- func WithBarSpinner(whichOne int) Opt
- func WithBarStepper(whichOne int) Opt
- func WithBarStepperPostInit(cb func(bar BarT)) Opt
- func WithBarTextSchema(schema string) Opt
- func WithBarUpperBound(ub int64) Opt
- func WithBarWidth(w int) Opt
- func WithBarWorker(w Worker) Opt
- type PB
- type SchemaData
- type TaskOpt
- type Tasks
- type Worker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MaxSpinners ¶
func MaxSpinners() int
func MaxSteppers ¶
func MaxSteppers() int
Types ¶
type BarT ¶ added in v1.2.0
type BarT interface { String(pb *pbar) string Bytes(pb *pbar) []byte Percent() string // just for stepper PercentF() float64 // return 0.905 PercentI() int // '90.5' -> return 91 Resumeable() bool SetResumeable(resumeable bool) SetInitialValue(initial int64) SetSchema(schema string) SetWidth(w int) SetIndentChars(s string) SetPrependText(s string) SetAppendText(s string) SetExtraTailSpaces(howMany int) SetBaseColor(clr int) SetHighlightColor(clr int) }
type DownloadTask ¶ added in v1.1.11
type DownloadTask struct {
Url, Filename, Title string
Req *http.Request
Resp *http.Response
File *os.File
Writer io.Writer
Buffer []byte
// contains filtered or unexported fields
}
func (*DownloadTask) Close ¶ added in v1.1.11
func (s *DownloadTask) Close()
func (*DownloadTask) Complete ¶ added in v1.2.7
func (s *DownloadTask) Complete()
type DownloadTasks ¶
type DownloadTasks struct {
// contains filtered or unexported fields
}
func NewDownloadTasks ¶
func NewDownloadTasks(bar MultiPB, opts ...DownloadTasksOpt) *DownloadTasks
NewDownloadTasks is a wrapped NewTasks to simplify the http downloading task.
Via this API you can easily download one and more http files.
type TitledUrl string func (t TitledUrl) String() string { return string(t) } func (t TitledUrl) Title() string { if parse, err := url.Parse(string(t)); err != nil { return string(t) } return path.Base(parse.Path) } func doEachGroup(group []string) { tasks := progressbar.NewDownloadTasks(progressbar.New(), // progressbar.WithTaskAddOnTaskCompleted(func...), ) defer tasks.Close() for _, ver := range group { url1 := TitledUrl("https://dl.google.com/go/go" + ver + ".src.tar.gz") tasks.Add(url1.String(), url1, progressbar.WithBarStepper(whichStepper), ) } tasks.Wait() // start waiting for all tasks completed gracefully } func downloadGroups() { for _, group := range [][]string{ {"1.14.2", "1.15.1"}, {"1.16.1", "1.17.1", "1.18.3"}, } { doEachGroup(group) } }
func (*DownloadTasks) Add ¶
func (s *DownloadTasks) Add(url string, filename any, opts ...Opt)
Add a url as a downloading task, which will be started at background right now.
The downloaded content is stored into local file, `filename` specified. The `filename` will be shown in progressbar as a title. You can customize its title with `interface{ Title() string`. A sample could be:
type TitledUrl string func (t TitledUrl) String() string { return string(t) } func (t TitledUrl) Title() string { if parse, err := url.Parse(string(t)); err != nil { return string(t) } return path.Base(parse.Path) } func doEachGroup(group []string) { tasks := progressbar.NewDownloadTasks(progressbar.New(), // progressbar.WithTaskAddOnTaskCompleted(func...), ) defer tasks.Close() for _, ver := range group { url1 := TitledUrl("https://dl.google.com/go/go" + ver + ".src.tar.gz") tasks.Add(url1.String(), url1, progressbar.WithBarStepper(whichStepper), ) } tasks.Wait() // start waiting for all tasks completed gracefully }
func (*DownloadTasks) Close ¶
func (s *DownloadTasks) Close()
func (*DownloadTasks) Wait ¶
func (s *DownloadTasks) Wait()
type DownloadTasksOpt ¶ added in v1.1.11
type DownloadTasksOpt func(tsk *DownloadTasks)
func WithDownloadTaskLogger ¶ added in v1.2.8
func WithDownloadTaskLogger(logger *slog.Logger) DownloadTasksOpt
func WithDownloadTaskOnStart ¶ added in v1.1.11
func WithDownloadTaskOnStart(fn OnStartCB) DownloadTasksOpt
type GroupedPB ¶ added in v1.2.0
type MultiPB ¶
type MultiPB interface { io.Writer Close() Cancel() // cancel the bar Add(maxBytes int64, title string, opts ...Opt) (index int) Remove(index int) Redraw() SignalExit() <-chan struct{} Bar(index int) BarT Percent(index int) string // just for stepper PercentF(index int) float64 // return 0.905 PercentI(index int) int // '90.5' -> return 91 }
func New ¶
New creates a managed MultiPB progressbar object so you can setup the properties of the bar.
bar := progressbar.New() bar.Add( resp.ContentLength, "downloading go1.14.2.src.tar.gz", // progressbar.WithSpinner(14), // progressbar.WithStepper(3), progressbar.WithBarStepper(0), )
A MultiPB or PB progressbar object is a writable object which can receive the data writing via Writer interface:
f, _ := os.OpenFile("debug.log", os.O_CREATE|os.O_WRONLY, 0o644) _, _ = io.Copy(io.MultiWriter(f, bar), resp.Body) f.Close() bar.Close()
The MultiPB object can be added into Tasks container. For more information to see NewTasks() and NewDownloadTasks().
type OnCompleted ¶
type OnCompleted func(bar PB)
type OnDataPrepared ¶ added in v1.1.1
type OnDataPrepared func(bar PB, data *SchemaData)
type OnStartCB ¶ added in v1.1.11
type OnStartCB func(task *DownloadTask, bar PB) (err error)
type Opt ¶
type Opt func(pb *pbar)
func WithBarAppendText ¶ added in v1.1.1
func WithBarExtraTailSpaces ¶ added in v1.1.1
WithBarExtraTailSpaces specifies how many spaces will be printed at end of each bar. These spaces can wipe out the dirty tail of line.
Default is 8 (spaces). You may specify -1 to disable extra spaces to be printed.
func WithBarIndentChars ¶ added in v1.1.1
func WithBarInitialValue ¶ added in v1.2.7
func WithBarOnCompleted ¶
func WithBarOnCompleted(cb OnCompleted) Opt
func WithBarOnDataPrepared ¶ added in v1.1.1
func WithBarOnDataPrepared(cb OnDataPrepared) Opt
func WithBarOnStart ¶
func WithBarPrependText ¶ added in v1.1.1
func WithBarResumeable ¶ added in v1.2.7
func WithBarSpinner ¶
func WithBarStepper ¶
func WithBarStepperPostInit ¶ added in v1.2.0
func WithBarTextSchema ¶
WithBarTextSchema allows cha
"{{.Indent}}{{.Prepend}} {{.Bar}} {{.Percent}} | {{.Title}} | {{.Current}}/{{.Total}} {{.Speed}} {{.Elapsed}} {{.Append}}"
func WithBarUpperBound ¶
func WithBarWidth ¶
func WithBarWorker ¶
type PB ¶
type PB interface { io.Writer Close() String() string Percent() string // just for stepper PercentF() float64 // return 0.905 PercentI() int // '90.5' -> return 91 Bar() BarT Resumeable() bool SetResumeable(resumeable bool) SetInitialValue(initial int64) UpdateRange(min, max int64) // modify the bounds Step(delta int64) // update the progress LowerBound() int64 // unsafe getter for lowerBound UpperBound() int64 // unsafe getter for upperBound Progress() int64 // unsafe progress getter // Bounds return lowerBound, upperBound and progress atomically. Bounds() (lb, ub, progress int64) }
type SchemaData ¶ added in v1.1.1
type TaskOpt ¶
type TaskOpt func(s *taskOptions)
func WithTaskAddBarOptions ¶
func WithTaskAddBarTitle ¶
func WithTaskAddOnTaskCompleted ¶
func WithTaskAddOnTaskCompleted(cb OnCompleted) TaskOpt
type Tasks ¶
type Tasks struct {
// contains filtered or unexported fields
}
func NewTasks ¶
NewTasks creates a Tasks container which you can add the tasks in it.
tasks := progressbar.NewTasks(progressbar.New()) defer tasks.Close() max := count _, h, _ := terminal.GetSize(int(os.Stdout.Fd())) if max >= h { max = h } for i := whichStepper; i < whichStepper+max; i++ { tasks.Add( progressbar.WithTaskAddBarOptions( progressbar.WithBarStepper(i), progressbar.WithBarUpperBound(100), progressbar.WithBarWidth(32), ), progressbar.WithTaskAddBarTitle("Task "+strconv.Itoa(i)), // fmt.Sprintf("Task %v", i)), progressbar.WithTaskAddOnTaskProgressing(func(bar progressbar.PB, exitCh <-chan struct{}) { for max, ix := bar.UpperBound(), int64(0); ix < max; ix++ { ms := time.Duration(10 + rand.Intn(300)) //nolint:gosec //just a demo time.Sleep(time.Millisecond * ms) bar.Step(1) } }), ) } tasks.Wait()
Above.