这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ wg.Wait()
uiprogress.Stop()
```

### `Set` counter and tracking progress of the external process

You can use progress bar to track the progress of any external process, for example
the process of execution of github actions workflow run. To get an accurate
elapsed time use `.WithStartedAt(time)` to initialize starting time for the progress
bar.

```
job, _ := FetchExternalJob() // request for external resource
totalStepsCount := len(job.Steps)

uiprogress.Start()
bar := uiprogress.AddBar(totalStepsCount).WithStartedAt(job.StartedAt).AppendCompleted().PrependElapsed()
for !job.IsCompleted {
// poll resource status
job, _ = FetchExternalJob()
// update progress
bar.Set(job.GetCurrentStepNumber())
// wait a second
time.Sleep(time.Second)
}
uiprogress.Stop()
```

## Installation

```sh
Expand Down
17 changes: 15 additions & 2 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (b *Bar) Set(n int) error {
if n > b.Total {
return ErrMaxCurrentReached
}

b.updateElapsed()
b.current = n
return nil
}
Expand All @@ -108,13 +110,17 @@ func (b *Bar) Incr() bool {
if n > b.Total {
return false
}
b.updateElapsed()
b.current = n
return true
}

func (b *Bar) updateElapsed() {
var t time.Time
if b.TimeStarted == t {
b.TimeStarted = time.Now()
}
b.timeElapsed = time.Since(b.TimeStarted)
b.current = n
return true
}

// Current returns the current progress of the bar
Expand Down Expand Up @@ -172,6 +178,13 @@ func (b *Bar) PrependElapsed() *Bar {
return b
}

// WithStartedAt initializes time of start for calculation of elapsed time
func (b *Bar) WithStartedAt(t time.Time) *Bar {
b.TimeStarted = t
b.updateElapsed()
return b
}

// Bytes returns the byte presentation of the progress bar
func (b *Bar) Bytes() []byte {
completedWidth := int(float64(b.Width) * (b.CompletedPercent() / 100.00))
Expand Down